Skip to content

Commit a8d6de5

Browse files
committed
Introduce the mismatched_elided_lifetime_styles lint
1 parent 9b0eda0 commit a8d6de5

File tree

7 files changed

+1210
-3
lines changed

7 files changed

+1210
-3
lines changed

compiler/rustc_hir/src/hir.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl ParamName {
8989
}
9090
}
9191

92-
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
92+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
9393
pub enum LifetimeName {
9494
/// User-given names or fresh (synthetic) names.
9595
Param(LocalDefId),
@@ -158,11 +158,23 @@ impl Lifetime {
158158
}
159159

160160
pub fn is_anonymous(&self) -> bool {
161-
self.ident.name == kw::Empty || self.ident.name == kw::UnderscoreLifetime
161+
self.is_syntactically_hidden() || self.is_syntactically_anonymous()
162+
}
163+
164+
pub fn is_syntactically_hidden(&self) -> bool {
165+
self.ident.name == kw::Empty
166+
}
167+
168+
pub fn is_syntactically_anonymous(&self) -> bool {
169+
self.ident.name == kw::UnderscoreLifetime
170+
}
171+
172+
pub fn is_static(&self) -> bool {
173+
self.res == LifetimeName::Static
162174
}
163175

164176
pub fn suggestion_position(&self, is_ref: bool) -> (LifetimeSuggestionPosition, Span) {
165-
if self.ident.name == kw::Empty {
177+
if self.is_syntactically_hidden() {
166178
if is_ref {
167179
(LifetimeSuggestionPosition::Ampersand, self.ident.span)
168180
} else if self.ident.span.is_empty() {

compiler/rustc_lint/messages.ftl

+19
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,25 @@ lint_metavariable_still_repeating = variable `{$name}` is still repeating at thi
504504
505505
lint_metavariable_wrong_operator = meta-variable repeats with different Kleene operator
506506
507+
lint_mismatched_elided_lifetime_styles =
508+
lifetime flowing from input to output with different syntax
509+
.label_mismatched_elided_lifetime_styles_inputs =
510+
{$n_inputs ->
511+
[one] this lifetime flows
512+
*[other] these lifetimes flow
513+
} to the output
514+
.label_mismatched_elided_lifetime_styles_outputs =
515+
the elided {$n_outputs ->
516+
[one] lifetime gets
517+
*[other] lifetimes get
518+
} resolved as `{$lifetime_name2}`
519+
520+
lint_mismatched_elided_lifetime_styles_suggestion_hidden =
521+
one option is to consistently hide the lifetime
522+
523+
lint_mismatched_elided_lifetime_styles_suggestion_named =
524+
one option is to consistently use `{$lifetime_name}`
525+
507526
lint_missing_fragment_specifier = missing fragment specifier
508527
509528
lint_missing_unsafe_on_extern = extern blocks should be unsafe

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod invalid_from_utf8;
5757
mod late;
5858
mod let_underscore;
5959
mod levels;
60+
mod lifetime_style;
6061
mod lints;
6162
mod macro_expr_fragment_specifier_2024_migration;
6263
mod map_unit_fn;
@@ -96,6 +97,7 @@ use impl_trait_overcaptures::ImplTraitOvercaptures;
9697
use internal::*;
9798
use invalid_from_utf8::*;
9899
use let_underscore::*;
100+
use lifetime_style::*;
99101
use macro_expr_fragment_specifier_2024_migration::*;
100102
use map_unit_fn::*;
101103
use multiple_supertrait_upcastable::*;
@@ -244,6 +246,7 @@ late_lint_methods!(
244246
IfLetRescope: IfLetRescope::default(),
245247
StaticMutRefs: StaticMutRefs,
246248
UnqualifiedLocalImports: UnqualifiedLocalImports,
249+
LifetimeStyle: LifetimeStyle,
247250
]
248251
]
249252
);

0 commit comments

Comments
 (0)