diff options
| author | Noah Lev <camelidcamel@gmail.com> | 2022-02-18 15:32:46 -0800 |
|---|---|---|
| committer | Noah Lev <camelidcamel@gmail.com> | 2022-03-23 22:31:57 -0700 |
| commit | ef74796178edd2cf28f17109083cbd235808f88d (patch) | |
| tree | 0aae1e1fd4cc19f6e1bc6e779611a89e620d81de | |
| parent | 95960b7d54ecd05b03734cadea071cd5cc29fa35 (diff) | |
| download | rust-ef74796178edd2cf28f17109083cbd235808f88d.tar.gz rust-ef74796178edd2cf28f17109083cbd235808f88d.zip | |
Change temporary variable name if it would conflict
| -rw-r--r-- | compiler/rustc_parse/src/parser/diagnostics.rs | 11 | ||||
| -rw-r--r-- | src/test/ui/parser/increment-autofix.stderr | 12 |
2 files changed, 10 insertions, 13 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 162982ebbe2..9590f26a76f 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1337,15 +1337,12 @@ 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)"); - } + let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" }; err.multipart_suggestion( - &msg, + &format!("use `{}= 1` instead", kind.op.chr()), vec![ - (pre_span, "{ let tmp = ".to_string()), - (post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())), + (pre_span, format!("{{ let {} = ", tmp_var)), + (post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)), ], Applicability::HasPlaceholders, ); diff --git a/src/test/ui/parser/increment-autofix.stderr b/src/test/ui/parser/increment-autofix.stderr index 8c934c9efde..04fd68ed85b 100644 --- a/src/test/ui/parser/increment-autofix.stderr +++ b/src/test/ui/parser/increment-autofix.stderr @@ -36,10 +36,10 @@ error: Rust has no postfix increment operator LL | tmp++; | ^^ not a valid postfix operator | -help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable) +help: use `+= 1` instead | -LL | { let tmp = tmp; tmp += 1; tmp }; - | +++++++++++ ~~~~~~~~~~~~~~~~~ +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++; @@ -52,10 +52,10 @@ error: Rust has no postfix increment operator LL | while tmp++ < 5 { | ^^ not a valid postfix operator | -help: use `+= 1` instead (rename `tmp` so it doesn't conflict with your variable) +help: use `+= 1` instead | -LL | while { let tmp = tmp; tmp += 1; tmp } < 5 { - | +++++++++++ ~~~~~~~~~~~~~~~~~ +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 { |
