Skip to content

Commit 4ca4e09

Browse files
committed
Suggest removal of arguments for unit variant, not replacement
1 parent e90c5fb commit 4ca4e09

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

compiler/rustc_typeck/src/check/callee.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
349349
ty::FnPtr(sig) => (sig, None),
350350
ref t => {
351351
let mut unit_variant = None;
352+
let mut removal_span = call_expr.span;
352353
if let ty::Adt(adt_def, ..) = t {
353354
if adt_def.is_enum() {
354355
if let hir::ExprKind::Call(expr, _) = call_expr.kind {
356+
removal_span =
357+
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
355358
unit_variant =
356359
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
357360
}
@@ -379,14 +382,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
379382
);
380383

381384
if let Some(ref path) = unit_variant {
382-
err.span_suggestion(
383-
call_expr.span,
385+
err.span_suggestion_verbose(
386+
removal_span,
384387
&format!(
385-
"`{}` is a unit variant, you need to write it \
386-
without the parentheses",
388+
"`{}` is a unit variant, you need to write it without the parentheses",
387389
path
388390
),
389-
path.to_string(),
391+
String::new(),
390392
Applicability::MachineApplicable,
391393
);
392394
}

src/test/ui/empty/empty-struct-unit-expr.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ LL | let e4 = E::Empty4();
2222
|
2323
help: `E::Empty4` is a unit variant, you need to write it without the parentheses
2424
|
25-
LL | let e4 = E::Empty4;
26-
| ~~~~~~~~~
25+
LL - let e4 = E::Empty4();
26+
LL + let e4 = E::Empty4;
27+
|
2728

2829
error[E0618]: expected function, found `empty_struct::XEmpty2`
2930
--> $DIR/empty-struct-unit-expr.rs:18:15
@@ -43,8 +44,9 @@ LL | let xe4 = XE::XEmpty4();
4344
|
4445
help: `XE::XEmpty4` is a unit variant, you need to write it without the parentheses
4546
|
46-
LL | let xe4 = XE::XEmpty4;
47-
| ~~~~~~~~~~~
47+
LL - let xe4 = XE::XEmpty4();
48+
LL + let xe4 = XE::XEmpty4;
49+
|
4850

4951
error: aborting due to 4 previous errors
5052

src/test/ui/error-codes/E0618.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ LL | X::Entry();
1111
|
1212
help: `X::Entry` is a unit variant, you need to write it without the parentheses
1313
|
14-
LL | X::Entry;
15-
| ~~~~~~~~
14+
LL - X::Entry();
15+
LL + X::Entry;
16+
|
1617

1718
error[E0618]: expected function, found `i32`
1819
--> $DIR/E0618.rs:9:5

src/test/ui/resolve/privacy-enum-ctor.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,9 @@ LL | let _ = Z::Unit();
340340
|
341341
help: `Z::Unit` is a unit variant, you need to write it without the parentheses
342342
|
343-
LL | let _ = Z::Unit;
344-
| ~~~~~~~
343+
LL - let _ = Z::Unit();
344+
LL + let _ = Z::Unit;
345+
|
345346

346347
error[E0308]: mismatched types
347348
--> $DIR/privacy-enum-ctor.rs:43:16
@@ -374,8 +375,9 @@ LL | let _: E = m::E::Unit();
374375
|
375376
help: `m::E::Unit` is a unit variant, you need to write it without the parentheses
376377
|
377-
LL | let _: E = m::E::Unit;
378-
| ~~~~~~~~~~
378+
LL - let _: E = m::E::Unit();
379+
LL + let _: E = m::E::Unit;
380+
|
379381

380382
error[E0308]: mismatched types
381383
--> $DIR/privacy-enum-ctor.rs:51:16
@@ -408,8 +410,9 @@ LL | let _: E = E::Unit();
408410
|
409411
help: `E::Unit` is a unit variant, you need to write it without the parentheses
410412
|
411-
LL | let _: E = E::Unit;
412-
| ~~~~~~~
413+
LL - let _: E = E::Unit();
414+
LL + let _: E = E::Unit;
415+
|
413416

414417
error: aborting due to 23 previous errors
415418

src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ LL | Alias::Unit();
2929
|
3030
help: `Alias::Unit` is a unit variant, you need to write it without the parentheses
3131
|
32-
LL | Alias::Unit;
33-
| ~~~~~~~~~~~
32+
LL - Alias::Unit();
33+
LL + Alias::Unit;
34+
|
3435

3536
error[E0164]: expected tuple struct or tuple variant, found unit variant `Alias::Unit`
3637
--> $DIR/incorrect-variant-form-through-alias-caught.rs:17:9

0 commit comments

Comments
 (0)