Skip to content

Commit cc42f38

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent 73fb568 commit cc42f38

File tree

111 files changed

+497
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+497
-667
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,7 @@ pub enum LifetimeRes {
842842
/// late resolution. Those lifetimes will be inferred by typechecking.
843843
Infer,
844844
/// `'static` lifetime.
845-
Static {
846-
/// We do not want to emit `elided_named_lifetimes`
847-
/// when we are inside of a const item or a static,
848-
/// because it would get too annoying.
849-
suppress_elision_warning: bool,
850-
},
845+
Static,
851846
/// Resolution failure.
852847
Error,
853848
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_lint/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,6 @@ lint_duplicate_macro_attribute =
253253
254254
lint_duplicate_matcher_binding = duplicate matcher binding
255255
256-
lint_elided_named_lifetime = elided lifetime has a name
257-
.label_elided = this elided lifetime gets resolved as `{$name}`
258-
.label_named = lifetime `{$name}` declared here
259-
.suggestion = consider specifying it explicitly
260-
261256
lint_enum_intrinsics_mem_discriminant =
262257
the return value of `mem::discriminant` is unspecified when called with a non-enum type
263258
.note = the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `{$ty_param}`, which is not an enum

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use rustc_errors::{
1010
use rustc_middle::middle::stability;
1111
use rustc_middle::ty::TyCtxt;
1212
use rustc_session::Session;
13-
use rustc_session::lint::{BuiltinLintDiag, ElidedLifetimeResolution};
14-
use rustc_span::{BytePos, kw};
13+
use rustc_session::lint::BuiltinLintDiag;
14+
use rustc_span::BytePos;
1515
use tracing::debug;
1616

17-
use crate::lints::{self, ElidedNamedLifetime};
17+
use crate::lints;
1818

1919
mod check_cfg;
2020

@@ -471,16 +471,5 @@ pub fn decorate_builtin_lint(
471471
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
472472
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
473473
}
474-
BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => {
475-
match resolution {
476-
ElidedLifetimeResolution::Static => {
477-
ElidedNamedLifetime { span, kind, name: kw::StaticLifetime, declaration: None }
478-
}
479-
ElidedLifetimeResolution::Param(name, declaration) => {
480-
ElidedNamedLifetime { span, kind, name, declaration: Some(declaration) }
481-
}
482-
}
483-
.decorate_lint(diag)
484-
}
485474
}
486475
}

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ fn register_builtins(store: &mut LintStore) {
356356
store.register_renamed("unused_tuple_struct_fields", "dead_code");
357357
store.register_renamed("static_mut_ref", "static_mut_refs");
358358
store.register_renamed("temporary_cstring_as_ptr", "dangling_pointers_from_temporaries");
359+
store.register_renamed("elided_named_lifetimes", "mismatched_lifetime_syntaxes");
359360

360361
// These were moved to tool lints, but rustc still sees them when compiling normally, before
361362
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use

compiler/rustc_lint/src/lifetime_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ declare_lint! {
6767
/// In certain `unsafe` code, lifetime elision combined with
6868
/// inconsistent lifetime syntax may result in unsound code.
6969
pub MISMATCHED_LIFETIME_SYNTAXES,
70-
Allow,
70+
Warn,
7171
"detects when an elided lifetime uses different syntax between arguments and return values"
7272
}
7373

compiler/rustc_lint/src/lints.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use rustc_errors::{
88
Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString, ElidedLifetimeInPathSubdiag,
99
EmissionGuarantee, LintDiagnostic, MultiSpan, Subdiagnostic, SuggestionStyle,
1010
};
11+
use rustc_hir as hir;
1112
use rustc_hir::def::Namespace;
1213
use rustc_hir::def_id::DefId;
1314
use rustc_hir::intravisit::VisitorExt;
14-
use rustc_hir::{self as hir, MissingLifetimeKind};
1515
use rustc_macros::{LintDiagnostic, Subdiagnostic};
1616
use rustc_middle::ty::inhabitedness::InhabitedPredicate;
1717
use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt};
1818
use rustc_session::Session;
1919
use rustc_session::lint::AmbiguityErrorDiag;
2020
use rustc_span::edition::Edition;
21-
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, kw, sym};
21+
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
2222

