Skip to content

Commit b1ddd57

Browse files
committed
Add check before suggest removing parens
1 parent b08dd92 commit b1ddd57

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
599599
= self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
600600
// Only suggest removing parens if there are no arguments
601601
&& arg_exprs.is_empty()
602+
&& call_expr.span.contains(callee_expr.span)
602603
{
603604
let descr = match kind {
604605
def::CtorOf::Struct => "struct",

tests/ui/suggestions/issue-114701.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enum Enum<T> { , SVariant { v: T }, UVariant } //~ ERROR expected identifier, found `,`
2+
3+
macro_rules! is_variant {
4+
(TSVariant, ) => (!);
5+
(SVariant, ) => (!);
6+
(UVariant, $expr:expr) => (is_variant!(@check UVariant, {}, $expr));
7+
(@check $variant:ident, $matcher:tt, $expr:expr) => (
8+
assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, //~ ERROR this `if` expression
9+
);
10+
);
11+
}
12+
13+
fn main() {
14+
is_variant!(UVariant, Enum::<()>::UVariant); //~ ERROR expected function
15+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error: expected identifier, found `,`
2+
--> $DIR/issue-114701.rs:1:16
3+
|
4+
LL | enum Enum<T> { , SVariant { v: T }, UVariant }
5+
| ^
6+
| |
7+
| expected identifier
8+
| help: remove this comma
9+
10+
error: this `if` expression is missing a block after the condition
11+
--> $DIR/issue-114701.rs:8:17
12+
|
13+
LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false },
14+
| ^^
15+
...
16+
LL | is_variant!(UVariant, Enum::<()>::UVariant);
17+
| ------------------------------------------- in this macro invocation
18+
|
19+
help: add a block here
20+
--> $DIR/issue-114701.rs:8:64
21+
|
22+
LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false },
23+
| ^
24+
...
25+
LL | is_variant!(UVariant, Enum::<()>::UVariant);
26+
| ------------------------------------------- in this macro invocation
27+
= note: this error originates in the macro `is_variant` (in Nightly builds, run with -Z macro-backtrace for more info)
28+
help: remove the `if` if you meant to write a `let...else` statement
29+
|
30+
LL - assert!(if let Enum::$variant::<()> $matcher = $expr () else { false },
31+
LL + assert!(let Enum::$variant::<()> $matcher = $expr () else { false },
32+
|
33+
34+
error[E0618]: expected function, found `Enum<()>`
35+
--> $DIR/issue-114701.rs:14:27
36+
|
37+
LL | enum Enum<T> { , SVariant { v: T }, UVariant }
38+
| -------- `Enum::UVariant` defined here
39+
...
40+
LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false },
41+
| -------- call expression requires function
42+
...
43+
LL | is_variant!(UVariant, Enum::<()>::UVariant);
44+
| ^^^^^^^^^^^^^^^^^^^^
45+
46+
error: aborting due to 3 previous errors
47+
48+
For more information about this error, try `rustc --explain E0618`.

0 commit comments

Comments
 (0)