Skip to content

Commit c710db8

Browse files
committed
feat: make let_binding_suggestion more reasonable
1 parent 09ac6e4 commit c710db8

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1966,13 +1966,22 @@ 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 path.segments.first() {
1973+
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
1974+
// a special case for #117894
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()),
1979+
};
1980+
19721981
err.span_suggestion_verbose(
1973-
ident_span.shrink_to_lo(),
1982+
span,
19741983
"you might have meant to introduce a new binding",
1975-
"let ".to_string(),
1984+
text,
19761985
Applicability::MaybeIncorrect,
19771986
);
19781987
return true;

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

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ fn main() {
77
let x = "x"; //~ ERROR cannot find value `x` in this scope
88
println!("x: {}", x); //~ ERROR cannot find value `x` in this scope
99

10+
let some_variable = 6; //~ cannot find value `let_some_variable` in this scope
11+
println!("some_variable: {}", some_variable); //~ ERROR cannot find value `some_variable` in this scope
12+
13+
let other_variable = 6; //~ cannot find value `letother_variable` in this scope
14+
println!("other_variable: {}", other_variable); //~ ERROR cannot find value `other_variable` in this scope
15+
1016
if x == "x" {
1117
//~^ ERROR cannot find value `x` in this scope
1218
println!("x is 1");

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

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ fn main() {
77
x = "x"; //~ ERROR cannot find value `x` in this scope
88
println!("x: {}", x); //~ ERROR cannot find value `x` in this scope
99

10+
let_some_variable = 6; //~ cannot find value `let_some_variable` in this scope
11+
println!("some_variable: {}", some_variable); //~ ERROR cannot find value `some_variable` in this scope
12+
13+
letother_variable = 6; //~ cannot find value `letother_variable` in this scope
14+
println!("other_variable: {}", other_variable); //~ ERROR cannot find value `other_variable` in this scope
15+
1016
if x == "x" {
1117
//~^ ERROR cannot find value `x` in this scope
1218
println!("x is 1");

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

+38-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,48 @@ error[E0425]: cannot find value `x` in this scope
3232
LL | println!("x: {}", x);
3333
| ^ not found in this scope
3434

35+
error[E0425]: cannot find value `let_some_variable` in this scope
36+
--> $DIR/suggest-let-for-assignment.rs:10:5
37+
|
38+
LL | let_some_variable = 6;
39+
| ^^^^^^^^^^^^^^^^^
40+
|
41+
help: you might have meant to introduce a new binding
42+
|
43+
LL | let some_variable = 6;
44+
| ~~~~~~~~~~~~~~~~~
45+
46+
error[E0425]: cannot find value `some_variable` in this scope
47+
--> $DIR/suggest-let-for-assignment.rs:11:35
48+
|
49+
LL | println!("some_variable: {}", some_variable);
50+
| ^^^^^^^^^^^^^ not found in this scope
51+
52+
error[E0425]: cannot find value `letother_variable` in this scope
53+
--> $DIR/suggest-let-for-assignment.rs:13:5
54+
|
55+
LL | letother_variable = 6;
56+
| ^^^^^^^^^^^^^^^^^
57+
|
58+
help: you might have meant to introduce a new binding
59+
|
60+
LL | let other_variable = 6;
61+
| ~~~~~~~~~~~~~~~~~~
62+
63+
error[E0425]: cannot find value `other_variable` in this scope
64+
--> $DIR/suggest-let-for-assignment.rs:14:36
65+
|
66+
LL | println!("other_variable: {}", other_variable);
67+
| ^^^^^^^^^^^^^^ not found in this scope
68+
3569
error[E0425]: cannot find value `x` in this scope
36-
--> $DIR/suggest-let-for-assignment.rs:10:8
70+
--> $DIR/suggest-let-for-assignment.rs:16:8
3771
|
3872
LL | if x == "x" {
3973
| ^ not found in this scope
4074

4175
error[E0425]: cannot find value `y` in this scope
42-
--> $DIR/suggest-let-for-assignment.rs:15:5
76+
--> $DIR/suggest-let-for-assignment.rs:21:5
4377
|
4478
LL | y = 1 + 2;
4579
| ^
@@ -50,11 +84,11 @@ LL | let y = 1 + 2;
5084
| +++
5185

5286
error[E0425]: cannot find value `y` in this scope
53-
--> $DIR/suggest-let-for-assignment.rs:16:23
87+
--> $DIR/suggest-let-for-assignment.rs:22:23
5488
|
5589
LL | println!("y: {}", y);
5690
| ^ not found in this scope
5791

58-
error: aborting due to 7 previous errors
92+
error: aborting due to 11 previous errors
5993

6094
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)