Skip to content

Commit 547db4a

Browse files
committed
elided_named_lifetimes: manually implement LintDiagnostic
1 parent dcfc713 commit 547db4a

20 files changed

+75
-59
lines changed

compiler/rustc_lint/messages.ftl

+1-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ lint_duplicate_matcher_binding = duplicate matcher binding
255255
lint_elided_named_lifetime = elided lifetime has a name
256256
.label_elided = this elided lifetime gets resolved as `{$name}`
257257
.label_named = lifetime `{$name}` declared here
258-
259-
lint_elided_named_lifetime_suggestion = consider specifying it explicitly
258+
.suggestion = consider specifying it explicitly
260259
261260
lint_enum_intrinsics_mem_discriminant =
262261
the return value of `mem::discriminant` is unspecified when called with a non-enum type

compiler/rustc_lint/src/context/diagnostics.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
77
use rustc_errors::{
88
elided_lifetime_in_path_suggestion, Applicability, Diag, DiagArgValue, LintDiagnostic,
99
};
10-
use rustc_hir::MissingLifetimeKind;
1110
use rustc_middle::middle::stability;
1211
use rustc_session::lint::{BuiltinLintDiag, ElidedLifetimeResolution};
1312
use rustc_session::Session;
1413
use rustc_span::symbol::kw;
1514
use rustc_span::BytePos;
1615
use tracing::debug;
1716

18-
use crate::fluent_generated;
1917
use crate::lints::{self, ElidedNamedLifetime};
2018

2119
mod check_cfg;
@@ -445,31 +443,15 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
445443
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
446444
}
447445
BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => {
448-
let (name, named_declaration) = match resolution {
449-
ElidedLifetimeResolution::Static => (kw::StaticLifetime, None),
450-
ElidedLifetimeResolution::Param(name, declaration) => (name, Some(declaration)),
451-
};
452-
ElidedNamedLifetime { span, name, named_declaration }.decorate_lint(diag);
453-
454-
let (applicability, suggestion) = match kind {
455-
MissingLifetimeKind::Underscore => {
456-
(Applicability::MachineApplicable, format!("{name}"))
446+
match resolution {
447+
ElidedLifetimeResolution::Static => {
448+
ElidedNamedLifetime { span, kind, name: kw::StaticLifetime, declaration: None }
457449
}
458-
MissingLifetimeKind::Ampersand => {
459-
(Applicability::MachineApplicable, format!("&{name} "))
450+
ElidedLifetimeResolution::Param(name, declaration) => {
451+
ElidedNamedLifetime { span, kind, name, declaration: Some(declaration) }
460452
}
461-
MissingLifetimeKind::Comma => (Applicability::Unspecified, format!("<{name}, ")),
462-
MissingLifetimeKind::Brackets => (
463-
Applicability::Unspecified,
464-
format!("{}<{name}>", sess.source_map().span_to_snippet(span).unwrap()),
465-
),
466-
};
467-
diag.span_suggestion_verbose(
468-
span,
469-
fluent_generated::lint_elided_named_lifetime_suggestion,
470-
suggestion,
471-
applicability,
472-
);
453+
}
454+
.decorate_lint(diag)
473455
}
474456
}
475457
}

compiler/rustc_lint/src/lints.rs

+41-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::{
99
};
1010
use rustc_hir::def::Namespace;
1111
use rustc_hir::def_id::DefId;
12-
use rustc_hir::{self as hir};
12+
use rustc_hir::{self as hir, MissingLifetimeKind};
1313
use rustc_macros::{LintDiagnostic, Subdiagnostic};
1414
use rustc_middle::ty::inhabitedness::InhabitedPredicate;
1515
use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt};
@@ -2623,14 +2623,49 @@ pub(crate) struct ElidedLifetimesInPaths {
26232623
pub subdiag: ElidedLifetimeInPathSubdiag,
26242624
}
26252625

