Skip to content

Commit

Permalink
Auto merge of #117934 - Young-Flash:dev, r=petrochenkov
Browse files Browse the repository at this point in the history
feat: make `let_binding_suggestion` more reasonable

This is my first PR for rustc, which trying to fix #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.
  • Loading branch information
bors committed Nov 24, 2023
2 parents beebcde + c710db8 commit 4fd68eb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
15 changes: 12 additions & 3 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2162,13 +2162,22 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
fn let_binding_suggestion(&mut self, err: &mut Diagnostic, ident_span: Span) -> bool {
if let Some(Expr { kind: ExprKind::Assign(lhs, ..), .. }) =
self.diagnostic_metadata.in_assignment
&& let ast::ExprKind::Path(None, _) = lhs.kind
&& let ast::ExprKind::Path(None, ref path) = lhs.kind
{
if !ident_span.from_expansion() {
let (span, text) = match path.segments.first() {
Some(seg) if let Some(name) = seg.ident.as_str().strip_prefix("let") => {
// a special case for #117894
let name = name.strip_prefix("_").unwrap_or(name);
(ident_span, format!("let {name}"))
}
_ => (ident_span.shrink_to_lo(), "let ".to_string()),
};

err.span_suggestion_verbose(
ident_span.shrink_to_lo(),
span,
"you might have meant to introduce a new binding",
"let ".to_string(),
text,
Applicability::MaybeIncorrect,
);
return true;
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/suggestions/suggest-let-for-assignment.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ fn main() {
let x = "x"; //~ ERROR cannot find value `x` in this scope
println!("x: {}", x); //~ ERROR cannot find value `x` in this scope

let some_variable = 6; //~ cannot find value `let_some_variable` in this scope
println!("some_variable: {}", some_variable); //~ ERROR cannot find value `some_variable` in this scope

let other_variable = 6; //~ cannot find value `letother_variable` in this scope
println!("other_variable: {}", other_variable); //~ ERROR cannot find value `other_variable` in this scope

if x == "x" {
//~^ ERROR cannot find value `x` in this scope
println!("x is 1");
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/suggestions/suggest-let-for-assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ fn main() {
x = "x"; //~ ERROR cannot find value `x` in this scope
println!("x: {}", x); //~ ERROR cannot find value `x` in this scope

let_some_variable = 6; //~ cannot find value `let_some_variable` in this scope
println!("some_variable: {}", some_variable); //~ ERROR cannot find value `some_variable` in this scope

letother_variable = 6; //~ cannot find value `letother_variable` in this scope
println!("other_variable: {}", other_variable); //~ ERROR cannot find value `other_variable` in this scope

if x == "x" {
//~^ ERROR cannot find value `x` in this scope
println!("x is 1");
Expand Down
42 changes: 38 additions & 4 deletions tests/ui/suggestions/suggest-let-for-assignment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,48 @@ error[E0425]: cannot find value `x` in this scope
LL | println!("x: {}", x);
| ^ not found in this scope

error[E0425]: cannot find value `let_some_variable` in this scope
--> $DIR/suggest-let-for-assignment.rs:10:5
|
LL | let_some_variable = 6;
| ^^^^^^^^^^^^^^^^^
|
help: you might have meant to introduce a new binding
|
LL | let some_variable = 6;
| ~~~~~~~~~~~~~~~~~

error[E0425]: cannot find value `some_variable` in this scope
--> $DIR/suggest-let-for-assignment.rs:11:35
|
LL | println!("some_variable: {}", some_variable);
| ^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `letother_variable` in this scope
--> $DIR/suggest-let-for-assignment.rs:13:5
|
LL | letother_variable = 6;
| ^^^^^^^^^^^^^^^^^
|
help: you might have meant to introduce a new binding
|
LL | let other_variable = 6;
| ~~~~~~~~~~~~~~~~~~

error[E0425]: cannot find value `other_variable` in this scope
--> $DIR/suggest-let-for-assignment.rs:14:36
|
LL | println!("other_variable: {}", other_variable);
| ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/suggest-let-for-assignment.rs:10:8
--> $DIR/suggest-let-for-assignment.rs:16:8
|
LL | if x == "x" {
| ^ not found in this scope

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

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

error: aborting due to 7 previous errors
error: aborting due to 11 previous errors

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

0 comments on commit 4fd68eb

Please sign in to comment.