Skip to content

Commit 67b44b1

Browse files
committed
needless_lifetimes and elidable_lifetime_names
* Change lint message to not mention the lifetime names * Only point to the lifetime definition in the lint span.
1 parent 6f2567d commit 67b44b1

File tree

8 files changed

+198
-225
lines changed

8 files changed

+198
-225
lines changed

clippy_lints/src/lifetimes.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -790,15 +790,6 @@ fn report_elidable_lifetimes(
790790
usages: &[Lifetime],
791791
include_suggestions: bool,
792792
) {
793-
let lts = elidable_lts
794-
.iter()
795-
// In principle, the result of the call to `Node::ident` could be `unwrap`ped, as `DefId` should refer to a
796-
// `Node::GenericParam`.
797-
.filter_map(|&def_id| cx.tcx.hir_node_by_def_id(def_id).ident())
798-
.map(|ident| ident.to_string())
799-
.collect::<Vec<_>>()
800-
.join(", ");
801-
802793
let elidable_usages: Vec<ElidableUsage> = usages
803794
.iter()
804795
.filter(|usage| named_lifetime(usage).is_some_and(|id| elidable_lts.contains(&id)))
@@ -825,24 +816,15 @@ fn report_elidable_lifetimes(
825816
elidable_lts
826817
.iter()
827818
.map(|&lt| cx.tcx.def_span(lt))
828-
.chain(usages.iter().filter_map(|usage| {
829-
if let LifetimeKind::Param(def_id) = usage.kind
830-
&& elidable_lts.contains(&def_id)
831-
{
832-
return Some(usage.ident.span);
833-
}
834-
835-
None
836-
}))
837819
.collect_vec(),
838-
format!("the following explicit lifetimes could be elided: {lts}"),
820+
"these lifetime parameters can be elided",
839821
|diag| {
840822
if !include_suggestions {
841823
return;
842824
}
843825

844826
if let Some(suggestions) = elision_suggestions(cx, generics, elidable_lts, &elidable_usages) {
845-
diag.multipart_suggestion("elide the lifetimes", suggestions, Applicability::MachineApplicable);
827+
diag.multipart_suggestion("remove the lifetime parameters", suggestions, Applicability::MachineApplicable);
846828
}
847829
},
848830
);

tests/ui/crashes/elidable_lifetime_names_impl_trait.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
error: the following explicit lifetimes could be elided: 'a
1+
error: these lifetime parameters can be elided
22
--> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:12:6
33
|
44
LL | impl<'a> Foo for Baz<'a> {}
5-
| ^^ ^^
5+
| ^^
66
|
77
note: the lint level is defined here
88
--> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:1:9
99
|
1010
LL | #![deny(clippy::elidable_lifetime_names)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
help: elide the lifetimes
12+
help: remove the lifetime parameters
1313
|
1414
LL - impl<'a> Foo for Baz<'a> {}
1515
LL + impl Foo for Baz<'_> {}
1616
|
1717

18-
error: the following explicit lifetimes could be elided: 'a
18+
error: these lifetime parameters can be elided
1919
--> tests/ui/crashes/elidable_lifetime_names_impl_trait.rs:16:12
2020
|
2121
LL | fn baz<'a>(&'a self) -> impl Foo + 'a {
22-
| ^^ ^^ ^^
22+
| ^^
2323
|
24-
help: elide the lifetimes
24+
help: remove the lifetime parameters
2525
|
2626
LL - fn baz<'a>(&'a self) -> impl Foo + 'a {
2727
LL + fn baz(&self) -> impl Foo + '_ {

