mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Suggest let for possible binding with ty
This commit is contained in:
parent
ad6b20bf52
commit
0bb43c63c3
@ -140,7 +140,7 @@ pub fn lint_diagnostic_derive(s: Structure<'_>) -> TokenStream {
|
||||
/// ```fluent
|
||||
/// parser_expected_identifier = expected identifier
|
||||
///
|
||||
/// parser_expected_identifier-found = expected identifier, found {$found}
|
||||
/// parser_expected_identifier_found = expected identifier, found {$found}
|
||||
///
|
||||
/// parser_raw_identifier = escape `{$ident}` to use it as an identifier
|
||||
/// ```
|
||||
|
@ -399,6 +399,23 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// we suggest add the missing `let` before the identifier
|
||||
// `a: Ty = 1` -> `let a: Ty = 1`
|
||||
if self.token == token::Colon {
|
||||
let prev_span = self.prev_token.span.shrink_to_lo();
|
||||
let snapshot = self.create_snapshot_for_diagnostic();
|
||||
self.bump();
|
||||
let res = self.parse_ty();
|
||||
if res.is_ok() && self.token == token::Eq {
|
||||
err.span_suggestion_verbose(
|
||||
prev_span,
|
||||
"you might have meant to introduce a new binding",
|
||||
"let ".to_string(),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
self.restore_snapshot(snapshot);
|
||||
}
|
||||
|
||||
if let Some(recovered_ident) = recovered_ident && recover {
|
||||
err.emit();
|
||||
|
@ -555,7 +555,6 @@ impl<'a> Parser<'a> {
|
||||
if self.token == token::Colon {
|
||||
// if next token is following a colon, it's likely a path
|
||||
// and we can suggest a path separator
|
||||
let ident_span = self.prev_token.span;
|
||||
self.bump();
|
||||
if self.token.span.lo() == self.prev_token.span.hi() {
|
||||
err.span_suggestion_verbose(
|
||||
@ -565,14 +564,6 @@ impl<'a> Parser<'a> {
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
if self.look_ahead(1, |token| token == &token::Eq) {
|
||||
err.span_suggestion_verbose(
|
||||
ident_span.shrink_to_lo(),
|
||||
"you might have meant to introduce a new binding",
|
||||
"let ",
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
if self.sess.unstable_features.is_nightly_build() {
|
||||
// FIXME(Nilstrieb): Remove this again after a few months.
|
||||
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
|
||||
|
11
tests/ui/suggestions/type-ascription-instead-of-let.fixed
Normal file
11
tests/ui/suggestions/type-ascription-instead-of-let.fixed
Normal file
@ -0,0 +1,11 @@
|
||||
// run-rustfix
|
||||
|
||||
fn fun(x: i32) -> i32 { x }
|
||||
|
||||
fn main() {
|
||||
let _closure_annotated = |value: i32| -> i32 {
|
||||
let temp: i32 = fun(5i32);
|
||||
//~^ ERROR expected identifier, found `:`
|
||||
temp + value + 1
|
||||
};
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
// run-rustfix
|
||||
|
||||
fn fun(x: i32) -> i32 { x }
|
||||
|
||||
fn main() {
|
||||
let closure_annotated = |value: i32| -> i32 {
|
||||
let _closure_annotated = |value: i32| -> i32 {
|
||||
temp: i32 = fun(5i32);
|
||||
//~^ ERROR expected identifier, found `:`
|
||||
temp + value + 1
|
||||
|
@ -1,8 +1,13 @@
|
||||
error: expected identifier, found `:`
|
||||
--> $DIR/type-ascription-instead-of-let.rs:5:13
|
||||
--> $DIR/type-ascription-instead-of-let.rs:7:13
|
||||
|
|
||||
LL | temp: i32 = fun(5i32);
|
||||
| ^ expected identifier
|
||||
|
|
||||
help: you might have meant to introduce a new binding
|
||||
|
|
||||
LL | let temp: i32 = fun(5i32);
|
||||
| +++
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
5
tests/ui/type/missing-let-in-binding-2.fixed
Normal file
5
tests/ui/type/missing-let-in-binding-2.fixed
Normal file
@ -0,0 +1,5 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
let _v: Vec<i32> = vec![1, 2, 3]; //~ ERROR expected identifier, found `:`
|
||||
}
|
5
tests/ui/type/missing-let-in-binding-2.rs
Normal file
5
tests/ui/type/missing-let-in-binding-2.rs
Normal file
@ -0,0 +1,5 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
_v: Vec<i32> = vec![1, 2, 3]; //~ ERROR expected identifier, found `:`
|
||||
}
|
13
tests/ui/type/missing-let-in-binding-2.stderr
Normal file
13
tests/ui/type/missing-let-in-binding-2.stderr
Normal file
@ -0,0 +1,13 @@
|
||||
error: expected identifier, found `:`
|
||||
--> $DIR/missing-let-in-binding-2.rs:4:7
|
||||
|
|
||||
LL | _v: Vec<i32> = vec![1, 2, 3];
|
||||
| ^ expected identifier
|
||||
|
|
||||
help: you might have meant to introduce a new binding
|
||||
|
|
||||
LL | let _v: Vec<i32> = vec![1, 2, 3];
|
||||
| +++
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue
Block a user