2323
use crate::builtin::{InitError, ShorthandAssocTyCollector, TypeAliasBounds};
2424
use crate::errors::{OverruledAttributeSub, RequestedLevel};
@@ -2752,58 +2752,6 @@ pub(crate) struct ElidedLifetimesInPaths {
27522752
pub subdiag: ElidedLifetimeInPathSubdiag,
27532753
}
27542754

2755-
pub(crate) struct ElidedNamedLifetime {
2756-
pub span: Span,
2757-
pub kind: MissingLifetimeKind,
2758-
pub name: Symbol,
2759-
pub declaration: Option<Span>,
2760-
}
2761-
2762-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2763-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2764-
let Self { span, kind, name, declaration } = self;
2765-
diag.primary_message(fluent::lint_elided_named_lifetime);
2766-
diag.arg("name", name);
2767-
diag.span_label(span, fluent::lint_label_elided);
2768-
if let Some(declaration) = declaration {
2769-
diag.span_label(declaration, fluent::lint_label_named);
2770-
}
2771-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2772-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2773-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2774-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2775-
if name != kw::StaticLifetime {
2776-
return;
2777-
}
2778-
match kind {
2779-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2780-
span,
2781-
fluent::lint_suggestion,
2782-
format!("{name}"),
2783-
Applicability::MachineApplicable,
2784-
),
2785-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2786-
span.shrink_to_hi(),
2787-
fluent::lint_suggestion,
2788-
format!("{name} "),
2789-
Applicability::MachineApplicable,
2790-
),
2791-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2792-
span.shrink_to_hi(),
2793-
fluent::lint_suggestion,
2794-
format!("{name}, "),
2795-
Applicability::MachineApplicable,
2796-
),
2797-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2798-
span.shrink_to_hi(),
2799-
fluent::lint_suggestion,
2800-
format!("<{name}>"),
2801-
Applicability::MachineApplicable,
2802-
),
2803-
};
2804-
}
2805-
}
2806-
28072755
#[derive(LintDiagnostic)]
28082756
#[diag(lint_invalid_crate_type_value)]
28092757
pub(crate) struct UnknownCrateTypes {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ declare_lint_pass! {
4040
DUPLICATE_MACRO_ATTRIBUTES,
4141
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4242
ELIDED_LIFETIMES_IN_PATHS,
43-
ELIDED_NAMED_LIFETIMES,
4443
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
4544
EXPORTED_PRIVATE_DEPENDENCIES,
4645
FFI_UNWIND_CALLS,
@@ -1827,38 +1826,6 @@ declare_lint! {
18271826
"hidden lifetime parameters in types are deprecated"
18281827
}
18291828

1830-
declare_lint! {
1831-
/// The `elided_named_lifetimes` lint detects when an elided
1832-
/// lifetime ends up being a named lifetime, such as `'static`
1833-
/// or some lifetime parameter `'a`.
1834-
///
1835-
/// ### Example
1836-
///
1837-
/// ```rust,compile_fail
1838-
/// #![deny(elided_named_lifetimes)]
1839-
/// struct Foo;
1840-
/// impl Foo {
1841-
/// pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
1842-
/// unsafe { &mut *(x as *mut _) }
1843-
/// }
1844-
/// }
1845-
/// ```
1846-
///
1847-
/// {{produces}}
1848-
///
1849-
/// ### Explanation
1850-
///
1851-
/// Lifetime elision is quite useful, because it frees you from having
1852-
/// to give each lifetime its own name, but sometimes it can produce
1853-
/// somewhat surprising resolutions. In safe code, it is mostly okay,
1854-
/// because the borrow checker prevents any unsoundness, so the worst
1855-
/// case scenario is you get a confusing error message in some other place.
1856-
/// But with `unsafe` code, such unexpected resolutions may lead to unsound code.
1857-
pub ELIDED_NAMED_LIFETIMES,
1858-
Warn,
1859-
"detects when an elided lifetime gets resolved to be `'static` or some named parameter"
1860-
}
1861-
18621829
declare_lint! {
18631830
/// The `bare_trait_objects` lint suggests using `dyn Trait` for trait
18641831
/// objects.

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::{
99
use rustc_error_messages::{DiagMessage, MultiSpan};
1010
use rustc_hir::def::Namespace;
1111
use rustc_hir::def_id::DefPathHash;
12-
use rustc_hir::{HashStableContext, HirId, ItemLocalId, MissingLifetimeKind};
12+
use rustc_hir::{HashStableContext, HirId, ItemLocalId};
1313
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1414
pub use rustc_span::edition::Edition;
1515
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
@@ -635,12 +635,6 @@ pub enum DeprecatedSinceKind {
635635
InVersion(String),
636636
}
637637

638-
#[derive(Debug)]
639-
pub enum ElidedLifetimeResolution {
640-
Static,
641-
Param(Symbol, Span),
642-
}
643-
644638
// This could be a closure, but then implementing derive trait
645639
// becomes hacky (and it gets allocated).
646640
#[derive(Debug)]
@@ -653,10 +647,6 @@ pub enum BuiltinLintDiag {
653647
},
654648
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
655649
ElidedLifetimesInPaths(usize, Span, bool, Span),
656-
ElidedNamedLifetimes {
657-
elided: (Span, MissingLifetimeKind),
658-
resolution: ElidedLifetimeResolution,
659-
},
660650
UnknownCrateTypes {
661651
span: Span,
662652
candidate: Option<Symbol>,

compiler/rustc_resolve/src/late.rs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17291729
if ident.name == kw::StaticLifetime {
17301730
self.record_lifetime_res(
17311731
lifetime.id,
1732-
LifetimeRes::Static { suppress_elision_warning: false },
1732+
LifetimeRes::Static,
17331733
LifetimeElisionCandidate::Named,
17341734
);
17351735
return;
@@ -1877,8 +1877,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18771877
if lifetimes_in_scope.is_empty() {
18781878
self.record_lifetime_res(
18791879
lifetime.id,
1880-
// We are inside a const item, so do not warn.
1881-
LifetimeRes::Static { suppress_elision_warning: true },
1880+
LifetimeRes::Static,
18821881
elision_candidate,
18831882
);
18841883
return;
@@ -2225,47 +2224,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22252224
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
22262225
}
22272226

2228-
match candidate {
2229-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
2230-
debug_assert_eq!(id, missing.id);
2231-
match res {
2232-
LifetimeRes::Static { suppress_elision_warning } => {
2233-
if !suppress_elision_warning {
2234-
self.r.lint_buffer.buffer_lint(
2235-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2236-
missing.id_for_lint,
2237-
missing.span,
2238-
BuiltinLintDiag::ElidedNamedLifetimes {
2239-
elided: (missing.span, missing.kind),
2240-
resolution: lint::ElidedLifetimeResolution::Static,
2241-
},
2242-
);
2243-
}
2244-
}
2245-
LifetimeRes::Param { param, binder: _ } => {
2246-
let tcx = self.r.tcx();
2247-
self.r.lint_buffer.buffer_lint(
2248-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2249-
missing.id_for_lint,
2250-
missing.span,
2251-
BuiltinLintDiag::ElidedNamedLifetimes {
2252-
elided: (missing.span, missing.kind),
2253-
resolution: lint::ElidedLifetimeResolution::Param(
2254-
tcx.item_name(param.into()),
2255-
tcx.source_span(param),
2256-
),
2257-
},
2258-
);
2259-
}
2260-
LifetimeRes::Fresh { .. }
2261-
| LifetimeRes::Infer
2262-
| LifetimeRes::Error
2263-
| LifetimeRes::ElidedAnchor { .. } => {}
2264-
}
2265-
}
2266-
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
2267-
}
2268-
22692227
match res {
22702228
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
22712229
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
@@ -2788,14 +2746,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27882746
..
27892747
}) => {
27902748
self.with_static_rib(def_kind, |this| {
2791-
this.with_lifetime_rib(
2792-
LifetimeRibKind::Elided(LifetimeRes::Static {
2793-
suppress_elision_warning: true,
2794-
}),
2795-
|this| {
2796-
this.visit_ty(ty);
2797-
},
2798-
);
2749+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2750+
this.visit_ty(ty);
2751+
});
27992752
if let Some(expr) = expr {
28002753
// We already forbid generic params because of the above item rib,
28012754
// so it doesn't matter whether this is a trivial constant.
@@ -2832,9 +2785,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28322785
this.visit_generics(generics);
28332786

28342787
this.with_lifetime_rib(
2835-
LifetimeRibKind::Elided(LifetimeRes::Static {
2836-
suppress_elision_warning: true,
2837-
}),
2788+
LifetimeRibKind::Elided(LifetimeRes::Static),
28382789
|this| this.visit_ty(ty),
28392790
);
28402791

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
34403440
maybe_static = true;
34413441
in_scope_lifetimes = vec![(
34423442
Ident::with_dummy_span(kw::StaticLifetime),
3443-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3443+
(DUMMY_NODE_ID, LifetimeRes::Static),
34443444
)];
34453445
}
34463446
} else if elided_len == 0 {
@@ -3452,7 +3452,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
34523452
maybe_static = true;
34533453
in_scope_lifetimes = vec![(
34543454
Ident::with_dummy_span(kw::StaticLifetime),
3455-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3455+
(DUMMY_NODE_ID, LifetimeRes::Static),
34563456
)];
34573457
}
34583458
} else if num_params == 1 {

src/tools/clippy/tests/ui/explicit_iter_loop.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ fn main() {
7777

7878
struct NoIntoIter();
7979
impl NoIntoIter {
80-
fn iter(&self) -> slice::Iter<u8> {
80+
fn iter(&self) -> slice::Iter<'_, u8> {
8181
unimplemented!()
8282
}
8383

84-
fn iter_mut(&mut self) -> slice::IterMut<u8> {
84+
fn iter_mut(&mut self) -> slice::IterMut<'_, u8> {
8585
unimplemented!()
8686
}
8787
}

src/tools/clippy/tests/ui/explicit_iter_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ fn main() {
7777

7878
struct NoIntoIter();
7979
impl NoIntoIter {
80-
fn iter(&self) -> slice::Iter<u8> {
80+
fn iter(&self) -> slice::Iter<'_, u8> {
8181
unimplemented!()
8282
}
8383

84-
fn iter_mut(&mut self) -> slice::IterMut<u8> {
84+
fn iter_mut(&mut self) -> slice::IterMut<'_, u8> {
8585
unimplemented!()
8686
}
8787
}

src/tools/clippy/tests/ui/iter_next_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88

99
struct Unrelated(&'static [u8]);
1010
impl Unrelated {
11-
fn next(&self) -> std::slice::Iter<u8> {
11+
fn next(&self) -> std::slice::Iter<'_, u8> {
1212
self.0.iter()
1313
}
1414
}

src/tools/clippy/tests/ui/iter_not_returning_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl S {
7171

7272
struct S2([u8]);
7373
impl S2 {
74-
fn iter(&self) -> core::slice::Iter<u8> {
74+
fn iter(&self) -> core::slice::Iter<'_, u8> {
7575
self.0.iter()
7676
}
7777
}

0 commit comments

Comments
 (0)