Improve handling of tmp variable name conflicts

This commit is contained in:
Noah Lev 2022-02-17 14:58:46 -08:00
parent 62b8ea67b7
commit 073010d425
5 changed files with 86 additions and 7 deletions

View File

@ -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,
);
}

View File

@ -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();
}

View File

@ -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

View File

@ -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();
}

View File

@ -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