Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::redundant_static_lifetimes: constants have by default a `'static` lifetime
What it meansChecks for constants and statics with an explicit 'static lifetime. It is in the style group and warns by default.
What you see
warning: constants have by default a `'static` lifetime --> main.rs:3:13 | 3 | const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] = | -^^^^^^^------------------------------------------------- help: consider removing `'static`: `&[(&'static str, &'static str, fn(&Bar) -> bool)]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#redundant_static_lifetimes = note: `#[warn(clippy::redundant_static_lifetimes)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Adding 'static to every reference can create very complicated types.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
&[...]
static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
&[...]
Write this instead
const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::redundant_static_lifetimes)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
redundant_static_lifetimes = "allow"One run
cargo clippy -- -A clippy::redundant-static-lifetimesThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for constants and statics with an explicit 'static lifetime.
Why is this bad?
Adding 'static to every reference can create very
complicated types.
Example
const FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
&[...]
static FOO: &'static [(&'static str, &'static str, fn(&Bar) -> bool)] =
&[...]
This code can be rewritten as
const FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
static FOO: &[(&str, &str, fn(&Bar) -> bool)] = &[...]
Past names
- const_static_lifetime
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- redundant_field_namesredundant field names in struct initialization
- redundant_patternthe `y @ _` pattern can be written as just `y`
- redundant_pattern_matchingLint for redundant pattern matching over Result, Option, std::task::Poll, std::net::IpAddr or bools
- result_map_or_into_optionChecks for usage of _.map_or(None, Some)
- result_unit_errthis returns a `Result<_, ()>`
- same_item_pushit looks like the same item is being pushed into this `Vec`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?