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 3 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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4380,6 +4380,7 @@ dependencies = [
"rustc_feature",
"rustc_hir",
"rustc_index",
"rustc_lint_defs",
"rustc_metadata",
"rustc_middle",
"rustc_query_system",
Expand Down
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
1 change: 1 addition & 0 deletions compiler/rustc_resolve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustc_expand = { path = "../rustc_expand" }
rustc_feature = { path = "../rustc_feature" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
Copy link
Contributor

Choose a reason for hiding this comment

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

rustc_resolve typically uses lints through rustc_session::lint, so adding this shouldn't be necessary.

rustc_metadata = { path = "../rustc_metadata" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_session = { path = "../rustc_session" }
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 @@ -16,6 +16,7 @@ use rustc_hir::def_id::{DefIdMap, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{GenericArg, GenericParam, LifetimeName, Node};
use rustc_hir::{GenericParamKind, HirIdMap};
use rustc_lint_defs::builtin::NAMED_STATIC_LIFETIMES;
use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::resolve_lifetime::*;
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(
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