Skip to content

Commit ccfe968

Browse files
authored
Rollup merge of #145798 - compiler-errors:unnamed-lt-primary, r=lqd
Use unnamed lifetime spans as primary spans for `MISMATCHED_LIFETIME_SYNTAXES` Fixes #145772 This PR changes the primary span(s) of the `MISMATCHED_LIFETIME_SYNTAXES` to point to the *unnamed* lifetime spans in both the inputs and *outputs* of the function signature. As reported in #145772, this should make it so that IDEs highlight the spans of the actionable part of this lint, rather than just the (possibly named) input spans like they do today. This could be tweaked further perhaps, for example for `fn foo(_: T<'_>) -> T`, we don't need to highlight the elided lifetime if the actionable part is to change only the return type to `T<'_>`, but I think it's improvement on what's here today, so I think that should be follow-up since I think the logic might get a bit hairy. cc ```@shepmaster```
2 parents 7026c84 + e4557f0 commit ccfe968

File tree

13 files changed

+122
-125
lines changed

13 files changed

+122
-125
lines changed

compiler/rustc_lint/src/lifetime_syntax.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ impl<T> LifetimeSyntaxCategories<Vec<T>> {
214214
}
215215
}
216216

217-
pub fn flatten(&self) -> impl Iterator<Item = &T> {
218-
let Self { hidden, elided, named } = self;
219-
[hidden.iter(), elided.iter(), named.iter()].into_iter().flatten()
217+
pub fn iter_unnamed(&self) -> impl Iterator<Item = &T> {
218+
let Self { hidden, elided, named: _ } = self;
219+
[hidden.iter(), elided.iter()].into_iter().flatten()
220220
}
221221
}
222222

@@ -495,7 +495,7 @@ fn emit_mismatch_diagnostic<'tcx>(
495495

496496
cx.emit_span_lint(
497497
MISMATCHED_LIFETIME_SYNTAXES,
498-
inputs.flatten().copied().collect::<Vec<_>>(),
498+
inputs.iter_unnamed().chain(outputs.iter_unnamed()).copied().collect::<Vec<_>>(),
499499
lints::MismatchedLifetimeSyntaxes { inputs, outputs, suggestions },
500500
);
501501
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ LL | fn barbar(_x: &mut Vec<u32>, y: &mut String) {
268268
| ^^^^^^^^^^^ help: change this to: `&mut str`
269269

270270
error: eliding a lifetime that's named elsewhere is confusing
271-
--> tests/ui/ptr_arg.rs:314:36
271+
--> tests/ui/ptr_arg.rs:314:56
272272
|
273273
LL | fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
274-
| ^^ ^^ ---- the same lifetime is elided here
274+
| -- -- ^^^^ the same lifetime is elided here
275275
| | |
276276
| | the lifetime is named here
277277
| the lifetime is named here

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: hiding a lifetime that's named elsewhere is confusing
2-
--> $DIR/issue-71348.rs:18:40
2+
--> $DIR/issue-71348.rs:18:56
33
|
44
LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a <Self as Get<N>>::Target
5-
| ^^ -- ------------------------ the same lifetime is hidden here
5+
| -- -- ^^^^^^^^^^^^^^^^^^^^^^^^ the same lifetime is hidden here
66
| | |
77
| | the same lifetime is named here
88
| the lifetime is named here

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: eliding a lifetime that's named elsewhere is confusing
2-
--> $DIR/rpit-assoc-pair-with-lifetime.rs:3:31
2+
--> $DIR/rpit-assoc-pair-with-lifetime.rs:3:82
33
|
44
LL | pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator<Item = (u32, &u32)> {
5-
| ^^ the lifetime is named here ---- the same lifetime is elided here
5+
| -- the lifetime is named here ^^^^ the same lifetime is elided here
66
|
77
= help: the same lifetime is referred to in inconsistent ways, making the signature confusing
88
= note: `#[warn(mismatched_lifetime_syntaxes)]` on by default

tests/ui/lifetimes/mismatched-lifetime-syntaxes-details/example-from-issue48686.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: eliding a lifetime that's named elsewhere is confusing
2-
--> $DIR/example-from-issue48686.rs:6:21
2+
--> $DIR/example-from-issue48686.rs:6:50
33
|
44
LL | pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
5-
| ^^^^^^^ ------- the same lifetime is elided here
5+
| ------- ^^^^^^^ the same lifetime is elided here
66
| |
77
| the lifetime is named here
88
|

tests/ui/lifetimes/mismatched-lifetime-syntaxes-details/missing-lifetime-kind.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: eliding a lifetime that's named elsewhere is confusing
2-
--> $DIR/missing-lifetime-kind.rs:3:22
2+
--> $DIR/missing-lifetime-kind.rs:3:32
33
|
44
LL | fn ampersand<'a>(x: &'a u8) -> &u8 {
5-
| ^^ --- the same lifetime is elided here
5+
| -- ^^^ the same lifetime is elided here
66
| |
77
| the lifetime is named here
88
|
@@ -18,10 +18,10 @@ LL | fn ampersand<'a>(x: &'a u8) -> &'a u8 {
1818
| ++
1919

2020
error: hiding a lifetime that's named elsewhere is confusing
21-
--> $DIR/missing-lifetime-kind.rs:10:21
21+
--> $DIR/missing-lifetime-kind.rs:10:31
2222
|
2323
LL | fn brackets<'a>(x: &'a u8) -> Brackets {
24-
| ^^ -------- the same lifetime is hidden here
24+
| -- ^^^^^^^^ the same lifetime is hidden here
2525
| |
2626
| the lifetime is named here
2727
|
@@ -32,10 +32,10 @@ LL | fn brackets<'a>(x: &'a u8) -> Brackets<'a> {
3232
| ++++
3333

