Skip to content

Commit ef74796

Browse files
committed
Change temporary variable name if it would conflict
1 parent 95960b7 commit ef74796

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1337,15 +1337,12 @@ impl<'a> Parser<'a> {
13371337
kind: IncDecRecovery,
13381338
(pre_span, post_span): (Span, Span),
13391339
) {
1340-
let mut msg = format!("use `{}= 1` instead", kind.op.chr());
1341-
if base_src.trim() == "tmp" {
1342-
msg.push_str(" (rename `tmp` so it doesn't conflict with your variable)");
1343-
}
1340+
let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" };
13441341
err.multipart_suggestion(
1345-
&msg,
1342+
&format!("use `{}= 1` instead", kind.op.chr()),
13461343
vec![
1347-
(pre_span, "{ let tmp = ".to_string()),
1348-
(post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())),
1344+
(pre_span, format!("{{ let {} = ", tmp_var)),
1345+
(post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)),
13491346
],
13501347
Applicability::HasPlaceholders,
13511348
);

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ error: Rust has no postfix increment operator
3636
LL | tmp++;
3737
| ^^ not a valid postfix operator
3838
|
39-
help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable)
39+
help: use `+= 1` instead
4040
|
41-
LL | { let tmp = tmp; tmp += 1; tmp };
42-
| +++++++++++ ~~~~~~~~~~~~~~~~~
41+
LL | { let tmp_ = tmp; tmp += 1; tmp_ };
42+
| ++++++++++++ ~~~~~~~~~~~~~~~~~~
4343
help: or, if you don't need to use it as an expression, change it to this
4444
|
4545
LL - tmp++;
@@ -52,10 +52,10 @@ error: Rust has no postfix increment operator
5252
LL | while tmp++ < 5 {
5353
| ^^ not a valid postfix operator
5454
|
55-
help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable)
55+
help: use `+= 1` instead
5656
|
57-
LL | while { let tmp = tmp; tmp += 1; tmp } < 5 {
58-
| +++++++++++ ~~~~~~~~~~~~~~~~~
57+
LL | while { let tmp_ = tmp; tmp += 1; tmp_ } < 5 {
58+
| ++++++++++++ ~~~~~~~~~~~~~~~~~~
5959
help: or, if you don't need to use it as an expression, change it to this
6060
|
6161
LL - while tmp++ < 5 {

0 commit comments

Comments
 (0)