Skip to content

fp: set-contains-or-insert where set is mutated between contains and insert #15990

@edwloef

Description

@edwloef

Summary

The lint probably shouldn't fire if the set is mutated between the contains and the insert call in general.

Lint Name

set-contains-or-insert

Reproducer

I tried this code:

use std::collections::HashSet;

fn test(s: &mut HashSet<usize>, v: usize) {
    if !s.contains(&v) {
        s.clear();
        s.insert(v);
    }
}

I saw this happen:

warning: usage of `HashSet::insert` after `HashSet::contains`
 --> src/lib.rs:4:11
  |
4 |     if !s.contains(&v) {
  |           ^^^^^^^^^^^^
5 |         s.clear();
6 |         s.insert(v);
  |           ^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#set_contains_or_insert
  = note: `-W clippy::set-contains-or-insert` implied by `-W clippy::nursery`
  = help: to override `-W clippy::nursery` add `#[allow(clippy::set_contains_or_insert)]`

I expected to see this happen: no output

Version

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions