Skip to content

Commit ce2ae62

Browse files
committed
Convert a hard-warning about named static lifetimes into lint "unused_lifetimes"
Define the `named_static_lifetimes` lint This lint will replace the existing hard-warning. Replace the named static lifetime hard-warning with the new lint Update the UI tests for the `named_static_lifetimes` lint Remove the direct dependency on `rustc_lint_defs` fix build Signed-off-by: Zhi Qi <[email protected]> use "UNUSED_LIFETIMES" instead Signed-off-by: Zhi Qi <[email protected]> update 1 test and fix typo Signed-off-by: Zhi Qi <[email protected]> update tests Signed-off-by: Zhi Qi <[email protected]> fix tests: add extra blank line Signed-off-by: Zhi Qi <[email protected]>
1 parent 246eae2 commit ce2ae62

21 files changed

+96
-121
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_middle::bug;
1818
use rustc_middle::hir::nested_filter;
1919
use rustc_middle::middle::resolve_bound_vars::*;
2020
use rustc_middle::ty::{self, ir::TypeVisitor, DefIdTree, TyCtxt, TypeSuperVisitable};
21+
use rustc_session::lint;
2122
use rustc_span::def_id::DefId;
2223
use rustc_span::symbol::{sym, Ident};
2324
use rustc_span::Span;
@@ -923,17 +924,16 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
923924
origin,
924925
..
925926
}) => {
926-
927927
let (bound_vars, binders): (FxIndexMap<LocalDefId, ResolvedArg>, Vec<_>) =
928928
bound_generic_params
929-
.iter()
930-
.enumerate()
931-
.map(|(late_bound_idx, param)| {
932-
let pair = ResolvedArg::late(late_bound_idx as u32, param);
933-
let r = late_arg_as_bound_arg(this.tcx, &pair.1, param);
934-
(pair, r)
935-
})
936-
.unzip();
929+
.iter()
930+
.enumerate()
931+
.map(|(late_bound_idx, param)| {
932+
let pair = ResolvedArg::late(late_bound_idx as u32, param);
933+
let r = late_arg_as_bound_arg(this.tcx, &pair.1, param);
934+
(pair, r)
935+
})
936+
.unzip();
937937
this.record_late_bound_vars(hir_id, binders.clone());
938938
// Even if there are no lifetimes defined here, we still wrap it in a binder
939939
// scope. If there happens to be a nested poly trait ref (an error), that
@@ -968,20 +968,22 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
968968
continue;
969969
}
970970
this.insert_lifetime(lt, ResolvedArg::StaticLifetime);
971-
this.tcx
972-
.sess
973-
.struct_span_warn(
974-
lifetime.ident.span,
975-
&format!(
976-
"unnecessary lifetime parameter `{}`",
971+
this.tcx.struct_span_lint_hir(
972+
lint::builtin::UNUSED_LIFETIMES,
973+
lifetime.hir_id,
974+
lifetime.ident.span,
975+
format!(
976+
"unnecessary lifetime parameter `{}`",
977+
lifetime.ident
978+
),
979+
|lint| {
980+
let help = &format!(
981+
"you can use the `'static` lifetime directly, in place of `{}`",
977982
lifetime.ident,
978-
),
979-
)
980-
.help(&format!(
981-
"you can use the `'static` lifetime directly, in place of `{}`",
982-
lifetime.ident,
983-
))
984-
.emit();
983+
);
984+
lint.help(help)
985+
},
986+
);
985987
}
986988
}
987989
}

tests/ui/generic-associated-types/method-unsatified-assoc-type-predicate.stderr renamed to tests/ui/generic-associated-types/method-unsatisfied-assoc-type-predicate.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0599]: the method `f` exists for struct `S`, but its trait bounds were not satisfied
2-
--> $DIR/method-unsatified-assoc-type-predicate.rs:28:7
2+
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:28:7
33
|
44
LL | struct S;
55
| --------
@@ -12,7 +12,7 @@ LL | a.f();
1212
| ^ method cannot be called on `S` due to unsatisfied trait bounds
1313
|
1414
note: trait bound `<S as X>::Y<i32> = i32` was not satisfied
15-
--> $DIR/method-unsatified-assoc-type-predicate.rs:12:11
15+
--> $DIR/method-unsatisfied-assoc-type-predicate.rs:12:11
1616
|
1717
LL | impl<T: X<Y<i32> = i32>> M for T {}
1818
| ^^^^^^^^^^^^ - -

