-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
A-lintArea: New lintsArea: New lints
Description
What it does
According to the reference, when you create and store a reference to a field access expression, a tuple indexing expression, or an indexing expression, lifetime extension can apply to the entire value inside, even though it's no longer accessible.
Clippy should warn when such code is written where something with a destructor is lifetime-extended but becomes inaccessible.
Advantage
- Run destructor of a value faster, which can avoid unnecessary resource usage or overly-long holding of locks.
- Avoids relying on surpring language semantics.
Drawbacks
- The lint would suggest changing the code in a way that changes what the code does.
- Could cause code to become more verbose.
Example
struct Loud;
impl Drop for Loud {
fn drop(&mut self) {
println!("drop");
}
}
fn make() -> (i32, Loud) {
(1, Loud)
}
fn main() {
let x = &make().0;
println!("{x}");
}Could be written as:
....
fn main() {
let temp = make().0;
let x = &temp;
println!("{x}");
}Note that the first code prints 1 then drop. The second code prints drop then 1.
Comparison with existing lints
No response
Additional Context
There are also some other ways that temporary-lifetime-extended data could become inaccessible. For example:
....
fn main() {
let (ref x, _) = make();
println!("{x}");
}Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lints