tests/ui/crashes/ice-2774.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: the following explicit lifetimes could be elided: 'a
1+
error: these lifetime parameters can be elided
22
--> tests/ui/crashes/ice-2774.rs:15:28
33
|
44
LL | pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) {
5-
| ^^ ^^
5+
| ^^
66
|
77
= note: `-D clippy::needless-lifetimes` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::needless_lifetimes)]`
9-
help: elide the lifetimes
9+
help: remove the lifetime parameters
1010
|
1111
LL - pub fn add_barfoos_to_foos<'a>(bars: &HashSet<&'a Bar>) {
1212
LL + pub fn add_barfoos_to_foos(bars: &HashSet<&Bar>) {

tests/ui/elidable_lifetime_names.fixed

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type Ref<'r> = &'r u8;
55
// No error; same lifetime on two params.
66
fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
77

8-
//~v ERROR: could be elided: 'a, 'b
8+
//~v elidable_lifetime_names
99
fn lifetime_param_2(_x: Ref<'_>, _y: &u8) {}
1010

1111
// No error; bounded lifetime.
@@ -30,7 +30,7 @@ where
3030
unreachable!()
3131
}
3232

33-
//~v ERROR: could be elided: 'a
33+
//~v elidable_lifetime_names
3434
fn fn_bound_2<F, I>(_m: Lt<'_, I>, _f: F) -> Lt<'_, I>
3535
where
3636
for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
@@ -40,7 +40,7 @@ where
4040

4141
struct Foo<'a>(&'a u8);
4242

43-
//~v ERROR: could be elided: 'a
43+
//~v elidable_lifetime_names
4444
fn struct_with_lt(_foo: Foo<'_>) -> &str {
4545
unimplemented!()
4646
}
@@ -55,14 +55,14 @@ fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
5555
unimplemented!()
5656
}
5757

58-
//~v ERROR: could be elided: 'b
58+
//~v elidable_lifetime_names
5959
fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str {
6060
unimplemented!()
6161
}
6262

6363
type FooAlias<'a> = Foo<'a>;
6464

65-
//~v ERROR: could be elided: 'a
65+
//~v elidable_lifetime_names
6666
fn alias_with_lt(_foo: FooAlias<'_>) -> &str {
6767
unimplemented!()
6868
}
@@ -77,7 +77,7 @@ fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
7777
unimplemented!()
7878
}
7979

80-
//~v ERROR: could be elided: 'b
80+
//~v elidable_lifetime_names
8181
fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str {
8282
unimplemented!()
8383
}
@@ -87,7 +87,7 @@ struct Cow<'a> {
8787
x: &'a str,
8888
}
8989

90-
//~v ERROR: could be elided: 'a
90+
//~v elidable_lifetime_names
9191
fn out_return_type_lts(e: &str) -> Cow<'_> {
9292
unimplemented!()
9393
}
@@ -99,10 +99,10 @@ mod issue2944 {
9999
bar: &'a Bar,
100100
}
101101

102-
//~v ERROR: could be elided: 'a
102+
//~v elidable_lifetime_names
103103
impl Foo for Baz<'_> {}
104104
impl Bar {
105-
//~v ERROR: could be elided: 'a
105+
//~v elidable_lifetime_names
106106
fn baz(&self) -> impl Foo + '_ {
107107
Baz { bar: self }
108108
}
@@ -135,7 +135,7 @@ mod issue13923 {
135135
}
136136
}
137137

138-
//~v ERROR: could be elided: 'py
138+
//~v elidable_lifetime_names
139139
impl<'t> ContentString<'t> {
140140
// `'py` can be elided because of `&self`
141141
fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> {
@@ -146,7 +146,7 @@ mod issue13923 {
146146
}
147147
}
148148

149-
//~v ERROR: could be elided: 'py
149+
//~v elidable_lifetime_names
150150
impl<'t> ContentString<'t> {
151151
// `'py` can be elided because of `&'_ self`
152152
fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, '_> {
@@ -167,7 +167,7 @@ mod issue13923 {
167167
}
168168
}
169169

