Skip to content

Commit 5611e54

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent ecb2c22 commit 5611e54

File tree

63 files changed

+375
-556
lines changed

Some content is hidden

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

63 files changed

+375
-556
lines changed

compiler/rustc_hir/src/def.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,7 @@ pub enum LifetimeRes {
850850
/// late resolution. Those lifetimes will be inferred by typechecking.
851851
Infer,
852852
/// `'static` lifetime.
853-
Static {
854-
/// We do not want to emit `elided_named_lifetimes`
855-
/// when we are inside of a const item or a static,
856-
/// because it would get too annoying.
857-
suppress_elision_warning: bool,
858-
},
853+
Static,
859854
/// Resolution failure.
860855
Error,
861856
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_lint/messages.ftl

-5
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ lint_duplicate_macro_attribute =
251251
252252
lint_duplicate_matcher_binding = duplicate matcher binding
253253
254-
lint_elided_named_lifetime = elided lifetime has a name
255-
.label_elided = this elided lifetime gets resolved as `{$name}`
256-
.label_named = lifetime `{$name}` declared here
257-
.suggestion = consider specifying it explicitly
258-
259254
lint_enum_intrinsics_mem_discriminant =
260255
the return value of `mem::discriminant` is unspecified when called with a non-enum type
261256
.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

+3-14
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

@@ -450,16 +450,5 @@ pub(super) fn decorate_lint(
450450
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
451451
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
452452
}
453-
BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => {
454-
match resolution {
455-
ElidedLifetimeResolution::Static => {
456-
ElidedNamedLifetime { span, kind, name: kw::StaticLifetime, declaration: None }
457-
}
458-
ElidedLifetimeResolution::Param(name, declaration) => {
459-
ElidedNamedLifetime { span, kind, name, declaration: Some(declaration) }
460-
}
461-
}
462-
.decorate_lint(diag)
463-
}
464453
}
465454
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ fn register_builtins(store: &mut LintStore) {
355355
store.register_renamed("unused_tuple_struct_fields", "dead_code");
356356
store.register_renamed("static_mut_ref", "static_mut_refs");
357357
store.register_renamed("temporary_cstring_as_ptr", "dangling_pointers_from_temporaries");
358+
store.register_renamed("elided_named_lifetimes", "mismatched_lifetime_syntaxes");
358359

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

compiler/rustc_lint/src/lifetime_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ declare_lint! {
6262
/// In certain `unsafe` code, lifetime elision combined with
6363
/// inconsistent lifetime syntax may result in unsound code.
6464
pub MISMATCHED_LIFETIME_SYNTAXES,
65-
Allow,
65+
Warn,
6666
"detects when an elided lifetime uses different syntax between arguments and return values"
6767
}
6868

compiler/rustc_lint/src/lints.rs

+2-54
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};
@@ -2669,58 +2669,6 @@ pub(crate) struct ElidedLifetimesInPaths {
26692669
pub subdiag: ElidedLifetimeInPathSubdiag,
26702670
}
26712671

