From 86220d6e517225d13c145fd12acfc96f78358170 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Wed, 23 Mar 2022 17:26:59 -0700 Subject: [PATCH] Fix rustfix panic on test `run-rustfix` applies all suggestions regardless of their Applicability. There's a flag, `rustfix-only-machine-applicable`, that does what it says, but then the produced `.fixed` file would have invalid code from the suggestions that weren't applied. So, I moved the cases of postfix increment, in which case multiple suggestions are given, to the `-notfixed` test, which does not run rustfix. I also changed the Applicability to Unspecified since MaybeIncorrect requires that the code be valid, even if it's incorrect. --- .../rustc_parse/src/parser/diagnostics.rs | 2 +- src/test/ui/parser/increment-autofix.fixed | 33 +++---- src/test/ui/parser/increment-autofix.rs | 49 +++-------- src/test/ui/parser/increment-autofix.stderr | 85 ++++++------------- src/test/ui/parser/increment-notfixed.rs | 34 ++++++-- src/test/ui/parser/increment-notfixed.stderr | 64 +++++++++++++- 6 files changed, 144 insertions(+), 123 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 0e0954d6ee2..bab4ac9d08a 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1326,7 +1326,7 @@ impl<'a> Parser<'a> { MultiSugg::emit_many( &mut err, "use `+= 1` instead", - Applicability::MaybeIncorrect, + Applicability::Unspecified, [sugg1, sugg2].into_iter(), ) } diff --git a/src/test/ui/parser/increment-autofix.fixed b/src/test/ui/parser/increment-autofix.fixed index ad61c4e66d2..24a5ac42ca6 100644 --- a/src/test/ui/parser/increment-autofix.fixed +++ b/src/test/ui/parser/increment-autofix.fixed @@ -1,19 +1,5 @@ // run-rustfix -fn post_regular() { - let mut i = 0; - { let tmp = i; i += 1; tmp }; //~ ERROR Rust has no postfix increment operator - println!("{}", i); -} - -fn post_while() { - let mut i = 0; - while { let tmp = i; i += 1; tmp } < 5 { - //~^ ERROR Rust has no postfix increment operator - println!("{}", i); - } -} - fn pre_regular() { let mut i = 0; i += 1; //~ ERROR Rust has no prefix increment operator @@ -28,9 +14,18 @@ fn pre_while() { } } -fn main() { - post_regular(); - post_while(); - pre_regular(); - pre_while(); +fn pre_regular_tmp() { + let mut tmp = 0; + tmp += 1; //~ ERROR Rust has no prefix increment operator + println!("{}", tmp); } + +fn pre_while_tmp() { + let mut tmp = 0; + while { tmp += 1; tmp } < 5 { + //~^ ERROR Rust has no prefix increment operator + println!("{}", tmp); + } +} + +fn main() {} diff --git a/src/test/ui/parser/increment-autofix.rs b/src/test/ui/parser/increment-autofix.rs index 909c8f8c371..b1cfd09e9ec 100644 --- a/src/test/ui/parser/increment-autofix.rs +++ b/src/test/ui/parser/increment-autofix.rs @@ -1,33 +1,5 @@ // run-rustfix -fn post_regular() { - let mut i = 0; - i++; //~ ERROR Rust has no postfix increment operator - println!("{}", i); -} - -fn post_while() { - let mut i = 0; - while i++ < 5 { - //~^ ERROR Rust has no postfix increment operator - println!("{}", i); - } -} - -fn post_regular_tmp() { - let mut tmp = 0; - tmp++; //~ ERROR Rust has no postfix increment operator - println!("{}", tmp); -} - -fn post_while_tmp() { - let mut tmp = 0; - while tmp++ < 5 { - //~^ ERROR Rust has no postfix increment operator - println!("{}", tmp); - } -} - fn pre_regular() { let mut i = 0; ++i; //~ ERROR Rust has no prefix increment operator @@ -42,11 +14,18 @@ fn pre_while() { } } -fn main() { - post_regular(); - post_while(); - post_regular_tmp(); - post_while_tmp(); - pre_regular(); - pre_while(); +fn pre_regular_tmp() { + let mut tmp = 0; + ++tmp; //~ ERROR Rust has no prefix increment operator + println!("{}", tmp); } + +fn pre_while_tmp() { + let mut tmp = 0; + while ++tmp < 5 { + //~^ ERROR Rust has no prefix increment operator + println!("{}", tmp); + } +} + +fn main() {} diff --git a/src/test/ui/parser/increment-autofix.stderr b/src/test/ui/parser/increment-autofix.stderr index 5bf4b2751de..a9dc70ad0d6 100644 --- a/src/test/ui/parser/increment-autofix.stderr +++ b/src/test/ui/parser/increment-autofix.stderr @@ -1,61 +1,5 @@ -error: Rust has no postfix increment operator - --> $DIR/increment-autofix.rs:5:6 - | -LL | i++; - | ^^ not a valid postfix operator - | -help: use `+= 1` instead - | -LL | { let tmp = i; i += 1; tmp }; - | +++++++++++ ~~~~~~~~~~~~~~~ -LL - i++; -LL + i += 1; - | - -error: Rust has no postfix increment operator - --> $DIR/increment-autofix.rs:11:12 - | -LL | while i++ < 5 { - | ^^ not a valid postfix operator - | -help: use `+= 1` instead - | -LL | while { let tmp = i; i += 1; tmp } < 5 { - | +++++++++++ ~~~~~~~~~~~~~~~ -LL - while i++ < 5 { -LL + while i += 1 < 5 { - | - -error: Rust has no postfix increment operator - --> $DIR/increment-autofix.rs:19:8 - | -LL | tmp++; - | ^^ not a valid postfix operator - | -help: use `+= 1` instead - | -LL | { let tmp_ = tmp; tmp += 1; tmp_ }; - | ++++++++++++ ~~~~~~~~~~~~~~~~~~ -LL - tmp++; -LL + tmp += 1; - | - -error: Rust has no postfix increment operator - --> $DIR/increment-autofix.rs:25:14 - | -LL | while tmp++ < 5 { - | ^^ not a valid postfix operator - | -help: use `+= 1` instead - | -LL | while { let tmp_ = tmp; tmp += 1; tmp_ } < 5 { - | ++++++++++++ ~~~~~~~~~~~~~~~~~~ -LL - while tmp++ < 5 { -LL + while tmp += 1 < 5 { - | - error: Rust has no prefix increment operator - --> $DIR/increment-autofix.rs:33:5 + --> $DIR/increment-autofix.rs:5:5 | LL | ++i; | ^^ not a valid prefix operator @@ -67,7 +11,7 @@ LL + i += 1; | error: Rust has no prefix increment operator - --> $DIR/increment-autofix.rs:39:11 + --> $DIR/increment-autofix.rs:11:11 | LL | while ++i < 5 { | ^^ not a valid prefix operator @@ -77,5 +21,28 @@ help: use `+= 1` instead LL | while { i += 1; i } < 5 { | ~ +++++++++ -error: aborting due to 6 previous errors +error: Rust has no prefix increment operator + --> $DIR/increment-autofix.rs:19:5 + | +LL | ++tmp; + | ^^ not a valid prefix operator + | +help: use `+= 1` instead + | +LL - ++tmp; +LL + tmp += 1; + | + +error: Rust has no prefix increment operator + --> $DIR/increment-autofix.rs:25:11 + | +LL | while ++tmp < 5 { + | ^^ not a valid prefix operator + | +help: use `+= 1` instead + | +LL | while { tmp += 1; tmp } < 5 { + | ~ +++++++++++ + +error: aborting due to 4 previous errors diff --git a/src/test/ui/parser/increment-notfixed.rs b/src/test/ui/parser/increment-notfixed.rs index 3db8a4c3326..543f73bf772 100644 --- a/src/test/ui/parser/increment-notfixed.rs +++ b/src/test/ui/parser/increment-notfixed.rs @@ -6,6 +6,34 @@ struct Bar { qux: i32, } +fn post_regular() { + let mut i = 0; + i++; //~ ERROR Rust has no postfix increment operator + println!("{}", i); +} + +fn post_while() { + let mut i = 0; + while i++ < 5 { + //~^ ERROR Rust has no postfix increment operator + println!("{}", i); + } +} + +fn post_regular_tmp() { + let mut tmp = 0; + tmp++; //~ ERROR Rust has no postfix increment operator + println!("{}", tmp); +} + +fn post_while_tmp() { + let mut tmp = 0; + while tmp++ < 5 { + //~^ ERROR Rust has no postfix increment operator + println!("{}", tmp); + } +} + fn post_field() { let foo = Foo { bar: Bar { qux: 0 } }; foo.bar.qux++; @@ -30,8 +58,4 @@ fn pre_field() { println!("{}", foo.bar.qux); } -fn main() { - post_field(); - post_field_tmp(); - pre_field(); -} +fn main() {} diff --git a/src/test/ui/parser/increment-notfixed.stderr b/src/test/ui/parser/increment-notfixed.stderr index 16ff42ca8b0..75fce91c6ef 100644 --- a/src/test/ui/parser/increment-notfixed.stderr +++ b/src/test/ui/parser/increment-notfixed.stderr @@ -1,5 +1,61 @@ error: Rust has no postfix increment operator - --> $DIR/increment-notfixed.rs:11:16 + --> $DIR/increment-notfixed.rs:11:6 + | +LL | i++; + | ^^ not a valid postfix operator + | +help: use `+= 1` instead + | +LL | { let tmp = i; i += 1; tmp }; + | +++++++++++ ~~~~~~~~~~~~~~~ +LL - i++; +LL + i += 1; + | + +error: Rust has no postfix increment operator + --> $DIR/increment-notfixed.rs:17:12 + | +LL | while i++ < 5 { + | ^^ not a valid postfix operator + | +help: use `+= 1` instead + | +LL | while { let tmp = i; i += 1; tmp } < 5 { + | +++++++++++ ~~~~~~~~~~~~~~~ +LL - while i++ < 5 { +LL + while i += 1 < 5 { + | + +error: Rust has no postfix increment operator + --> $DIR/increment-notfixed.rs:25:8 + | +LL | tmp++; + | ^^ not a valid postfix operator + | +help: use `+= 1` instead + | +LL | { let tmp_ = tmp; tmp += 1; tmp_ }; + | ++++++++++++ ~~~~~~~~~~~~~~~~~~ +LL - tmp++; +LL + tmp += 1; + | + +error: Rust has no postfix increment operator + --> $DIR/increment-notfixed.rs:31:14 + | +LL | while tmp++ < 5 { + | ^^ not a valid postfix operator + | +help: use `+= 1` instead + | +LL | while { let tmp_ = tmp; tmp += 1; tmp_ } < 5 { + | ++++++++++++ ~~~~~~~~~~~~~~~~~~ +LL - while tmp++ < 5 { +LL + while tmp += 1 < 5 { + | + +error: Rust has no postfix increment operator + --> $DIR/increment-notfixed.rs:39:16 | LL | foo.bar.qux++; | ^^ not a valid postfix operator @@ -13,7 +69,7 @@ LL + foo.bar.qux += 1; | error: Rust has no postfix increment operator - --> $DIR/increment-notfixed.rs:21:10 + --> $DIR/increment-notfixed.rs:49:10 | LL | s.tmp++; | ^^ not a valid postfix operator @@ -27,7 +83,7 @@ LL + s.tmp += 1; | error: Rust has no prefix increment operator - --> $DIR/increment-notfixed.rs:28:5 + --> $DIR/increment-notfixed.rs:56:5 | LL | ++foo.bar.qux; | ^^ not a valid prefix operator @@ -38,5 +94,5 @@ LL - ++foo.bar.qux; LL + foo.bar.qux += 1; | -error: aborting due to 3 previous errors +error: aborting due to 7 previous errors