Skip to content

Commit c905503

Browse files
committed
fix: panicking_unwrap FP on field access with implicit deref
1 parent 65c339e commit c905503

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clippy_lints/src/unwrap.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,19 @@ impl Local {
180180
field_indices,
181181
..
182182
} => {
183+
let field_projections = place
184+
.projections
185+
.iter()
186+
.filter(|proj| matches!(proj.kind, ProjectionKind::Field(_, _)))
187+
.collect::<Vec<_>>();
183188
is_potentially_local_place(*local_id, place)
184189
// If there were projections other than field projections, err on the side of caution and say that they
185190
// _might_ be mutating something.
186191
//
187192
// The reason we use `<=` and not `==` is that a mutation of `struct` or `struct.field1` should count as
188193
// mutation of the child fields such as `struct.field1.field2`
189-
&& place.projections.len() <= field_indices.len()
190-
&& iter::zip(&place.projections, field_indices.iter().copied().rev()).all(|(proj, field_idx)| {
194+
&& field_projections.len() <= field_indices.len()
195+
&& iter::zip(&field_projections, field_indices.iter().copied().rev()).all(|(proj, field_idx)| {
191196
match proj.kind {
192197
ProjectionKind::Field(f_idx, _) => f_idx == field_idx,
193198
// If this is a projection we don't expect, it _might_ be mutating something

tests/ui/checked_unwrap/simple_conditionals.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,22 @@ fn issue15321() {
472472
//~^ unnecessary_unwrap
473473
}
474474
}
475+
476+
mod issue16188 {
477+
struct Foo {
478+
value: Option<i32>,
479+
}
480+
481+
impl Foo {
482+
pub fn bar(&mut self) {
483+
let print_value = |v: i32| {
484+
println!("{}", v);
485+
};
486+
487+
if self.value.is_none() {
488+
self.value = Some(10);
489+
print_value(self.value.unwrap());
490+
}
491+
}
492+
}
493+
}

0 commit comments

Comments
 (0)