Skip to content

Commit 04c590b

Browse files
Be more precise when suggesting removal of parens on unit adt ctor
1 parent 7210e46 commit 04c590b

File tree

12 files changed

+86
-39
lines changed

12 files changed

+86
-39
lines changed

compiler/rustc_typeck/src/check/callee.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::type_error_struct;
44

55
use rustc_errors::{struct_span_err, Applicability, Diagnostic};
66
use rustc_hir as hir;
7-
use rustc_hir::def::{Namespace, Res};
7+
use rustc_hir::def::{self, Namespace, Res};
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
99
use rustc_infer::{
1010
infer,
@@ -390,17 +390,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390390
(fn_sig, Some(def_id))
391391
}
392392
ty::FnPtr(sig) => (sig, None),
393-
ref t => {
393+
_ => {
394394
let mut unit_variant = None;
395-
let mut removal_span = call_expr.span;
396-
if let ty::Adt(adt_def, ..) = t
397-
&& adt_def.is_enum()
398-
&& let hir::ExprKind::Call(expr, _) = call_expr.kind
395+
if let hir::ExprKind::Path(qpath) = &callee_expr.kind
396+
&& let Res::Def(def::DefKind::Ctor(kind, def::CtorKind::Const), _)
397+
= self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
398+
// Only suggest removing parens if there are no arguments
399+
&& arg_exprs.is_empty()
400+
&& let Ok(path) = self.tcx.sess.source_map().span_to_snippet(callee_expr.span)
399401
{
400-
removal_span =
401-
expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
402+
let descr = match kind {
403+
def::CtorOf::Struct => "struct",
404+
def::CtorOf::Variant => "enum variant",
405+
};
406+
let removal_span =
407+
callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
402408
unit_variant =
403-
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
409+
Some((removal_span, descr, path));
404410
}
405411

406412
let callee_ty = self.resolve_vars_if_possible(callee_ty);
@@ -410,8 +416,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
410416
callee_ty,
411417
E0618,
412418
"expected function, found {}",
413-
match unit_variant {
414-
Some(ref path) => format!("enum variant `{path}`"),
419+
match &unit_variant {
420+
Some((_, kind, path)) => format!("{kind} `{path}`"),
415421
None => format!("`{callee_ty}`"),
416422
}
417423
);
@@ -423,11 +429,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
423429
callee_expr.span,
424430
);
425431

426-
if let Some(ref path) = unit_variant {
432+
if let Some((removal_span, kind, path)) = &unit_variant {
427433
err.span_suggestion_verbose(
428-
removal_span,
434+
*removal_span,
429435
&format!(
430-
"`{path}` is a unit variant, you need to write it without the parentheses",
436+
"`{path}` is a unit {kind}, and does not take parentheses to be constructed",
431437
),
432438
"",
433439
Applicability::MachineApplicable,
@@ -470,7 +476,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
470476
if let Some(span) = self.tcx.hir().res_span(def) {
471477
let callee_ty = callee_ty.to_string();
472478
let label = match (unit_variant, inner_callee_path) {
473-
(Some(path), _) => Some(format!("`{path}` defined here")),
479+
(Some((_, kind, path)), _) => Some(format!("{kind} `{path}` defined here")),
474480
(_, Some(hir::QPath::Resolved(_, path))) => self
475481
.tcx
476482
.sess

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ enum E {
1212
}
1313

1414
fn main() {
15-
let e2 = Empty2(); //~ ERROR expected function, found `Empty2`
15+
let e2 = Empty2(); //~ ERROR expected function, found struct `Empty2`
1616
let e4 = E::Empty4();
1717
//~^ ERROR expected function, found enum variant `E::Empty4` [E0618]
18-
let xe2 = XEmpty2(); //~ ERROR expected function, found `empty_struct::XEmpty2`
18+
let xe2 = XEmpty2(); //~ ERROR expected function, found struct `XEmpty2`
1919
let xe4 = XE::XEmpty4();
2020
//~^ ERROR expected function, found enum variant `XE::XEmpty4` [E0618]
2121
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,50 @@
1-
error[E0618]: expected function, found `Empty2`
1+
error[E0618]: expected function, found struct `Empty2`
22
--> $DIR/empty-struct-unit-expr.rs:15:14
33
|
44
LL | struct Empty2;
5-
| ------------- `Empty2` defined here
5+
| ------------- struct `Empty2` defined here
66
...
77
LL | let e2 = Empty2();
88
| ^^^^^^--
99
| |
1010
| call expression requires function
11+
|
12+
help: `Empty2` is a unit struct, and does not take parentheses to be constructed
13+
|
14+
LL - let e2 = Empty2();
15+
LL + let e2 = Empty2;
16+
|
1117

1218
error[E0618]: expected function, found enum variant `E::Empty4`
1319
--> $DIR/empty-struct-unit-expr.rs:16:14
1420
|
1521
LL | Empty4
16-
| ------ `E::Empty4` defined here
22+
| ------ enum variant `E::Empty4` defined here
1723
...
1824
LL | let e4 = E::Empty4();
1925
| ^^^^^^^^^--
2026
| |
2127
| call expression requires function
2228
|
23-
help: `E::Empty4` is a unit variant, you need to write it without the parentheses
29+
help: `E::Empty4` is a unit enum variant, and does not take parentheses to be constructed
2430
|
2531
LL - let e4 = E::Empty4();
2632
LL + let e4 = E::Empty4;
2733
|
2834

29-
error[E0618]: expected function, found `empty_struct::XEmpty2`
35+
error[E0618]: expected function, found struct `XEmpty2`
3036
--> $DIR/empty-struct-unit-expr.rs:18:15
3137
|
3238
LL | let xe2 = XEmpty2();
3339
| ^^^^^^^--
3440
| |
3541
| call expression requires function
42+
|
43+
help: `XEmpty2` is a unit struct, and does not take parentheses to be constructed
44+
|
45+
LL - let xe2 = XEmpty2();
46+
LL + let xe2 = XEmpty2;
47+
|
3648

3749
error[E0618]: expected function, found enum variant `XE::XEmpty4`
3850
--> $DIR/empty-struct-unit-expr.rs:19:15
@@ -42,7 +54,7 @@ LL | let xe4 = XE::XEmpty4();
4254
| |
4355
| call expression requires function
4456
|
45-
help: `XE::XEmpty4` is a unit variant, you need to write it without the parentheses
57+
help: `XE::XEmpty4` is a unit enum variant, and does not take parentheses to be constructed
4658
|
4759
LL - let xe4 = XE::XEmpty4();
4860
LL + let xe4 = XE::XEmpty4;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ error[E0618]: expected function, found enum variant `X::Entry`
22
--> $DIR/E0618.rs:6:5
33
|
44
LL | Entry,
5-
| ----- `X::Entry` defined here
5+
| ----- enum variant `X::Entry` defined here
66
...
77
LL | X::Entry();
88
| ^^^^^^^^--
99
| |
1010
| call expression requires function
1111
|
12-
help: `X::Entry` is a unit variant, you need to write it without the parentheses
12+
help: `X::Entry` is a unit enum variant, and does not take parentheses to be constructed
1313
|
1414
LL - X::Entry();
1515
LL + X::Entry;

src/test/ui/issues/issue-20714.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct G;
22

33
fn main() {
4-
let g = G(); //~ ERROR: expected function, found `G`
4+
let g = G(); //~ ERROR: expected function, found struct `G`
55
}

src/test/ui/issues/issue-20714.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
error[E0618]: expected function, found `G`
1+
error[E0618]: expected function, found struct `G`
22
--> $DIR/issue-20714.rs:4:13
33
|
44
LL | struct G;
5-
| -------- `G` defined here
5+
| -------- struct `G` defined here
66
...
77
LL | let g = G();
88
| ^--
99
| |
1010
| call expression requires function
11+
|
12+
help: `G` is a unit struct, and does not take parentheses to be constructed
13+
|
14+
LL - let g = G();
15+
LL + let g = G;
16+
|
1117

1218
error: aborting due to previous error
1319

src/test/ui/issues/issue-21701.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Bar;
77

88
pub fn some_func() {
99
let f = Bar();
10-
//~^ ERROR: expected function, found `Bar`
10+
//~^ ERROR: expected function, found struct `Bar`
1111
}
1212

1313
fn main() {

src/test/ui/issues/issue-21701.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ LL | let y = t();
88
| |
99
| call expression requires function
1010

11-
error[E0618]: expected function, found `Bar`
11+
error[E0618]: expected function, found struct `Bar`
1212
--> $DIR/issue-21701.rs:9:13
1313
|
1414
LL | struct Bar;
15-
| ---------- `Bar` defined here
15+
| ---------- struct `Bar` defined here
1616
...
1717
LL | let f = Bar();
1818
| ^^^--
1919
| |
2020
| call expression requires function
21+
|
22+
help: `Bar` is a unit struct, and does not take parentheses to be constructed
23+
|
24+
LL - let f = Bar();
25+
LL + let f = Bar;
26+
|
2127

2228
error: aborting due to 2 previous errors
2329

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ error[E0618]: expected function, found enum variant `Z::Unit`
336336
--> $DIR/privacy-enum-ctor.rs:31:17
337337
|
338338
LL | Unit,
339-
| ---- `Z::Unit` defined here
339+
| ---- enum variant `Z::Unit` defined here
340340
...
341341
LL | let _ = Z::Unit();
342342
| ^^^^^^^--
343343
| |
344344
| call expression requires function
345345
|
346-
help: `Z::Unit` is a unit variant, you need to write it without the parentheses
346+
help: `Z::Unit` is a unit enum variant, and does not take parentheses to be constructed
347347
|
348348
LL - let _ = Z::Unit();
349349
LL + let _ = Z::Unit;
@@ -371,14 +371,14 @@ error[E0618]: expected function, found enum variant `m::E::Unit`
371371
--> $DIR/privacy-enum-ctor.rs:47:16
372372
|
373373
LL | Unit,
374-
| ---- `m::E::Unit` defined here
374+
| ---- enum variant `m::E::Unit` defined here
375375
...
376376
LL | let _: E = m::E::Unit();
377377
| ^^^^^^^^^^--
378378
| |
379379
| call expression requires function
380380
|
381-
help: `m::E::Unit` is a unit variant, you need to write it without the parentheses
381+
help: `m::E::Unit` is a unit enum variant, and does not take parentheses to be constructed
382382
|
383383
LL - let _: E = m::E::Unit();
384384
LL + let _: E = m::E::Unit;
@@ -406,14 +406,14 @@ error[E0618]: expected function, found enum variant `E::Unit`
406406
--> $DIR/privacy-enum-ctor.rs:55:16
407407
|
408408
LL | Unit,
409-
| ---- `E::Unit` defined here
409+
| ---- enum variant `E::Unit` defined here
410410
...
411411
LL | let _: E = E::Unit();
412412
| ^^^^^^^--
413413
| |
414414
| call expression requires function
415415
|
416-
help: `E::Unit` is a unit variant, you need to write it without the parentheses
416+
help: `E::Unit` is a unit enum variant, and does not take parentheses to be constructed
417417
|
418418
LL - let _: E = E::Unit();
419419
LL + let _: E = E::Unit;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn fmt(it: &(std::cell::Cell<Option<impl FnOnce()>>,)) {
2+
(it.0.take())()
3+
//~^ ERROR expected function
4+
}
5+
6+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0618]: expected function, found `Option<impl FnOnce()>`
2+
--> $DIR/issue-99240.rs:2:5
3+
|
4+
LL | (it.0.take())()
5+
| ^^^^^^^^^^^^^--
6+
| |
7+
| call expression requires function
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0618`.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ error[E0618]: expected function, found enum variant `Alias::Unit`
2020
--> $DIR/incorrect-variant-form-through-alias-caught.rs:15:5
2121
|
2222
LL | enum Enum { Braced {}, Unit, Tuple() }
23-
| ---- `Alias::Unit` defined here
23+
| ---- enum variant `Alias::Unit` defined here
2424
...
2525
LL | Alias::Unit();
2626
| ^^^^^^^^^^^--
2727
| |
2828
| call expression requires function
2929
|
30-
help: `Alias::Unit` is a unit variant, you need to write it without the parentheses
30+
help: `Alias::Unit` is a unit enum variant, and does not take parentheses to be constructed
3131
|
3232
LL - Alias::Unit();
3333
LL + Alias::Unit;

0 commit comments

Comments
 (0)