Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions clippy_lints/src/matches/collapsible_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ fn pat_bindings_moved_or_mutated<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>,
}

let mut candidates = delegate.moved;
if let Some(mutated) = mutated_variables(expr, cx) {
candidates.extend(mutated);
}
let Some(mutated) = mutated_variables(expr, cx) else {
return true;
};
candidates.extend(mutated);

!pat.walk_short(|pat| {
if let PatKind::Binding(_, hir_id, ..) = pat.kind
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/collapsible_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,28 @@ fn issue16705(x: Option<String>) {
_ => false,
};
}

// issue #16864: don't suggest guard when condition mutates pattern binding
fn issue16864() -> Option<u32> {
struct Foo(u32);

impl Foo {
fn mutates(&mut self) -> bool {
self.0 += 1;
self.0.is_multiple_of(2)
}
}

let mut value: Option<Foo> = Some(Foo(42));
// Should NOT lint: inner.mutates() requires &mut self,
// but variables are immutable in match guards.
match &mut value {
Some(inner) => {
if inner.mutates() {
return Some(inner.0);
}
},
_ => {},
}
None
}
Loading