Skip to content

Commit 5e6f119

Browse files
Actually, just reuse the UNUSED_LIFETIMES lint
1 parent 18e9c4f commit 5e6f119

13 files changed

+107
-124
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

-26
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_middle::hir::nested_filter;
1919
use rustc_middle::middle::resolve_bound_vars::*;
2020
use rustc_middle::query::Providers;
2121
use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
22-
use rustc_session::lint;
2322
use rustc_span::def_id::DefId;
2423
use rustc_span::symbol::{sym, Ident};
2524
use rustc_span::{Span, DUMMY_SP};
@@ -905,31 +904,6 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
905904
}) => {
906905
self.visit_lifetime(lifetime);
907906
walk_list!(self, visit_param_bound, bounds);
908-
909-
if lifetime.res != hir::LifetimeName::Static {
910-
for bound in bounds {
911-
let hir::GenericBound::Outlives(lt) = bound else {
912-
continue;
913-
};
914-
if lt.res != hir::LifetimeName::Static {
915-
continue;
916-
}
917-
self.insert_lifetime(lt, ResolvedArg::StaticLifetime);
918-
self.tcx.struct_span_lint_hir(
919-
lint::builtin::UNUSED_LIFETIMES,
920-
lifetime.hir_id,
921-
lifetime.ident.span,
922-
format!("unnecessary lifetime parameter `{}`", lifetime.ident),
923-
|lint| {
924-
let help = format!(
925-
"you can use the `'static` lifetime directly, in place of `{}`",
926-
lifetime.ident,
927-
);
928-
lint.help(help)
929-
},
930-
);
931-
}
932-
}
933907
}
934908
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { lhs_ty, rhs_ty, .. }) => {
935909
self.visit_ty(lhs_ty);

compiler/rustc_lint/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ lint_reason_must_be_string_literal = reason must be a string literal
470470
471471
lint_reason_must_come_last = reason in lint attribute must come last
472472
473-
lint_redundant_lifetime_args = lifetime `{$victim}` is required to be equal to `{$candidate}`, and is redundant and can be removed
473+
lint_redundant_lifetime_args = unnecessary lifetime parameter `{$victim}`
474+
.note = you can use the `{$candidate}` lifetime directly, in place of `{$victim}`
474475
475476
lint_redundant_semicolons =
476477
unnecessary trailing {$multiple ->

compiler/rustc_lint/src/redundant_lifetime_args.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@ use rustc_infer::infer::outlives::env::OutlivesEnvironment;
88
use rustc_infer::infer::{SubregionOrigin, TyCtxtInferExt};
99
use rustc_macros::LintDiagnostic;
1010
use rustc_middle::ty::{self, TyCtxt};
11+
use rustc_session::lint::builtin::UNUSED_LIFETIMES;
1112
use rustc_span::DUMMY_SP;
1213
use rustc_trait_selection::traits::{outlives_bounds::InferCtxtExt, ObligationCtxt};
1314

1415
use crate::{LateContext, LateLintPass};
1516

16-
declare_lint! {
17-
/// Docs
18-
pub REDUNDANT_LIFETIME_ARGS,
19-
Allow,
20-
"do something"
21-
}
22-
23-
declare_lint_pass!(RedundantLifetimeArgs => [REDUNDANT_LIFETIME_ARGS]);
17+
declare_lint_pass!(RedundantLifetimeArgs => []);
2418

2519
impl<'tcx> LateLintPass<'tcx> for RedundantLifetimeArgs {
2620
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
@@ -142,10 +136,10 @@ fn check<'tcx>(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, owner_id: hir::
142136
if infcx.resolve_regions(outlives_env).is_empty() {
143137
shadowed.insert(victim);
144138
tcx.emit_spanned_lint(
145-
REDUNDANT_LIFETIME_ARGS,
139+
UNUSED_LIFETIMES,
146140
tcx.local_def_id_to_hir_id(def_id.expect_local()),
147141
tcx.def_span(def_id),
148-
RedundantLifetimeArgsList { candidate, victim },
142+
RedundantLifetimeArgsLint { candidate, victim },
149143
);
150144
}
151145
}
@@ -154,7 +148,8 @@ fn check<'tcx>(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, owner_id: hir::
154148

155149
#[derive(LintDiagnostic)]
156150
#[diag(lint_redundant_lifetime_args)]
157-
struct RedundantLifetimeArgsList<'tcx> {
151+
#[note]
152+
struct RedundantLifetimeArgsLint<'tcx> {
158153
candidate: ty::Region<'tcx>,
159154
victim: ty::Region<'tcx>,
160155
}

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

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

85
impl X for () {
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,44 @@
1-
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/unsatisfied-item-lifetime-bound.rs:4:12
3-
|
4-
LL | type Y<'a: 'static>;
5-
| ^^
6-
|
7-
= 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-
| ^^^^^^^^^^^^^^^^
13-
141
error[E0478]: lifetime bound not satisfied
15-
--> $DIR/unsatisfied-item-lifetime-bound.rs:14:8
2+
--> $DIR/unsatisfied-item-lifetime-bound.rs:11:8
163
|
174
LL | f: <T as X>::Y<'a>,
185
| ^^^^^^^^^^^^^^^
196
|
207
note: lifetime parameter instantiated with the lifetime `'a` as defined here
21-
--> $DIR/unsatisfied-item-lifetime-bound.rs:13:10
8+
--> $DIR/unsatisfied-item-lifetime-bound.rs:10:10
229
|
2310
LL | struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
2411
| ^^
2512
= note: but lifetime parameter must outlive the static lifetime
2613

2714
error[E0478]: lifetime bound not satisfied
28-
--> $DIR/unsatisfied-item-lifetime-bound.rs:19:8
15+
--> $DIR/unsatisfied-item-lifetime-bound.rs:16:8
2916
|
3017
LL | f: <T as X>::Y<'a>,
3118
| ^^^^^^^^^^^^^^^
3219
|
3320
note: lifetime parameter instantiated with the lifetime `'a` as defined here
34-
--> $DIR/unsatisfied-item-lifetime-bound.rs:18:10
21+
--> $DIR/unsatisfied-item-lifetime-bound.rs:15:10
3522
|
3623
LL | struct C<'a, T: X> {
3724
| ^^
3825
= note: but lifetime parameter must outlive the static lifetime
3926

4027
error[E0478]: lifetime bound not satisfied
41-
--> $DIR/unsatisfied-item-lifetime-bound.rs:24:8
28+
--> $DIR/unsatisfied-item-lifetime-bound.rs:21:8
4229
|
4330
LL | f: <() as X>::Y<'a>,
4431
| ^^^^^^^^^^^^^^^^
4532
|
4633
note: lifetime parameter instantiated with the lifetime `'a` as defined here
47-
--> $DIR/unsatisfied-item-lifetime-bound.rs:23:10
34+
--> $DIR/unsatisfied-item-lifetime-bound.rs:20:10
4835
|
4936
LL | struct D<'a> {
5037
| ^^
5138
= note: but lifetime parameter must outlive the static lifetime
5239

5340
error[E0478]: lifetime bound not satisfied
54-
--> $DIR/unsatisfied-item-lifetime-bound.rs:9:18
41+
--> $DIR/unsatisfied-item-lifetime-bound.rs:6:18
5542
|
5643
LL | type Y<'a: 'static>;
5744
| ------------------- definition of `Y` from trait
@@ -60,7 +47,7 @@ LL | type Y<'a> = &'a ();
6047
| ^^^^^^
6148
|
6249
note: lifetime parameter instantiated with the lifetime `'a` as defined here
63-
--> $DIR/unsatisfied-item-lifetime-bound.rs:9:12
50+
--> $DIR/unsatisfied-item-lifetime-bound.rs:6:12
6451
|
6552
LL | type Y<'a> = &'a ();
6653
| ^^
@@ -70,6 +57,6 @@ help: copy the `where` clause predicates from the trait
7057
LL | type Y<'a> = &'a () where 'a: 'static;
7158
| +++++++++++++++++
7259

73-
error: aborting due to 4 previous errors; 1 warning emitted
60+
error: aborting due to 4 previous errors
7461

7562
For more information about this error, try `rustc --explain E0478`.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
#![warn(unused_lifetimes)]
1212

13-
fn test<'a,'b>(x: &'a i32) -> &'b i32
14-
where 'a: 'static //~ WARN unnecessary lifetime parameter `'a`
13+
fn test<'a,'b>(x: &'a i32) -> &'b i32 //~ WARN unnecessary lifetime parameter `'a`
14+
where 'a: 'static
1515
{
1616
x
1717
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:14:11
2+
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:13:9
33
|
4-
LL | where 'a: 'static
5-
| ^^
4+
LL | fn test<'a,'b>(x: &'a i32) -> &'b i32
5+
| ^^
66
|
7-
= help: you can use the `'static` lifetime directly, in place of `'a`
7+
= note: you can use the `'static` lifetime directly, in place of `'a`
88
note: the lint level is defined here
99
--> $DIR/regions-free-region-outlives-static-outlives-free-region.rs:11:9
1010
|

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
#![warn(unused_lifetimes)]
44

55
fn invariant_id<'a,'b>(t: &'b mut &'static ()) -> &'b mut &'a ()
6-
where 'a: 'static { t }
76
//~^ WARN unnecessary lifetime parameter `'a`
7+
where 'a: 'static { t }
88

99
fn static_id<'a>(t: &'a ()) -> &'static ()
10-
where 'a: 'static { t }
1110
//~^ WARN unnecessary lifetime parameter `'a`
11+
where 'a: 'static { t }
1212

1313
fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static ()
14+
//~^ WARN unnecessary lifetime parameter `'a`
15+
//~| WARN unnecessary lifetime parameter `'b`
1416
where 'a: 'b, 'b: 'static { t }
15-
//~^ WARN unnecessary lifetime parameter `'b`
1617

1718
fn ref_id<'a>(t: &'a ()) -> &'a () where 'static: 'a { t }
1819

Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
warning: unnecessary lifetime parameter `'a`
2-
--> $DIR/regions-static-bound-rpass.rs:6:11
2+
--> $DIR/regions-static-bound-rpass.rs:5:17
33
|
4-
LL | where 'a: 'static { t }
5-
| ^^
4+
LL | fn invariant_id<'a,'b>(t: &'b mut &'static ()) -> &'b mut &'a ()
5+
| ^^
66
|
7-
= help: you can use the `'static` lifetime directly, in place of `'a`
7+
= note: you can use the `'static` lifetime directly, in place of `'a`
88
note: the lint level is defined here
99
--> $DIR/regions-static-bound-rpass.rs:3:9
1010
|
1111
LL | #![warn(unused_lifetimes)]
1212
| ^^^^^^^^^^^^^^^^
1313

1414
warning: unnecessary lifetime parameter `'a`
15-
--> $DIR/regions-static-bound-rpass.rs:10:11
15+
--> $DIR/regions-static-bound-rpass.rs:9:14
1616
|
17-
LL | where 'a: 'static { t }
18-
| ^^
17+
LL | fn static_id<'a>(t: &'a ()) -> &'static ()
18+
| ^^
1919
|
20-
= help: you can use the `'static` lifetime directly, in place of `'a`
20+
= note: you can use the `'static` lifetime directly, in place of `'a`
21+
22+
warning: unnecessary lifetime parameter `'a`
23+
--> $DIR/regions-static-bound-rpass.rs:13:23
24+
|
25+
LL | fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static ()
26+
| ^^
27+
|
28+
= note: you can use the `'static` lifetime directly, in place of `'a`
2129

2230
warning: unnecessary lifetime parameter `'b`
23-
--> $DIR/regions-static-bound-rpass.rs:14:19
31+
--> $DIR/regions-static-bound-rpass.rs:13:26
2432
|
25-
LL | where 'a: 'b, 'b: 'static { t }
26-
| ^^
33+
LL | fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static ()
34+
| ^^
2735
|
28-
= help: you can use the `'static` lifetime directly, in place of `'b`
36+
= note: you can use the `'static` lifetime directly, in place of `'b`
2937

30-
warning: 3 warnings emitted
38+
warning: 4 warnings emitted
3139

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

-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
#![warn(unused_lifetimes)]
2-
31
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`
62

73
fn static_id_indirect<'a,'b>(t: &'a ()) -> &'static ()
84
where 'a: 'b, 'b: 'static { t }
9-
//~^ WARN unnecessary lifetime parameter `'b`
105

116
fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
127
t
+4-34
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,13 @@
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-
15-
warning: unnecessary lifetime parameter `'a`
16-
--> $DIR/regions-static-bound.rs:3:53
17-
|
18-
LL | fn static_id<'a,'b>(t: &'a ()) -> &'static () where 'a: 'static { t }
19-
| ^^
20-
|
21-
= help: you can use the `'static` lifetime directly, in place of `'a`
22-
23-
warning: unnecessary lifetime parameter `'b`
24-
--> $DIR/regions-static-bound.rs:8:19
25-
|
26-
LL | where 'a: 'b, 'b: 'static { t }
27-
| ^^
28-
|
29-
= help: you can use the `'static` lifetime directly, in place of `'b`
30-
311
error: lifetime may not live long enough
32-
--> $DIR/regions-static-bound.rs:12:5
2+
--> $DIR/regions-static-bound.rs:7:5
333
|
344
LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
355
| -- lifetime `'a` defined here
366
LL | t
377
| ^ returning this value requires that `'a` must outlive `'static`
388

399
error[E0521]: borrowed data escapes outside of function
40-
--> $DIR/regions-static-bound.rs:17:5
10+
--> $DIR/regions-static-bound.rs:12:5
4111
|
4212
LL | fn error(u: &(), v: &()) {
4313
| - - let's call the lifetime of this reference `'1`
@@ -50,7 +20,7 @@ LL | static_id(&u);
5020
| argument requires that `'1` must outlive `'static`
5121

5222
error[E0521]: borrowed data escapes outside of function
53-
--> $DIR/regions-static-bound.rs:19:5
23+
--> $DIR/regions-static-bound.rs:14:5
5424
|
5525
LL | fn error(u: &(), v: &()) {
5626
| - - let's call the lifetime of this reference `'2`
@@ -63,6 +33,6 @@ LL | static_id_indirect(&v);
6333
| `v` escapes the function body here
6434
| argument requires that `'2` must outlive `'static`
6535

66-
error: aborting due to 3 previous errors; 3 warnings emitted
36+
error: aborting due to 3 previous errors
6737

6838
For more information about this error, try `rustc --explain E0521`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![allow(unused)]
2+
#![deny(unused_lifetimes)]
3+
4+
fn a<'a, 'b>(x: &'a &'b &'a ()) {} //~ ERROR unnecessary lifetime parameter `'b`
5+
6+
fn b<'a: 'b, 'b: 'a>() {} //~ ERROR unnecessary lifetime parameter `'b`
7+
8+
struct Foo<T: 'static>(T);
9+
fn c<'a>(_: Foo<&'a ()>) {} //~ ERROR unnecessary lifetime parameter `'a`
10+
11+
struct Bar<'a>(&'a ());
12+
impl<'a> Bar<'a> {
13+
fn d<'b: 'a>(&'b self) {} //~ ERROR unnecessary lifetime parameter `'b`
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)