Skip to content

Commit 5b2aff2

Browse files
committed
refactor: use path to get ident name
1 parent 10f1431 commit 5b2aff2

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -1966,21 +1966,16 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
19661966
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
19671967
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
19681968
self.diagnostic_metadata.in_assignment
1969-
&& let ast::ExprKind::Path(None, _) = lhs.kind
1969+
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
19701970
{
19711971
if !ident_span.from_expansion() {
1972-
let (span, text) = match self.r.tcx.sess.source_map().span_to_snippet(ident_span) {
1973-
Ok(var_name) => {
1972+
let (span, text) = match path.segments.first().map(|it| it.ident.as_str()) {
1973+
Some(name) if let Some(name) = name.strip_prefix("let") => {
19741974
// a special case for #117894
1975-
let text = var_name.strip_prefix("let").and_then(|rest| {
1976-
match rest.starts_with("_") {
1977-
true => rest.strip_prefix("_").map(String::from),
1978-
false => Some(rest.to_string())
1979-
}
1980-
}).unwrap_or(var_name);
1981-
(ident_span, format!("let {}", text))
1982-
},
1983-
_ => (ident_span.shrink_to_lo(), "let ".to_string())
1975+
let name = name.strip_prefix("_").unwrap_or(name);
1976+
(ident_span, format!("let {name}"))
1977+
}
1978+
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
19841979
};
19851980

19861981
err.span_suggestion_verbose(

tests/ui/suggestions/issue-104086-suggest-let.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | x = x = x;
77
help: you might have meant to introduce a new binding
88
|
99
LL | let x = x = x;
10-
| ~~~~~
10+
| +++
1111

1212
error[E0425]: cannot find value `x` in this scope
1313
--> $DIR/issue-104086-suggest-let.rs:2:9
@@ -30,7 +30,7 @@ LL | x = y = y = y;
3030
help: you might have meant to introduce a new binding
3131
|
3232
LL | let x = y = y = y;
33-
| ~~~~~
33+
| +++
3434

3535
error[E0425]: cannot find value `y` in this scope
3636
--> $DIR/issue-104086-suggest-let.rs:7:9
@@ -59,7 +59,7 @@ LL | x = y = y;
5959
help: you might have meant to introduce a new binding
6060
|
6161
LL | let x = y = y;
62-
| ~~~~~
62+
| +++
6363

6464
error[E0425]: cannot find value `y` in this scope
6565
--> $DIR/issue-104086-suggest-let.rs:13:9
@@ -82,7 +82,7 @@ LL | x = x = y;
8282
help: you might have meant to introduce a new binding
8383
|
8484
LL | let x = x = y;
85-
| ~~~~~
85+
| +++
8686

8787
error[E0425]: cannot find value `x` in this scope
8888
--> $DIR/issue-104086-suggest-let.rs:18:9
@@ -105,7 +105,7 @@ LL | x = x; // will suggest add `let`
105105
help: you might have meant to introduce a new binding
106106
|
107107
LL | let x = x; // will suggest add `let`
108-
| ~~~~~
108+
| +++
109109

110110
error[E0425]: cannot find value `x` in this scope
111111
--> $DIR/issue-104086-suggest-let.rs:23:9
@@ -122,7 +122,7 @@ LL | x = y // will suggest add `let`
122122
help: you might have meant to introduce a new binding
123123
|
124124
LL | let x = y // will suggest add `let`
125-
| ~~~~~
125+
| +++
126126

127127
error[E0425]: cannot find value `y` in this scope
128128
--> $DIR/issue-104086-suggest-let.rs:27:9

tests/ui/suggestions/suggest-let-for-assignment.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | demo = 1;
77
help: you might have meant to introduce a new binding
88
|
99
LL | let demo = 1;
10-
| ~~~~~~~~
10+
| +++
1111

1212
error[E0425]: cannot find value `demo` in this scope
1313
--> $DIR/suggest-let-for-assignment.rs:5:10
@@ -24,7 +24,7 @@ LL | x = "x";
2424
help: you might have meant to introduce a new binding
2525
|
2626
LL | let x = "x";
27-
| ~~~~~
27+
| +++
2828

2929
error[E0425]: cannot find value `x` in this scope
3030
--> $DIR/suggest-let-for-assignment.rs:8:23
@@ -81,7 +81,7 @@ LL | y = 1 + 2;
8181
help: you might have meant to introduce a new binding
8282
|
8383
LL | let y = 1 + 2;
84-
| ~~~~~
84+
| +++
8585

8686
error[E0425]: cannot find value `y` in this scope
8787
--> $DIR/suggest-let-for-assignment.rs:22:23

0 commit comments

Comments
 (0)