tests/ui/generic-associated-types/unsatified-item-lifetime-bound.rs renamed to tests/ui/generic-associated-types/unsatisfied-item-lifetime-bound.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(unused_lifetimes)]
2+
13
pub trait X {
24
type Y<'a: 'static>;
35
//~^ WARNING unnecessary lifetime parameter

tests/ui/generic-associated-types/unsatified-item-lifetime-bound.stderr renamed to tests/ui/generic-associated-types/unsatisfied-item-lifetime-bound.stderr

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
11
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/unsatified-item-lifetime-bound.rs:2:12
2+
--> $DIR/unsatisfied-item-lifetime-bound.rs:4:12
33
|
44
LL | type Y<'a: 'static>;
55
| ^^
66
|
77
= help: you can use the `'static` lifetime directly, in place of `'a`
8+
note: the lint level is defined here
9+
--> $DIR/unsatisfied-item-lifetime-bound.rs:1:9
10+
|
11+
LL | #![warn(unused_lifetimes)]
12+
| ^^^^^^^^^^^^^^^^
813

914
error[E0478]: lifetime bound not satisfied
10-
--> $DIR/unsatified-item-lifetime-bound.rs:11:8
15+
--> $DIR/unsatisfied-item-lifetime-bound.rs:13:8
1116
|
1217
LL | f: <T as X>::Y<'a>,
1318
| ^^^^^^^^^^^^^^^
1419
|
1520
note: lifetime parameter instantiated with the lifetime `'a` as defined here
16-
--> $DIR/unsatified-item-lifetime-bound.rs:10:10
21+
--> $DIR/unsatisfied-item-lifetime-bound.rs:12:10
1722
|
1823
LL | struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
1924
| ^^
2025
= note: but lifetime parameter must outlive the static lifetime
2126

2227
error[E0478]: lifetime bound not satisfied
23-
--> $DIR/unsatified-item-lifetime-bound.rs:16:8
28+
--> $DIR/unsatisfied-item-lifetime-bound.rs:18:8
2429
|
2530
LL | f: <T as X>::Y<'a>,
2631
| ^^^^^^^^^^^^^^^
2732
|
2833
note: lifetime parameter instantiated with the lifetime `'a` as defined here
29-
--> $DIR/unsatified-item-lifetime-bound.rs:15:10
34+
--> $DIR/unsatisfied-item-lifetime-bound.rs:17:10
3035
|
3136
LL | struct C<'a, T: X> {
3237
| ^^
3338
= note: but lifetime parameter must outlive the static lifetime
3439

3540
error[E0478]: lifetime bound not satisfied
36-
--> $DIR/unsatified-item-lifetime-bound.rs:21:8
41+
--> $DIR/unsatisfied-item-lifetime-bound.rs:23:8
3742
|
3843
LL | f: <() as X>::Y<'a>,
3944
| ^^^^^^^^^^^^^^^^
4045
|
4146
note: lifetime parameter instantiated with the lifetime `'a` as defined here
42-
--> $DIR/unsatified-item-lifetime-bound.rs:20:10
47+
--> $DIR/unsatisfied-item-lifetime-bound.rs:22:10
4348
|
4449
LL | struct D<'a> {
4550
| ^^

tests/ui/impl-trait/equal-hidden-lifetimes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// `'a == 'static` so `&'a i32` is fine as the return type
77
fn equal_regions_static<'a: 'static>(x: &'a i32) -> impl Sized {
8-
//~^ WARNING unnecessary lifetime parameter `'a`
98
x
109
}
1110

tests/ui/impl-trait/equal-hidden-lifetimes.stderr

-10
This file was deleted.

tests/ui/issues/issue-30438-c.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ trait Trait { type Out; }
55
struct Test<'a> { s: &'a str }
66

77
fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
8-
//~^ WARN unnecessary lifetime parameter `'z`
98
let x = Test { s: "this cannot last" };
109
&x
1110
//~^ ERROR: cannot return reference to local variable `x`

