diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 26ca018685e..dcb150f18aa 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1325,13 +1325,17 @@ impl<'a> Parser<'a> { kind: IncDecRecovery, (pre_span, post_span): (Span, Span), ) { + let mut msg = format!("use `{}= 1` instead", kind.op.chr()); + if base_src.trim() == "tmp" { + msg.push_str(" (rename `tmp` so it doesn't conflict with your variable)"); + } err.multipart_suggestion( - &format!("use `{}= 1` instead", kind.op.chr()), + &msg, vec![ (pre_span, "{ let tmp = ".to_string()), (post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())), ], - Applicability::MachineApplicable, + Applicability::HasPlaceholders, ); } diff --git a/src/test/ui/parser/increment-autofix.rs b/src/test/ui/parser/increment-autofix.rs index f31031fed3a..909c8f8c371 100644 --- a/src/test/ui/parser/increment-autofix.rs +++ b/src/test/ui/parser/increment-autofix.rs @@ -14,6 +14,20 @@ fn post_while() { } } +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 @@ -31,6 +45,8 @@ fn pre_while() { fn main() { post_regular(); post_while(); + post_regular_tmp(); + post_while_tmp(); pre_regular(); pre_while(); } diff --git a/src/test/ui/parser/increment-autofix.stderr b/src/test/ui/parser/increment-autofix.stderr index e5386c7bdba..8c934c9efde 100644 --- a/src/test/ui/parser/increment-autofix.stderr +++ b/src/test/ui/parser/increment-autofix.stderr @@ -30,8 +30,40 @@ 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 (rename `tmp` so it doesn't conflict with your variable) + | +LL | { let tmp = tmp; tmp += 1; tmp }; + | +++++++++++ ~~~~~~~~~~~~~~~~~ +help: or, if you don't need to use it as an expression, change it to this + | +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 (rename `tmp` so it doesn't conflict with your variable) + | +LL | while { let tmp = tmp; tmp += 1; tmp } < 5 { + | +++++++++++ ~~~~~~~~~~~~~~~~~ +help: or, if you don't need to use it as an expression, change it to this + | +LL - while tmp++ < 5 { +LL + while tmp += 1 < 5 { + | + error: Rust has no prefix increment operator - --> $DIR/increment-autofix.rs:19:5 + --> $DIR/increment-autofix.rs:33:5 | LL | ++i; | ^^ not a valid prefix operator @@ -43,7 +75,7 @@ LL + i += 1; | error: Rust has no prefix increment operator - --> $DIR/increment-autofix.rs:25:11 + --> $DIR/increment-autofix.rs:39:11 | LL | while ++i < 5 { | ^^ not a valid prefix operator @@ -53,5 +85,5 @@ help: use `+= 1` instead LL | while { i += 1; i } < 5 { | ~ +++++++++ -error: aborting due to 4 previous errors +error: aborting due to 6 previous errors diff --git a/src/test/ui/parser/increment-notfixed.rs b/src/test/ui/parser/increment-notfixed.rs index d0efe952982..3db8a4c3326 100644 --- a/src/test/ui/parser/increment-notfixed.rs +++ b/src/test/ui/parser/increment-notfixed.rs @@ -13,6 +13,16 @@ fn post_field() { println!("{}", foo.bar.qux); } +fn post_field_tmp() { + struct S { + tmp: i32 + } + let s = S { tmp: 0 }; + s.tmp++; + //~^ ERROR Rust has no postfix increment operator + println!("{}", s.tmp); +} + fn pre_field() { let foo = Foo { bar: Bar { qux: 0 } }; ++foo.bar.qux; @@ -22,5 +32,6 @@ fn pre_field() { fn main() { post_field(); + post_field_tmp(); pre_field(); } diff --git a/src/test/ui/parser/increment-notfixed.stderr b/src/test/ui/parser/increment-notfixed.stderr index 43586c4c25e..c4c83b5113b 100644 --- a/src/test/ui/parser/increment-notfixed.stderr +++ b/src/test/ui/parser/increment-notfixed.stderr @@ -14,8 +14,24 @@ LL - foo.bar.qux++; LL + foo.bar.qux += 1; | +error: Rust has no postfix increment operator + --> $DIR/increment-notfixed.rs:21:10 + | +LL | s.tmp++; + | ^^ not a valid postfix operator + | +help: use `+= 1` instead + | +LL | { let tmp = s.tmp; s.tmp += 1; tmp }; + | +++++++++++ ~~~~~~~~~~~~~~~~~~~ +help: or, if you don't need to use it as an expression, change it to this + | +LL - s.tmp++; +LL + s.tmp += 1; + | + error: Rust has no prefix increment operator - --> $DIR/increment-notfixed.rs:18:5 + --> $DIR/increment-notfixed.rs:28:5 | LL | ++foo.bar.qux; | ^^ not a valid prefix operator @@ -26,5 +42,5 @@ LL - ++foo.bar.qux; LL + foo.bar.qux += 1; | -error: aborting due to 2 previous errors +error: aborting due to 3 previous errors