Skip to content

Commit 10f1431

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

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,10 +1969,24 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
19691969
&& let ast::ExprKind::Path(None, _) = 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) => {
1974+
// 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())
1984+
};
1985+
19721986
err.span_suggestion_verbose(
1973-
ident_span.shrink_to_lo(),
1987+
span,
19741988
"you might have meant to introduce a new binding",
1975-
"let ".to_string(),
1989+
text,
19761990
Applicability::MaybeIncorrect,
19771991
);
19781992
return true;

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

Lines changed: 6 additions & 6 deletions
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.fixed

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 41 additions & 7 deletions
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,37 +24,71 @@ 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
3131
|
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
| ^
4680
|
4781
help: you might have meant to introduce a new binding
4882
|
4983
LL | let y = 1 + 2;
50-
| +++
84+
| ~~~~~
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)