Open xilexio opened 1 year ago
@rustbot label +I-suggestion-causes-error
Not sure if "error" is the right wording here, but I didn't see an "incorrect suggestion" label
I found a related example where this suggestion does actually cause a compile error because of the lifetime extension, which is arguably still less bad than subtly changing the runtime behavior
trait Blub {
type Iterator<'a>: Iterator<Item=Self::Key> where Self: 'a;
type Key: Copy;
fn keys(&self) -> Self::Iterator<'_>;
fn remove(&mut self, k: Self::Key);
}
impl<T> Blub for Vec<T> {
type Iterator<'a> = std::ops::Range<usize> where Self: 'a;
type Key = usize;
fn keys(&self) -> Self::Iterator<'_> {
0..self.len()
}
fn remove(&mut self, i: usize) {
Vec::remove(self, i);
}
}
fn bla<T: Blub>(blub: &mut T) {
while let Some(i) = (|| blub.keys().next())() {
blub.remove(i);
}
}
fn main() {
let mut blub = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
bla(&mut blub);
println!("blub: {:?}", blub);
}
Summary
Values X in matched expressions in
match X { ... }
,while let ... = X { ... }
,if let ... = X { ... }
have their lifetime extended until the end of match/while-let/if-let. In this context, simplification of some expressions such as(|| code)()
intocode
produces semantically different results - values in the first being dropped at the end of the function and not dropped until the end of match/while-let/if-let in the second. Therefore, clippy should be extra careful about proposing simplifications that involve reducing the number of scopes in this context.Lint Name
redundant_closure_call
Reproducer
I tried this code in clippy 0.1.72 (e6d4725 2023-06-05):
I saw this happen:
I expected to see this happen: No suggestion at all.
Version
Additional Labels
I-suggestion-causes-error