Skip to content

Commit ac40ea7

Browse files
Suggest typo fix for static lifetime
1 parent 30f168e commit ac40ea7

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

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

+30-18
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
2424
use rustc_hir::{MissingLifetimeKind, PrimTy};
2525
use rustc_middle::ty;
2626
use rustc_session::{Session, lint};
27-
use rustc_span::edit_distance::find_best_match_for_name;
27+
use rustc_span::edit_distance::{edit_distance, find_best_match_for_name};
2828
use rustc_span::edition::Edition;
2929
use rustc_span::hygiene::MacroKind;
3030
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
@@ -2874,23 +2874,35 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
28742874
)
28752875
.with_span_label(lifetime_ref.ident.span, "undeclared lifetime")
28762876
};
2877-
self.suggest_introducing_lifetime(
2878-
&mut err,
2879-
Some(lifetime_ref.ident.name.as_str()),
2880-
|err, _, span, message, suggestion, span_suggs| {
2881-
err.multipart_suggestion_with_style(
2882-
message,
2883-
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2884-
Applicability::MaybeIncorrect,
2885-
if span_suggs.is_empty() {
2886-
SuggestionStyle::ShowCode
2887-
} else {
2888-
SuggestionStyle::ShowAlways
2889-
},
2890-
);
2891-
true
2892-
},
2893-
);
2877+
2878+
// Check if this is a typo of `'static`.
2879+
if edit_distance(lifetime_ref.ident.name.as_str(), "'static", 2).is_some() {
2880+
err.span_suggestion_verbose(
2881+
lifetime_ref.ident.span,
2882+
"you may have misspelled the `'static` lifetime",
2883+
"'static",
2884+
Applicability::MachineApplicable,
2885+
);
2886+
} else {
2887+
self.suggest_introducing_lifetime(
2888+
&mut err,
2889+
Some(lifetime_ref.ident.name.as_str()),
2890+
|err, _, span, message, suggestion, span_suggs| {
2891+
err.multipart_suggestion_with_style(
2892+
message,
2893+
std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(),
2894+
Applicability::MaybeIncorrect,
2895+
if span_suggs.is_empty() {
2896+
SuggestionStyle::ShowCode
2897+
} else {
2898+
SuggestionStyle::ShowAlways
2899+
},
2900+
);
2901+
true
2902+
},
2903+
);
2904+
}
2905+
28942906
err.emit();
28952907
}
28962908

Diff for: tests/ui/lifetimes/static-typos.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn stati<T: 'stati>() {}
2+
//~^ ERROR use of undeclared lifetime name `'stati`
3+
4+
fn statoc<T: 'statoc>() {}
5+
//~^ ERROR use of undeclared lifetime name `'statoc`
6+
7+
fn main() {}

Diff for: tests/ui/lifetimes/static-typos.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0261]: use of undeclared lifetime name `'stati`
2+
--> $DIR/static-typos.rs:1:13
3+
|
4+
LL | fn stati<T: 'stati>() {}
5+
| ^^^^^^ undeclared lifetime
6+
|
7+
help: you may have misspelled the `'static` lifetime
8+
|
9+
LL | fn stati<T: 'static>() {}
10+
| +
11+
12+
error[E0261]: use of undeclared lifetime name `'statoc`
13+
--> $DIR/static-typos.rs:4:14
14+
|
15+
LL | fn statoc<T: 'statoc>() {}
16+
| ^^^^^^^ undeclared lifetime
17+
|
18+
help: you may have misspelled the `'static` lifetime
19+
|
20+
LL - fn statoc<T: 'statoc>() {}
21+
LL + fn statoc<T: 'static>() {}
22+
|
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)