Skip to content

Commit 073010d

Browse files
committed
Improve handling of tmp variable name conflicts
1 parent 62b8ea6 commit 073010d

File tree

5 files changed

+86
-7
lines changed

5 files changed

+86
-7
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,13 +1325,17 @@ impl<'a> Parser<'a> {
13251325
kind: IncDecRecovery,
13261326
(pre_span, post_span): (Span, Span),
13271327
) {
1328+
let mut msg = format!("use `{}= 1` instead", kind.op.chr());
1329+
if base_src.trim() == "tmp" {
1330+
msg.push_str(" (rename `tmp` so it doesn't conflict with your variable)");
1331+
}
13281332
err.multipart_suggestion(
1329-
&format!("use `{}= 1` instead", kind.op.chr()),
1333+
&msg,
13301334
vec![
13311335
(pre_span, "{ let tmp = ".to_string()),
13321336
(post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())),
13331337
],
1334-
Applicability::MachineApplicable,
1338+
Applicability::HasPlaceholders,
13351339
);
13361340
}
13371341

src/test/ui/parser/increment-autofix.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ fn post_while() {
1414
}
1515
}
1616

17+
fn post_regular_tmp() {
18+
let mut tmp = 0;
19+
tmp++; //~ ERROR Rust has no postfix increment operator
20+
println!("{}", tmp);
21+
}
22+
23+
fn post_while_tmp() {
24+
let mut tmp = 0;
25+
while tmp++ < 5 {
26+
//~^ ERROR Rust has no postfix increment operator
27+
println!("{}", tmp);
28+
}
29+
}
30+
1731
fn pre_regular() {
1832
let mut i = 0;
1933
++i; //~ ERROR Rust has no prefix increment operator
@@ -31,6 +45,8 @@ fn pre_while() {
3145
fn main() {
3246
post_regular();
3347
post_while();
48+
post_regular_tmp();
49+
post_while_tmp();
3450
pre_regular();
3551
pre_while();
3652
}

src/test/ui/parser/increment-autofix.stderr

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,40 @@ LL - while i++ < 5 {
3030
LL + while i += 1 < 5 {
3131
|
3232

33+
error: Rust has no postfix increment operator
34+
--> $DIR/increment-autofix.rs:19:8
35+
|
36+
LL | tmp++;
37+
| ^^ not a valid postfix operator
38+
|
39+
help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable)
40+
|
41+
LL | { let tmp = tmp; tmp += 1; tmp };
42+
| +++++++++++ ~~~~~~~~~~~~~~~~~
43+
help: or, if you don't need to use it as an expression, change it to this
44+
|
45+
LL - tmp++;
46+
LL + tmp += 1;
47+
|
48+
49+
error: Rust has no postfix increment operator
50+
--> $DIR/increment-autofix.rs:25:14
51+
|
52+
LL | while tmp++ < 5 {
53+
| ^^ not a valid postfix operator
54+
|
55+
help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable)
56+
|
57+
LL | while { let tmp = tmp; tmp += 1; tmp } < 5 {
58+
| +++++++++++ ~~~~~~~~~~~~~~~~~
59+
help: or, if you don't need to use it as an expression, change it to this
60+
|
61+
LL - while tmp++ < 5 {
62+
LL + while tmp += 1 < 5 {
63+
|
64+
3365
error: Rust has no prefix increment operator
34-
--> $DIR/increment-autofix.rs:19:5
66+
--> $DIR/increment-autofix.rs:33:5
3567
|
3668
LL | ++i;
3769
| ^^ not a valid prefix operator
@@ -43,7 +75,7 @@ LL + i += 1;
4375
|
4476

4577
error: Rust has no prefix increment operator
46-
--> $DIR/increment-autofix.rs:25:11
78+
--> $DIR/increment-autofix.rs:39:11
4779
|
4880
LL | while ++i < 5 {
4981
| ^^ not a valid prefix operator
@@ -53,5 +85,5 @@ help: use `+= 1` instead
5385
LL | while { i += 1; i } < 5 {
5486
| ~ +++++++++
5587

56-
error: aborting due to 4 previous errors
88+
error: aborting due to 6 previous errors
5789

src/test/ui/parser/increment-notfixed.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ fn post_field() {
1313
println!("{}", foo.bar.qux);
1414
}
1515

16+
fn post_field_tmp() {
17+
struct S {
18+
tmp: i32
19+
}
20+
let s = S { tmp: 0 };
21+
s.tmp++;
22+
//~^ ERROR Rust has no postfix increment operator
23+
println!("{}", s.tmp);
24+
}
25+
1626
fn pre_field() {
1727
let foo = Foo { bar: Bar { qux: 0 } };
1828
++foo.bar.qux;
@@ -22,5 +32,6 @@ fn pre_field() {
2232

2333
fn main() {
2434
post_field();
35+
post_field_tmp();
2536
pre_field();
2637
}

src/test/ui/parser/increment-notfixed.stderr

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,24 @@ LL - foo.bar.qux++;
1414
LL + foo.bar.qux += 1;
1515
|
1616

17+
error: Rust has no postfix increment operator
18+
--> $DIR/increment-notfixed.rs:21:10
19+
|
20+
LL | s.tmp++;
21+
| ^^ not a valid postfix operator
22+
|
23+
help: use `+= 1` instead
24+
|
25+
LL | { let tmp = s.tmp; s.tmp += 1; tmp };
26+
| +++++++++++ ~~~~~~~~~~~~~~~~~~~
27+
help: or, if you don't need to use it as an expression, change it to this
28+
|
29+
LL - s.tmp++;
30+
LL + s.tmp += 1;
31+
|
32+
1733
error: Rust has no prefix increment operator
18-
--> $DIR/increment-notfixed.rs:18:5
34+
--> $DIR/increment-notfixed.rs:28:5
1935
|
2036
LL | ++foo.bar.qux;
2137
| ^^ not a valid prefix operator
@@ -26,5 +42,5 @@ LL - ++foo.bar.qux;
2642
LL + foo.bar.qux += 1;
2743
|
2844

29-
error: aborting due to 2 previous errors
45+
error: aborting due to 3 previous errors
3046

0 commit comments

Comments
 (0)