tests/ui/issues/issue-30438-c.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
warning: unnecessary lifetime parameter `'z`
2-
--> $DIR/issue-30438-c.rs:7:74
3-
|
4-
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
5-
| ^^
6-
|
7-
= help: you can use the `'static` lifetime directly, in place of `'z`
8-
91
error[E0515]: cannot return reference to local variable `x`
10-
--> $DIR/issue-30438-c.rs:10:5
2+
--> $DIR/issue-30438-c.rs:9:5
113
|
124
LL | &x
135
| ^^ returns a reference to data owned by the current function
146

15-
error: aborting due to previous error; 1 warning emitted
7+
error: aborting due to previous error
168

179
For more information about this error, try `rustc --explain E0515`.

tests/ui/regions/regions-free-region-outlives-static-outlives-free-region.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//
99
// 'a : 'b
1010

11+
#![warn(unused_lifetimes)]
12+
1113
fn test<'a,'b>(x: &'a i32) -> &'b i32
1214
where 'a: 'static //~ WARN unnecessary lifetime parameter `'a`
1315
{
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:12:11
2+
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:14:11
33
|
44
LL | where 'a: 'static
55
| ^^
66
|
77
= help: you can use the `'static` lifetime directly, in place of `'a`
8+
note: the lint level is defined here
9+
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:11:9
10+
|
11+
LL | #![warn(unused_lifetimes)]
12+
| ^^^^^^^^^^^^^^^^
813

914
warning: 1 warning emitted
1015

tests/ui/regions/regions-static-bound-rpass.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// run-pass
22

3+
#![warn(unused_lifetimes)]
4+
35
fn invariant_id<'a,'b>(t: &'b mut &'static ()) -> &'b mut &'a ()
46
where 'a: 'static { t }
57
//~^ WARN unnecessary lifetime parameter `'a`

tests/ui/regions/regions-static-bound-rpass.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/regions-static-bound-rpass.rs:4:11
2+
--> $DIR/regions-static-bound-rpass.rs:6:11
33
|
44
LL | where 'a: 'static { t }
55
| ^^
66
|
77
= help: you can use the `'static` lifetime directly, in place of `'a`
8+
note: the lint level is defined here
9+
--> $DIR/regions-static-bound-rpass.rs:3:9
10+
|
11+
LL | #![warn(unused_lifetimes)]
12+
| ^^^^^^^^^^^^^^^^
813

914
warning: unnecessary lifetime parameter `'a`
10-
--> $DIR/regions-static-bound-rpass.rs:8:11
15+
--> $DIR/regions-static-bound-rpass.rs:10:11
1116
|
1217
LL | where 'a: 'static { t }
1318
| ^^
1419
|
1520
= help: you can use the `'static` lifetime directly, in place of `'a`
1621

1722
warning: unnecessary lifetime parameter `'b`
18-
--> $DIR/regions-static-bound-rpass.rs:12:19
23+
--> $DIR/regions-static-bound-rpass.rs:14:19
1924
|
2025
LL | where 'a: 'b, 'b: 'static { t }
2126
| ^^

tests/ui/regions/regions-static-bound.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
fn static_id<'a,'b>(t: &'a ()) -> &'static ()
2-
where 'a: 'static { t }
3-
//~^ WARN unnecessary lifetime parameter `'a`
1+
#![warn(unused_lifetimes)]
2+
3+
fn static_id<'a,'b>(t: &'a ()) -> &'static () where 'a: 'static { t }
4+
//~^ WARN lifetime parameter `'b` never used
5+
//~| WARN unnecessary lifetime parameter `'a`
46

57
fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static ()
68
where 'a: 'b, 'b: 'static { t }

tests/ui/regions/regions-static-bound.stderr

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
1+
warning: lifetime parameter `'b` never used
2+
--> $DIR/regions-static-bound.rs:3:17
3+
|
4+
LL | fn static_id<'a,'b>(t: &'a ()) -> &'static () where 'a: 'static { t }
5+
| -^^
6+
| |
7+
| help: elide the unused lifetime
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/regions-static-bound.rs:1:9
11+
|
12+
LL | #![warn(unused_lifetimes)]
13+
| ^^^^^^^^^^^^^^^^
14+
115
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/regions-static-bound.rs:2:11
16+
--> $DIR/regions-static-bound.rs:3:53
317
|
4-
LL | where 'a: 'static { t }
5-
| ^^
18+
LL | fn static_id<'a,'b>(t: &'a ()) -> &'static () where 'a: 'static { t }
19+
| ^^
620
|
721
= help: you can use the `'static` lifetime directly, in place of `'a`
822

923
warning: unnecessary lifetime parameter `'b`
10-
--> $DIR/regions-static-bound.rs:6:19
24+
--> $DIR/regions-static-bound.rs:8:19
1125
|
1226
LL | where 'a: 'b, 'b: 'static { t }
1327
| ^^
1428
|
1529
= help: you can use the `'static` lifetime directly, in place of `'b`
1630

1731
error: lifetime may not live long enough
18-
--> $DIR/regions-static-bound.rs:10:5
32+
--> $DIR/regions-static-bound.rs:12:5
1933
|
2034
LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
2135
| -- lifetime `'a` defined here
2236
LL | t
2337
| ^ returning this value requires that `'a` must outlive `'static`
2438

2539
error[E0521]: borrowed data escapes outside of function
26-
--> $DIR/regions-static-bound.rs:15:5
40+
--> $DIR/regions-static-bound.rs:17:5
2741
|
2842
LL | fn error(u: &(), v: &()) {
2943
| - - let's call the lifetime of this reference `'1`
@@ -36,7 +50,7 @@ LL | static_id(&u);
3650
| argument requires that `'1` must outlive `'static`
3751

3852
error[E0521]: borrowed data escapes outside of function
39-
--> $DIR/regions-static-bound.rs:17:5
53+
--> $DIR/regions-static-bound.rs:19:5
4054
|
4155
LL | fn error(u: &(), v: &()) {
4256
| - - let's call the lifetime of this reference `'2`
@@ -49,6 +63,6 @@ LL | static_id_indirect(&v);
4963
| `v` escapes the function body here
5064
| argument requires that `'2` must outlive `'static`
5165

52-
error: aborting due to 3 previous errors; 2 warnings emitted
66+
error: aborting due to 3 previous errors; 3 warnings emitted
5367

5468
For more information about this error, try `rustc --explain E0521`.

tests/ui/static/static-lifetime-bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn f<'a: 'static>(_: &'a i32) {} //~WARN unnecessary lifetime parameter `'a`
1+
fn f<'a: 'static>(_: &'a i32) {}
22

33
fn main() {
44
let x = 0;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/static-lifetime-bound.rs:1:6
3-
|
4-
LL | fn f<'a: 'static>(_: &'a i32) {}
5-
| ^^
6-
|
7-
= help: you can use the `'static` lifetime directly, in place of `'a`
8-
91
error[E0597]: `x` does not live long enough
102
--> $DIR/static-lifetime-bound.rs:5:7
113
|
@@ -19,6 +11,6 @@ LL | f(&x);
1911
LL | }
2012
| - `x` dropped here while still borrowed
2113

22-
error: aborting due to previous error; 1 warning emitted
14+
error: aborting due to previous error
2315

2416
For more information about this error, try `rustc --explain E0597`.

tests/ui/type-alias-impl-trait/bounds-are-checked.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
type X<'a> = impl Into<&'static str> + From<&'a str>;
77

88
fn f<'a: 'static>(t: &'a str) -> X<'a> {
9-
//~^ WARNING unnecessary lifetime parameter
109
t
1110
//~^ ERROR expected generic lifetime parameter, found `'static`
1211
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/bounds-are-checked.rs:8:6
3-
|
4-
LL | fn f<'a: 'static>(t: &'a str) -> X<'a> {
5-
| ^^
6-
|
7-
= help: you can use the `'static` lifetime directly, in place of `'a`
8-
91
error[E0792]: expected generic lifetime parameter, found `'static`
10-
--> $DIR/bounds-are-checked.rs:10:5
2+
--> $DIR/bounds-are-checked.rs:9:5
113
|
124
LL | type X<'a> = impl Into<&'static str> + From<&'a str>;
135
| -- cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type
146
...
157
LL | t
168
| ^
179

18-
error: aborting due to previous error; 1 warning emitted
10+
error: aborting due to previous error
1911

2012
For more information about this error, try `rustc --explain E0792`.

0 commit comments

Comments
 (0)