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

Lines changed: 24 additions & 22 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 12 additions & 7 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 2 additions & 0 deletions
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
{

0 commit comments

Comments
 (0)