From b1ddd57b5c0d306c02d06fbf8c3750f1c5a16d3c Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Sun, 13 Aug 2023 20:34:36 +0800 Subject: [PATCH 1/2] Add check before suggest removing parens --- compiler/rustc_hir_typeck/src/callee.rs | 1 + tests/ui/suggestions/issue-114701.rs | 15 ++++++++ tests/ui/suggestions/issue-114701.stderr | 48 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/ui/suggestions/issue-114701.rs create mode 100644 tests/ui/suggestions/issue-114701.stderr diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index dd79d1afc62fe..02371f85ac3b6 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -599,6 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { = self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id) // Only suggest removing parens if there are no arguments && arg_exprs.is_empty() + && call_expr.span.contains(callee_expr.span) { let descr = match kind { def::CtorOf::Struct => "struct", diff --git a/tests/ui/suggestions/issue-114701.rs b/tests/ui/suggestions/issue-114701.rs new file mode 100644 index 0000000000000..09d573f054bdc --- /dev/null +++ b/tests/ui/suggestions/issue-114701.rs @@ -0,0 +1,15 @@ +enum Enum { , SVariant { v: T }, UVariant } //~ ERROR expected identifier, found `,` + +macro_rules! is_variant { + (TSVariant, ) => (!); + (SVariant, ) => (!); + (UVariant, $expr:expr) => (is_variant!(@check UVariant, {}, $expr)); + (@check $variant:ident, $matcher:tt, $expr:expr) => ( + assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, //~ ERROR this `if` expression + ); + ); +} + +fn main() { + is_variant!(UVariant, Enum::<()>::UVariant); //~ ERROR expected function +} diff --git a/tests/ui/suggestions/issue-114701.stderr b/tests/ui/suggestions/issue-114701.stderr new file mode 100644 index 0000000000000..bccc98e060883 --- /dev/null +++ b/tests/ui/suggestions/issue-114701.stderr @@ -0,0 +1,48 @@ +error: expected identifier, found `,` + --> $DIR/issue-114701.rs:1:16 + | +LL | enum Enum { , SVariant { v: T }, UVariant } + | ^ + | | + | expected identifier + | help: remove this comma + +error: this `if` expression is missing a block after the condition + --> $DIR/issue-114701.rs:8:17 + | +LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, + | ^^ +... +LL | is_variant!(UVariant, Enum::<()>::UVariant); + | ------------------------------------------- in this macro invocation + | +help: add a block here + --> $DIR/issue-114701.rs:8:64 + | +LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, + | ^ +... +LL | is_variant!(UVariant, Enum::<()>::UVariant); + | ------------------------------------------- in this macro invocation + = note: this error originates in the macro `is_variant` (in Nightly builds, run with -Z macro-backtrace for more info) +help: remove the `if` if you meant to write a `let...else` statement + | +LL - assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, +LL + assert!(let Enum::$variant::<()> $matcher = $expr () else { false }, + | + +error[E0618]: expected function, found `Enum<()>` + --> $DIR/issue-114701.rs:14:27 + | +LL | enum Enum { , SVariant { v: T }, UVariant } + | -------- `Enum::UVariant` defined here +... +LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, + | -------- call expression requires function +... +LL | is_variant!(UVariant, Enum::<()>::UVariant); + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0618`. From 860fc246088c349bd95ce2da3e8637bb0c590105 Mon Sep 17 00:00:00 2001 From: r0cky Date: Tue, 15 Aug 2023 10:58:33 +0000 Subject: [PATCH 2/2] Remove extra errors --- tests/ui/suggestions/issue-114701.rs | 4 +-- tests/ui/suggestions/issue-114701.stderr | 41 +++--------------------- 2 files changed, 6 insertions(+), 39 deletions(-) diff --git a/tests/ui/suggestions/issue-114701.rs b/tests/ui/suggestions/issue-114701.rs index 09d573f054bdc..81d7803ec8ce0 100644 --- a/tests/ui/suggestions/issue-114701.rs +++ b/tests/ui/suggestions/issue-114701.rs @@ -1,11 +1,11 @@ -enum Enum { , SVariant { v: T }, UVariant } //~ ERROR expected identifier, found `,` +enum Enum { SVariant { v: T }, UVariant } macro_rules! is_variant { (TSVariant, ) => (!); (SVariant, ) => (!); (UVariant, $expr:expr) => (is_variant!(@check UVariant, {}, $expr)); (@check $variant:ident, $matcher:tt, $expr:expr) => ( - assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, //~ ERROR this `if` expression + assert!(if let Enum::$variant::<()> $matcher = $expr () { true } else { false }, ); ); } diff --git a/tests/ui/suggestions/issue-114701.stderr b/tests/ui/suggestions/issue-114701.stderr index bccc98e060883..67462a09c78da 100644 --- a/tests/ui/suggestions/issue-114701.stderr +++ b/tests/ui/suggestions/issue-114701.stderr @@ -1,48 +1,15 @@ -error: expected identifier, found `,` - --> $DIR/issue-114701.rs:1:16 - | -LL | enum Enum { , SVariant { v: T }, UVariant } - | ^ - | | - | expected identifier - | help: remove this comma - -error: this `if` expression is missing a block after the condition - --> $DIR/issue-114701.rs:8:17 - | -LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, - | ^^ -... -LL | is_variant!(UVariant, Enum::<()>::UVariant); - | ------------------------------------------- in this macro invocation - | -help: add a block here - --> $DIR/issue-114701.rs:8:64 - | -LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, - | ^ -... -LL | is_variant!(UVariant, Enum::<()>::UVariant); - | ------------------------------------------- in this macro invocation - = note: this error originates in the macro `is_variant` (in Nightly builds, run with -Z macro-backtrace for more info) -help: remove the `if` if you meant to write a `let...else` statement - | -LL - assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, -LL + assert!(let Enum::$variant::<()> $matcher = $expr () else { false }, - | - error[E0618]: expected function, found `Enum<()>` --> $DIR/issue-114701.rs:14:27 | -LL | enum Enum { , SVariant { v: T }, UVariant } - | -------- `Enum::UVariant` defined here +LL | enum Enum { SVariant { v: T }, UVariant } + | -------- `Enum::UVariant` defined here ... -LL | assert!(if let Enum::$variant::<()> $matcher = $expr () else { false }, +LL | assert!(if let Enum::$variant::<()> $matcher = $expr () { true } else { false }, | -------- call expression requires function ... LL | is_variant!(UVariant, Enum::<()>::UVariant); | ^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 3 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0618`.