Skip to content

Commit 4fd68eb

Browse files
committed
Auto merge of rust-lang#117934 - Young-Flash:dev, r=petrochenkov
feat: make `let_binding_suggestion` more reasonable This is my first PR for rustc, which trying to fix rust-lang#117894, I am not familiar with some internal api so maybe some modification here isn't the way to go, appreciated for any review suggestion.
2 parents beebcde + c710db8 commit 4fd68eb

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -2162,13 +2162,22 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
21622162
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
21632163
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
21642164
self.diagnostic_metadata.in_assignment
2165-
&& let ast::ExprKind::Path(None, _) = lhs.kind
2165+
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
21662166
{
21672167
if !ident_span.from_expansion() {
2168+
let (span, text) = match path.segments.first() {
2169+
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
2170+
// a special case for #117894
2171+
let name = name.strip_prefix("_").unwrap_or(name);
2172+
(ident_span, format!("let {name}"))
2173+
}
2174+
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
2175+
};
2176+
21682177
err.span_suggestion_verbose(
2169-
ident_span.shrink_to_lo(),
2178+
span,
21702179
"you might have meant to introduce a new binding",
2171-
"let ".to_string(),
2180+
text,
21722181
Applicability::MaybeIncorrect,
21732182
);
21742183
return true;

Diff for: 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");

Diff for: 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");

Diff for: 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)