Skip to content

Commit 9b27e4e

Browse files
jeremydavis519LittleFall
authored andcommitted
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`
1 parent 940d00f commit 9b27e4e

File tree

10 files changed

+61
-13
lines changed

10 files changed

+61
-13
lines changed

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

Lines changed: 18 additions & 13 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_lifetime::*;
2020
use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeSuperVisitable, TypeVisitor};
21+
use rustc_session::lint;
2122
use rustc_span::def_id::DefId;
2223
use rustc_span::symbol::{sym, Ident};
2324
use rustc_span::Span;
@@ -921,20 +922,24 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
921922
continue;
922923
}
923924
this.insert_lifetime(lt, Region::Static);
924-
this.tcx
925-
.sess
926-
.struct_span_warn(
927-
lifetime.ident.span,
928-
&format!(
925+
this.tcx.struct_span_lint_hir(
926+
lint::builtin::NAMED_STATIC_LIFETIMES,
927+
lifetime.hir_id,
928+
lifetime.span,
929+
|lint| {
930+
let msg = &format!(
929931
"unnecessary lifetime parameter `{}`",
930-
lifetime.ident,
931-
),
932-
)
933-
.help(&format!(
934-
"you can use the `'static` lifetime directly, in place of `{}`",
935-
lifetime.ident,
936-
))
937-
.emit();
932+
lifetime.name.ident(),
933+
);
934+
let help = &format!(
935+
"you can use the `'static` lifetime directly, in place of `{}`",
936+
lifetime.name.ident(),
937+
);
938+
lint.build(msg)
939+
.help(help)
940+
.emit();
941+
},
942+
);
938943
}
939944
}
940945
}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,40 @@ declare_lint! {
16631663
"detects lifetime parameters that are never used"
16641664
}
16651665

1666+
declare_lint! {
1667+
/// The `named_static_lifetimes` lint detects lifetime parameters that are
1668+
/// defined as outliving the static lifetime.
1669+
///
1670+
/// ### Example
1671+
///
1672+
/// ```rust,compile_fail
1673+
/// #[deny(named_static_lifetimes)]
1674+
///
1675+
/// pub fn foo<'a>(_s: &'a str) where 'a: 'static {}
1676+
/// ```
1677+
///
1678+
/// {{produces}}
1679+
///
1680+
/// ### Explanation
1681+
///
1682+
/// By definition, the static lifetime (`'static`) outlives every other
1683+
/// lifetime. If another lifetime `'a` lives at least as long as `'static`,
1684+
/// then it is identical to `'static`. In that case, there is no need for
1685+
/// the name `'a`, and it likely only makes the code harder to read.
1686+
///
1687+
/// To fix this, consider using `'static` instead of the named lifetime.
1688+
/// Here is the result of doing that in the example above:
1689+
///
1690+
/// ```rust
1691+
/// #[deny(named_static_lifetimes)]
1692+
///
1693+
/// pub fn foo(_s: &'static str) {}
1694+
/// ```
1695+
pub NAMED_STATIC_LIFETIMES,
1696+
Warn,
1697+
"detects lifetime parameters that are identical to `'static`"
1698+
}
1699+
16661700
declare_lint! {
16671701
/// The `tyvar_behind_raw_pointer` lint detects raw pointer to an
16681702
/// inference variable.
@@ -3321,6 +3355,7 @@ declare_lint_pass! {
33213355
UNCONDITIONAL_RECURSION,
33223356
SINGLE_USE_LIFETIMES,
33233357
UNUSED_LIFETIMES,
3358+
NAMED_STATIC_LIFETIMES,
33243359
UNUSED_LABELS,
33253360
TYVAR_BEHIND_RAW_POINTER,
33263361
ELIDED_LIFETIMES_IN_PATHS,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | type Y<'a: 'static>;
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
error[E0478]: lifetime bound not satisfied

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | fn equal_regions_static<'a: 'static>(x: &'a i32) -> impl Sized {
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
warning: 1 warning emitted

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'z`
44
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'z`
89

910
error[E0515]: cannot return reference to local variable `x`

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | where 'a: 'static
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
warning: 1 warning emitted

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | where 'a: 'static { t }
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
warning: unnecessary lifetime parameter `'a`

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | where 'a: 'static { t }
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
warning: unnecessary lifetime parameter `'b`

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | fn f<'a: 'static>(_: &'a i32) {}
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
error[E0597]: `x` does not live long enough

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
44
LL | fn f<'a: 'static>(t: &'a str) -> X<'a> {
55
| ^^
66
|
7+
= note: `#[warn(named_static_lifetimes)]` on by default
78
= help: you can use the `'static` lifetime directly, in place of `'a`
89

910
error: non-defining opaque type use in defining scope

0 commit comments

Comments
 (0)