170-
//~v ERROR: could be elided: 'py
170+
//~v elidable_lifetime_names
171171
impl<'t> ContentString<'t> {
172172
// `'py` can be elided because of `&Self`
173173
fn map_content5(

tests/ui/elidable_lifetime_names.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type Ref<'r> = &'r u8;
55
// No error; same lifetime on two params.
66
fn lifetime_param_1<'a>(_x: Ref<'a>, _y: &'a u8) {}
77

8-
//~v ERROR: could be elided: 'a, 'b
8+
//~v elidable_lifetime_names
99
fn lifetime_param_2<'a, 'b>(_x: Ref<'a>, _y: &'b u8) {}
1010

1111
// No error; bounded lifetime.
@@ -30,7 +30,7 @@ where
3030
unreachable!()
3131
}
3232

33-
//~v ERROR: could be elided: 'a
33+
//~v elidable_lifetime_names
3434
fn fn_bound_2<'a, F, I>(_m: Lt<'a, I>, _f: F) -> Lt<'a, I>
3535
where
3636
for<'x> F: Fn(Lt<'x, I>) -> Lt<'x, I>,
@@ -40,7 +40,7 @@ where
4040

4141
struct Foo<'a>(&'a u8);
4242

43-
//~v ERROR: could be elided: 'a
43+
//~v elidable_lifetime_names
4444
fn struct_with_lt<'a>(_foo: Foo<'a>) -> &'a str {
4545
unimplemented!()
4646
}
@@ -55,14 +55,14 @@ fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
5555
unimplemented!()
5656
}
5757

58-
//~v ERROR: could be elided: 'b
58+
//~v elidable_lifetime_names
5959
fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
6060
unimplemented!()
6161
}
6262

6363
type FooAlias<'a> = Foo<'a>;
6464

65-
//~v ERROR: could be elided: 'a
65+
//~v elidable_lifetime_names
6666
fn alias_with_lt<'a>(_foo: FooAlias<'a>) -> &'a str {
6767
unimplemented!()
6868
}
@@ -77,7 +77,7 @@ fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
7777
unimplemented!()
7878
}
7979

80-
//~v ERROR: could be elided: 'b
80+
//~v elidable_lifetime_names
8181
fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
8282
unimplemented!()
8383
}
@@ -87,7 +87,7 @@ struct Cow<'a> {
8787
x: &'a str,
8888
}
8989

90-
//~v ERROR: could be elided: 'a
90+
//~v elidable_lifetime_names
9191
fn out_return_type_lts<'a>(e: &'a str) -> Cow<'a> {
9292
unimplemented!()
9393
}
@@ -99,10 +99,10 @@ mod issue2944 {
9999
bar: &'a Bar,
100100
}
101101

102-
//~v ERROR: could be elided: 'a
102+
//~v elidable_lifetime_names
103103
impl<'a> Foo for Baz<'a> {}
104104
impl Bar {
105-
//~v ERROR: could be elided: 'a
105+
//~v elidable_lifetime_names
106106
fn baz<'a>(&'a self) -> impl Foo + 'a {
107107
Baz { bar: self }
108108
}
@@ -135,7 +135,7 @@ mod issue13923 {
135135
}
136136
}
137137

138-
//~v ERROR: could be elided: 'py
138+
//~v elidable_lifetime_names
139139
impl<'t, 'py> ContentString<'t> {
140140
// `'py` can be elided because of `&self`
141141
fn map_content2(&self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> {
@@ -146,7 +146,7 @@ mod issue13923 {
146146
}
147147
}
148148

149-
//~v ERROR: could be elided: 'py
149+
//~v elidable_lifetime_names
150150
impl<'t, 'py> ContentString<'t> {
151151
// `'py` can be elided because of `&'_ self`
152152
fn map_content3(&'_ self, f: impl FnOnce(&'t str) -> &'t str) -> Content<'t, 'py> {
@@ -167,7 +167,7 @@ mod issue13923 {
167167
}
168168
}
169169

170-
//~v ERROR: could be elided: 'py
170+
//~v elidable_lifetime_names
171171
impl<'t, 'py> ContentString<'t> {
172172
// `'py` can be elided because of `&Self`
173173
fn map_content5(

0 commit comments

Comments
 (0)