2626-
#[derive(LintDiagnostic)]
2627-
#[diag(lint_elided_named_lifetime)]
26282626
pub(crate) struct ElidedNamedLifetime {
2629-
#[label(lint_label_elided)]
26302627
pub span: Span,
2628+
pub kind: MissingLifetimeKind,
26312629
pub name: Symbol,
2632-
#[label(lint_label_named)]
2633-
pub named_declaration: Option<Span>,
2630+
pub declaration: Option<Span>,
2631+
}
2632+
2633+
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2634+
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2635+
let Self { span, kind, name, declaration } = self;
2636+
diag.primary_message(fluent::lint_elided_named_lifetime);
2637+
diag.arg("name", name);
2638+
diag.span_label(span, fluent::lint_label_elided);
2639+
if let Some(declaration) = declaration {
2640+
diag.span_label(declaration, fluent::lint_label_named);
2641+
}
2642+
match kind {
2643+
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2644+
span,
2645+
fluent::lint_suggestion,
2646+
format!("{name}"),
2647+
Applicability::MachineApplicable,
2648+
),
2649+
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2650+
span.shrink_to_hi(),
2651+
fluent::lint_suggestion,
2652+
format!("{name} "),
2653+
Applicability::MachineApplicable,
2654+
),
2655+
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2656+
span.shrink_to_hi(),
2657+
fluent::lint_suggestion,
2658+
format!("{name}, "),
2659+
Applicability::MachineApplicable,
2660+
),
2661+
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2662+
span.shrink_to_hi(),
2663+
fluent::lint_suggestion,
2664+
format!("<{name}>"),
2665+
Applicability::MachineApplicable,
2666+
),
2667+
};
2668+
}
26342669
}
26352670

26362671
#[derive(LintDiagnostic)]

tests/ui/async-await/issues/issue-63388-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | ) -> &dyn Foo
1111
help: consider specifying it explicitly
1212
|
1313
LL | ) -> &'a dyn Foo
14-
| ~~~
14+
| ++
1515

1616
error[E0621]: explicit lifetime required in the type of `foo`
1717
--> $DIR/issue-63388-1.rs:13:5

tests/ui/const-generics/type-dependent/issue-71348.full.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Ta
88
help: consider specifying it explicitly
99
|
1010
LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<'a, N>>::Target
11-
| ~~~~
11+
| +++
1212

1313
warning: 1 warning emitted
1414

tests/ui/const-generics/type-dependent/issue-71348.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Ta
88
help: consider specifying it explicitly
99
|
1010
LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<'a, N>>::Target
11-
| ~~~~
11+
| +++
1212

1313
error: `&'static str` is forbidden as the type of a const generic parameter
1414
--> $DIR/issue-71348.rs:10:24

