Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert a hard-warning about named static lifetimes into a lint #98079

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,40 @@ declare_lint! {
"detects lifetime parameters that are never used"
}

declare_lint! {
/// The `named_static_lifetimes` lint detects lifetime parameters that are
/// defined as outliving the static lifetime.
///
/// ### Example
///
/// ```rust,compile_fail
/// #[deny(named_static_lifetimes)]
///
/// pub fn foo<'a>(_s: &'a str) where 'a: 'static {}
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// By definition, the static lifetime (`'static`) outlives every other
/// lifetime. If another lifetime `'a` lives at least as long as `'static`,
/// then it is identical to `'static`. In that case, there is no need for
/// the name `'a`, and it likely only makes the code harder to read.
///
/// To fix this, consider using `'static` instead of the named lifetime.
/// Here is the result of doing that in the example above:
///
/// ```rust
/// #[deny(named_static_lifetimes)]
///
/// pub fn foo(_s: &'static str) {}
/// ```
pub NAMED_STATIC_LIFETIMES,
Warn,
"detects lifetime parameters that are identical to `'static`"
}

declare_lint! {
/// The `tyvar_behind_raw_pointer` lint detects raw pointer to an
/// inference variable.
Expand Down Expand Up @@ -3174,6 +3208,7 @@ declare_lint_pass! {
UNCONDITIONAL_RECURSION,
SINGLE_USE_LIFETIMES,
UNUSED_LIFETIMES,
NAMED_STATIC_LIFETIMES,
UNUSED_LABELS,
TYVAR_BEHIND_RAW_POINTER,
ELIDED_LIFETIMES_IN_PATHS,
Expand Down
29 changes: 17 additions & 12 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::middle::resolve_lifetime::*;
use rustc_middle::ty::{self, GenericParamDefKind, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::lint;
use rustc_span::def_id::DefId;
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Span;
Expand Down Expand Up @@ -1255,20 +1256,24 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
continue;
}
this.insert_lifetime(lt, Region::Static);
this.tcx
.sess
.struct_span_warn(
lifetime.span,
&format!(
this.tcx.struct_span_lint_hir(
lint::builtin::NAMED_STATIC_LIFETIMES,
lifetime.hir_id,
lifetime.span,
|lint| {
let msg = &format!(
"unnecessary lifetime parameter `{}`",
lifetime.name.ident(),
),
)
.help(&format!(
"you can use the `'static` lifetime directly, in place of `{}`",
lifetime.name.ident(),
))
.emit();
);
let help = &format!(
"you can use the `'static` lifetime directly, in place of `{}`",
lifetime.name.ident(),
);
lint.build(msg)
.help(help)
Comment on lines +1268 to +1273
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily something to do on this PR, but we have enough context to turn the help into a structured suggestion, by inspecting the bounds and suggesting:

LL -     type Y<'a: 'static>;
LL +     type Y<'static>;
   |            ~~~~~~~

.emit();
},
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | type Y<'a: 'static>;
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

error[E0478]: lifetime bound not satisfied
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/impl-trait/equal-hidden-lifetimes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | fn equal_regions_static<'a: 'static>(x: &'a i32) -> impl Sized {
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

warning: 1 warning emitted
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/issues/issue-30438-c.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'z`
LL | fn silly<'y, 'z>(_s: &'y Test<'z>) -> &'y <Test<'z> as Trait>::Out where 'z: 'static {
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'z`

error[E0515]: cannot return reference to local variable `x`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | where 'a: 'static
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

warning: 1 warning emitted
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-static-bound-rpass.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | where 'a: 'static { t }
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

warning: unnecessary lifetime parameter `'a`
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/regions/regions-static-bound.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | where 'a: 'static { t }
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

warning: unnecessary lifetime parameter `'b`
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/static/static-lifetime-bound.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | fn f<'a: 'static>(_: &'a i32) {}
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

error[E0597]: `x` does not live long enough
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ warning: unnecessary lifetime parameter `'a`
LL | fn f<'a: 'static>(t: &'a str) -> X<'a> {
| ^^
|
= note: `#[warn(named_static_lifetimes)]` on by default
= help: you can use the `'static` lifetime directly, in place of `'a`

error: non-defining opaque type use in defining scope
Expand Down