2672-
pub(crate) struct ElidedNamedLifetime {
2673-
pub span: Span,
2674-
pub kind: MissingLifetimeKind,
2675-
pub name: Symbol,
2676-
pub declaration: Option<Span>,
2677-
}
2678-
2679-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2680-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2681-
let Self { span, kind, name, declaration } = self;
2682-
diag.primary_message(fluent::lint_elided_named_lifetime);
2683-
diag.arg("name", name);
2684-
diag.span_label(span, fluent::lint_label_elided);
2685-
if let Some(declaration) = declaration {
2686-
diag.span_label(declaration, fluent::lint_label_named);
2687-
}
2688-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2689-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2690-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2691-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2692-
if name != kw::StaticLifetime {
2693-
return;
2694-
}
2695-
match kind {
2696-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2697-
span,
2698-
fluent::lint_suggestion,
2699-
format!("{name}"),
2700-
Applicability::MachineApplicable,
2701-
),
2702-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2703-
span.shrink_to_hi(),
2704-
fluent::lint_suggestion,
2705-
format!("{name} "),
2706-
Applicability::MachineApplicable,
2707-
),
2708-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2709-
span.shrink_to_hi(),
2710-
fluent::lint_suggestion,
2711-
format!("{name}, "),
2712-
Applicability::MachineApplicable,
2713-
),
2714-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2715-
span.shrink_to_hi(),
2716-
fluent::lint_suggestion,
2717-
format!("<{name}>"),
2718-
Applicability::MachineApplicable,
2719-
),
2720-
};
2721-
}
2722-
}
2723-
27242672
#[derive(LintDiagnostic)]
27252673
#[diag(lint_invalid_crate_type_value)]
27262674
pub(crate) struct UnknownCrateTypes {

compiler/rustc_lint_defs/src/builtin.rs

-33
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

+1-11
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

+6-55
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17011701
if ident.name == kw::StaticLifetime {
17021702
self.record_lifetime_res(
17031703
lifetime.id,
1704-
LifetimeRes::Static { suppress_elision_warning: false },
1704+
LifetimeRes::Static,
17051705
LifetimeElisionCandidate::Named,
17061706
);
17071707
return;
@@ -1849,8 +1849,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18491849
if lifetimes_in_scope.is_empty() {
18501850
self.record_lifetime_res(
18511851
lifetime.id,
1852-
// We are inside a const item, so do not warn.
1853-
LifetimeRes::Static { suppress_elision_warning: true },
1852+
LifetimeRes::Static,
18541853
elision_candidate,
18551854
);
18561855
return;
@@ -2197,47 +2196,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
21972196
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
21982197
}
21992198

2200-
match candidate {
2201-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
2202-
debug_assert_eq!(id, missing.id);
2203-
match res {
2204-
LifetimeRes::Static { suppress_elision_warning } => {
2205-
if !suppress_elision_warning {
2206-
self.r.lint_buffer.buffer_lint(
2207-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2208-
missing.id_for_lint,
2209-
missing.span,
2210-
BuiltinLintDiag::ElidedNamedLifetimes {
2211-
elided: (missing.span, missing.kind),
2212-
resolution: lint::ElidedLifetimeResolution::Static,
2213-
},
2214-
);
2215-
}
2216-
}
2217-
LifetimeRes::Param { param, binder: _ } => {
2218-
let tcx = self.r.tcx();
2219-
self.r.lint_buffer.buffer_lint(
2220-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2221-
missing.id_for_lint,
2222-
missing.span,
2223-
BuiltinLintDiag::ElidedNamedLifetimes {
2224-
elided: (missing.span, missing.kind),
2225-
resolution: lint::ElidedLifetimeResolution::Param(
2226-
tcx.item_name(param.into()),
2227-
tcx.source_span(param),
2228-
),
2229-
},
2230-
);
2231-
}
2232-
LifetimeRes::Fresh { .. }
2233-
| LifetimeRes::Infer
2234-
| LifetimeRes::Error
2235-
| LifetimeRes::ElidedAnchor { .. } => {}
2236-
}
2237-
}
2238-
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
2239-
}
2240-
22412199
match res {
22422200
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
22432201
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
@@ -2755,14 +2713,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27552713
..
27562714
}) => {
27572715
self.with_static_rib(def_kind, |this| {
2758-
this.with_lifetime_rib(
2759-
LifetimeRibKind::Elided(LifetimeRes::Static {
2760-
suppress_elision_warning: true,
2761-
}),
2762-
|this| {
2763-
this.visit_ty(ty);
2764-
},
2765-
);
2716+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2717+
this.visit_ty(ty);
2718+
});
27662719
if let Some(expr) = expr {
27672720
// We already forbid generic params because of the above item rib,
27682721
// so it doesn't matter whether this is a trivial constant.
@@ -2799,9 +2752,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27992752
this.visit_generics(generics);
28002753

28012754
this.with_lifetime_rib(
2802-
LifetimeRibKind::Elided(LifetimeRes::Static {
2803-
suppress_elision_warning: true,
2804-
}),
2755+
LifetimeRibKind::Elided(LifetimeRes::Static),
28052756
|this| this.visit_ty(ty),
28062757
);
28072758

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32763276
maybe_static = true;
32773277
in_scope_lifetimes = vec![(
32783278
Ident::with_dummy_span(kw::StaticLifetime),
3279-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3279+
(DUMMY_NODE_ID, LifetimeRes::Static),
32803280
)];
32813281
}
32823282
} else if elided_len == 0 {
@@ -3288,7 +3288,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32883288
maybe_static = true;
32893289
in_scope_lifetimes = vec![(
32903290
Ident::with_dummy_span(kw::StaticLifetime),
3291-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3291+
(DUMMY_NODE_ID, LifetimeRes::Static),
32923292
)];
32933293
}
32943294
} else if num_params == 1 {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
clippy::unnecessary_wraps,
1111
dyn_drop,
1212
clippy::get_first,
13-
elided_named_lifetimes
13+
mismatched_lifetime_syntaxes,
1414
)]
1515

1616
extern crate proc_macros;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
clippy::unnecessary_wraps,
1111
dyn_drop,
1212
clippy::get_first,
13-
elided_named_lifetimes
13+
mismatched_lifetime_syntaxes,
1414
)]
1515

1616
extern crate proc_macros;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod issue_9218 {
312312

313313
// Inferred to be `&'a str`, afaik.
314314
fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
315-
//~^ ERROR: elided lifetime has a name
315+
//~^ ERROR: lifetime flowing from input to output with different syntax
316316
todo!()
317317
}
318318
}

0 commit comments

Comments
 (0)