3434
error: hiding a lifetime that's named elsewhere is confusing
35-
--> $DIR/missing-lifetime-kind.rs:17:18
35+
--> $DIR/missing-lifetime-kind.rs:17:28
3636
|
3737
LL | fn comma<'a>(x: &'a u8) -> Comma<u8> {
38-
| ^^ --------- the same lifetime is hidden here
38+
| -- ^^^^^^^^^ the same lifetime is hidden here
3939
| |
4040
| the lifetime is named here
4141
|
@@ -46,10 +46,10 @@ LL | fn comma<'a>(x: &'a u8) -> Comma<'a, u8> {
4646
| +++
4747

4848
error: eliding a lifetime that's named elsewhere is confusing
49-
--> $DIR/missing-lifetime-kind.rs:22:23
49+
--> $DIR/missing-lifetime-kind.rs:22:34
5050
|
5151
LL | fn underscore<'a>(x: &'a u8) -> &'_ u8 {
52-
| ^^ -- the same lifetime is elided here
52+
| -- ^^ the same lifetime is elided here
5353
| |
5454
| the lifetime is named here
5555
|

tests/ui/lifetimes/mismatched-lifetime-syntaxes-details/not-tied-to-crate.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: eliding a lifetime that's named elsewhere is confusing
2-
--> $DIR/not-tied-to-crate.rs:8:16
2+
--> $DIR/not-tied-to-crate.rs:8:31
33
|
44
LL | fn bar(x: &'static u8) -> &u8 {
5-
| ^^^^^^^ --- the same lifetime is elided here
5+
| ------- ^^^ the same lifetime is elided here
66
| |
77
| the lifetime is named here
88
|
@@ -18,10 +18,10 @@ LL | fn bar(x: &'static u8) -> &'static u8 {
1818
| +++++++
1919

2020
error: eliding a lifetime that's named elsewhere is confusing
21-
--> $DIR/not-tied-to-crate.rs:14:16
21+
--> $DIR/not-tied-to-crate.rs:14:31
2222
|
2323
LL | fn baz(x: &'static u8) -> &u8 {
24-
| ^^^^^^^ --- the same lifetime is elided here
24+
| ------- ^^^ the same lifetime is elided here
2525
| |
2626
| the lifetime is named here
2727
|

tests/ui/lifetimes/mismatched-lifetime-syntaxes-details/static.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: eliding a lifetime that's named elsewhere is confusing
2-
--> $DIR/static.rs:16:18
2+
--> $DIR/static.rs:16:33
33
|
44
LL | fn ampersand(x: &'static u8) -> &u8 {
5-
| ^^^^^^^ --- the same lifetime is elided here
5+
| ------- ^^^ the same lifetime is elided here
66
| |
77
| the lifetime is named here
88
|
@@ -18,10 +18,10 @@ LL | fn ampersand(x: &'static u8) -> &'static u8 {
1818
| +++++++
1919

2020
error: hiding a lifetime that's named elsewhere is confusing
21-
--> $DIR/static.rs:23:17
21+
--> $DIR/static.rs:23:32
2222
|
2323
LL | fn brackets(x: &'static u8) -> Brackets {
24-
| ^^^^^^^ -------- the same lifetime is hidden here
24+
| ------- ^^^^^^^^ the same lifetime is hidden here
2525
| |
2626
| the lifetime is named here
2727
|
@@ -32,10 +32,10 @@ LL | fn brackets(x: &'static u8) -> Brackets<'static> {
3232
| +++++++++
3333

3434
error: hiding a lifetime that's named elsewhere is confusing
35-
--> $DIR/static.rs:30:14
35+
--> $DIR/static.rs:30:29
3636
|
3737
LL | fn comma(x: &'static u8) -> Comma<u8> {
38-
| ^^^^^^^ --------- the same lifetime is hidden here
38+
| ------- ^^^^^^^^^ the same lifetime is hidden here
3939
| |
4040
| the lifetime is named here
4141
|
@@ -46,10 +46,10 @@ LL | fn comma(x: &'static u8) -> Comma<'static, u8> {
4646
| ++++++++
4747

4848
error: eliding a lifetime that's named elsewhere is confusing
49-
--> $DIR/static.rs:35:19
49+
--> $DIR/static.rs:35:35
5050
|
5151
LL | fn underscore(x: &'static u8) -> &'_ u8 {
52-
| ^^^^^^^ -- the same lifetime is elided here
52+
| ------- ^^ the same lifetime is elided here
5353
| |
5454
| the lifetime is named here
5555
|

tests/ui/lifetimes/mismatched-lifetime-syntaxes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ fn explicit_bound_path_to_implicit_path<'a>(v: ContainsLifetime<'a>) -> Contains
3636

3737
fn explicit_bound_path_to_explicit_anonymous_path<'a>(
3838
v: ContainsLifetime<'a>,
39-
//~^ ERROR eliding a lifetime that's named elsewhere is confusing
4039
) -> ContainsLifetime<'_> {
40+
//~^ ERROR eliding a lifetime that's named elsewhere is confusing
4141
v
4242
}
4343

@@ -188,8 +188,8 @@ mod impl_trait {
188188

189189
fn explicit_bound_path_to_impl_trait_precise_capture<'a>(
190190
v: ContainsLifetime<'a>,
191-
//~^ ERROR eliding a lifetime that's named elsewhere is confusing
192191
) -> impl FnOnce() + use<'_> {
192+
//~^ ERROR eliding a lifetime that's named elsewhere is confusing
193193
move || _ = v
194194
}
195195
}
@@ -208,8 +208,8 @@ mod dyn_trait {
208208

209209
fn explicit_bound_path_to_dyn_trait_bound<'a>(
210210
v: ContainsLifetime<'a>,
211-
//~^ ERROR hiding a lifetime that's named elsewhere is confusing
212211
) -> Box<dyn Iterator<Item = ContainsLifetime> + '_> {
212+
//~^ ERROR hiding a lifetime that's named elsewhere is confusing
213213
Box::new(iter::once(v))
214214
}
215215
}

0 commit comments

Comments
 (0)