1
Fork 0

Change temporary variable name if it would conflict

This commit is contained in:
Noah Lev 2022-02-18 15:32:46 -08:00
parent 95960b7d54
commit ef74796178
2 changed files with 10 additions and 13 deletions

View file

@ -1337,15 +1337,12 @@ impl<'a> Parser<'a> {
kind: IncDecRecovery, kind: IncDecRecovery,
(pre_span, post_span): (Span, Span), (pre_span, post_span): (Span, Span),
) { ) {
let mut msg = format!("use `{}= 1` instead", kind.op.chr()); let tmp_var = if base_src.trim() == "tmp" { "tmp_" } else { "tmp" };
if base_src.trim() == "tmp" {
msg.push_str(" (rename `tmp` so it doesn't conflict with your variable)");
}
err.multipart_suggestion( err.multipart_suggestion(
&msg, &format!("use `{}= 1` instead", kind.op.chr()),
vec![ vec![
(pre_span, "{ let tmp = ".to_string()), (pre_span, format!("{{ let {} = ", tmp_var)),
(post_span, format!("; {} {}= 1; tmp }}", base_src, kind.op.chr())), (post_span, format!("; {} {}= 1; {} }}", base_src, kind.op.chr(), tmp_var)),
], ],
Applicability::HasPlaceholders, Applicability::HasPlaceholders,
); );

View file

@ -36,10 +36,10 @@ error: Rust has no postfix increment operator
LL | tmp++; LL | tmp++;
| ^^ not a valid postfix operator | ^^ 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 help: or, if you don't need to use it as an expression, change it to this
| |
LL - tmp++; LL - tmp++;
@ -52,10 +52,10 @@ error: Rust has no postfix increment operator
LL | while tmp++ < 5 { LL | while tmp++ < 5 {
| ^^ not a valid postfix operator | ^^ 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 help: or, if you don't need to use it as an expression, change it to this
| |
LL - while tmp++ < 5 { LL - while tmp++ < 5 {