tests/ui/consts/min_const_fn/min_const_fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | const fn get_lt(&'a self) -> &T { &self.0 }
1111
help: consider specifying it explicitly
1212
|
1313
LL | const fn get_lt(&'a self) -> &'a T { &self.0 }
14-
| ~~~
14+
| ++
1515

1616
warning: elided lifetime has a name
1717
--> $DIR/min_const_fn.rs:48:42
@@ -25,7 +25,7 @@ LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
2525
help: consider specifying it explicitly
2626
|
2727
LL | const fn get_mut_lt(&'a mut self) -> &'a mut T { &mut self.0 }
28-
| ~~~
28+
| ++
2929

3030
error[E0493]: destructor of `Foo<T>` cannot be evaluated at compile-time
3131
--> $DIR/min_const_fn.rs:37:25

tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator<Item =
88
help: consider specifying it explicitly
99
|
1010
LL | pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator<Item = (u32, &'a u32)> {
11-
| ~~~
11+
| ++
1212

1313
warning: 1 warning emitted
1414

tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ LL | fn m<'a>(_: &'a Foo<'a>) -> &str { "" }
117117
help: consider specifying it explicitly
118118
|
119119
LL | fn m<'a>(_: &'a Foo<'a>) -> &'a str { "" }
120-
| ~~~
120+
| ++
121121

122122
error: aborting due to 7 previous errors; 1 warning emitted
123123

tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-if-else-using-impl-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn foo<'a>(&'a self, x: &i32) -> &i32 {
1010
help: consider specifying it explicitly
1111
|
1212
LL | fn foo<'a>(&'a self, x: &i32) -> &'a i32 {
13-
| ~~~
13+
| ++
1414

1515
error[E0621]: explicit lifetime required in the type of `x`
1616
--> $DIR/ex1-return-one-existing-name-if-else-using-impl-3.rs:9:36

tests/ui/lint/elided-named-lifetimes/example-from-issue48686.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | #![deny(elided_named_lifetimes)]
1212
help: consider specifying it explicitly
1313
|
1414
LL | pub fn get_mut(&'static self, x: &mut u8) -> &'static mut u8 {
15-
| ~~~~~~~~
15+
| +++++++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | #![deny(elided_named_lifetimes)]
1414
help: consider specifying it explicitly
1515
|
1616
LL | fn ampersand<'a>(x: &'a u8) -> &'a u8 {
17-
| ~~~
17+
| ++
1818

1919
error: elided lifetime has a name
2020
--> $DIR/missing-lifetime-kind.rs:10:31
@@ -27,7 +27,7 @@ LL | fn brackets<'a>(x: &'a u8) -> Brackets {
2727
help: consider specifying it explicitly
2828
|
2929
LL | fn brackets<'a>(x: &'a u8) -> Brackets<'a> {
30-
| ~~~~~~~~~~~~
30+
| ++++
3131

3232
error: elided lifetime has a name
3333
--> $DIR/missing-lifetime-kind.rs:17:33
@@ -40,7 +40,7 @@ LL | fn comma<'a>(x: &'a u8) -> Comma<u8> {
4040
help: consider specifying it explicitly
4141
|
4242
LL | fn comma<'a>(x: &'a u8) -> Comma<'a, u8> {
43-
| ~~~~
43+
| +++
4444

4545
error: elided lifetime has a name
4646
--> $DIR/missing-lifetime-kind.rs:22:34

tests/ui/lint/elided-named-lifetimes/not-tied-to-crate.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | #[warn(elided_named_lifetimes)]
1212
help: consider specifying it explicitly
1313
|
1414
LL | fn bar(x: &'static u8) -> &'static u8 {
15-
| ~~~~~~~~
15+
| +++++++
1616

1717
error: elided lifetime has a name
1818
--> $DIR/not-tied-to-crate.rs:11:31
@@ -28,7 +28,7 @@ LL | #[deny(elided_named_lifetimes)]
2828
help: consider specifying it explicitly
2929
|
3030
LL | fn baz(x: &'static u8) -> &'static u8 {
31-
| ~~~~~~~~
31+
| +++++++
3232

3333
error: aborting due to 1 previous error; 1 warning emitted
3434

tests/ui/lint/elided-named-lifetimes/static.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | #![deny(elided_named_lifetimes)]
1212
help: consider specifying it explicitly
1313
|
1414
LL | fn ampersand(x: &'static u8) -> &'static u8 {
15-
| ~~~~~~~~
15+
| +++++++
1616

1717
error: elided lifetime has a name
1818
--> $DIR/static.rs:23:32
@@ -23,7 +23,7 @@ LL | fn brackets(x: &'static u8) -> Brackets {
2323
help: consider specifying it explicitly
2424
|
2525
LL | fn brackets(x: &'static u8) -> Brackets<'static> {
26-
| ~~~~~~~~~~~~~~~~~
26+
| +++++++++
2727

2828
error: elided lifetime has a name
2929
--> $DIR/static.rs:30:34
@@ -34,7 +34,7 @@ LL | fn comma(x: &'static u8) -> Comma<u8> {
3434
help: consider specifying it explicitly
3535
|
3636
LL | fn comma(x: &'static u8) -> Comma<'static, u8> {
37-
| ~~~~~~~~~
37+
| ++++++++
3838

3939
error: elided lifetime has a name
4040
--> $DIR/static.rs:35:35

tests/ui/object-lifetime/object-lifetime-default-elision.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn load2<'a>(ss: &'a dyn SomeTrait) -> &dyn SomeTrait {
1010
help: consider specifying it explicitly
1111
|
1212
LL | fn load2<'a>(ss: &'a dyn SomeTrait) -> &'a dyn SomeTrait {
13-
| ~~~
13+
| ++
1414

1515
error: lifetime may not live long enough
1616
--> $DIR/object-lifetime-default-elision.rs:73:5

tests/ui/self/elision/ignore-non-reference-lifetimes.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn a<'a>(self: Self, a: &'a str) -> &str {
88
help: consider specifying it explicitly
99
|
1010
LL | fn a<'a>(self: Self, a: &'a str) -> &'a str {
11-
| ~~~
11+
| ++
1212

1313
warning: elided lifetime has a name
1414
--> $DIR/ignore-non-reference-lifetimes.rs:10:44
@@ -19,7 +19,7 @@ LL | fn b<'a>(self: Foo<'b>, a: &'a str) -> &str {
1919
help: consider specifying it explicitly
2020
|
2121
LL | fn b<'a>(self: Foo<'b>, a: &'a str) -> &'a str {
22-
| ~~~
22+
| ++
2323

2424
warning: 2 warnings emitted
2525

tests/ui/self/self_lifetime-async.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | async fn foo<'b>(self: &'b Foo<'a>) -> &() { self.0 }
1010
help: consider specifying it explicitly
1111
|
1212
LL | async fn foo<'b>(self: &'b Foo<'a>) -> &'b () { self.0 }
13-
| ~~~
13+
| ++
1414

1515
warning: elided lifetime has a name
1616
--> $DIR/self_lifetime-async.rs:12:52
@@ -21,7 +21,7 @@ LL | async fn bar<'a>(self: &Alias, arg: &'a ()) -> &() { arg }
2121
help: consider specifying it explicitly
2222
|
2323
LL | async fn bar<'a>(self: &Alias, arg: &'a ()) -> &'a () { arg }
24-
| ~~~
24+
| ++
2525

2626
warning: 2 warnings emitted
2727

tests/ui/self/self_lifetime.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn foo<'b>(self: &'b Foo<'a>) -> &() { self.0 }
1010
help: consider specifying it explicitly
1111
|
1212
LL | fn foo<'b>(self: &'b Foo<'a>) -> &'b () { self.0 }
13-
| ~~~
13+
| ++
1414

1515
warning: elided lifetime has a name
1616
--> $DIR/self_lifetime.rs:13:46
@@ -21,7 +21,7 @@ LL | fn bar<'a>(self: &Alias, arg: &'a ()) -> &() { arg }
2121
help: consider specifying it explicitly
2222
|
2323
LL | fn bar<'a>(self: &Alias, arg: &'a ()) -> &'a () { arg }
24-
| ~~~
24+
| ++
2525

2626
warning: 2 warnings emitted
2727

tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &T) {
134134
help: consider specifying it explicitly
135135
|
136136
LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &'a T) {
137-
| ~~~
137+
| ++
138138

139139
error[E0658]: anonymous lifetimes in `impl Trait` are unstable
140140
--> $DIR/impl-trait-missing-lifetime-gated.rs:6:35

tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | fn defining<'a, T>(x: &'a i32) -> Opaque<T> { x }
1010
help: consider specifying it explicitly
1111
|
1212
LL | fn defining<'a, T>(x: &'a i32) -> Opaque<'a, T> { x }
13-
| ~~~~
13+
| +++
1414

1515
error[E0700]: hidden type for `Opaque2<T>` captures lifetime that does not appear in bounds
1616
--> $DIR/missing_lifetime_bound.rs:5:47

0 commit comments

Comments
 (0)