Skip to content

Clippy fix makes breaking change on warning: usage of contains_key followed by insert on a HashMap #13649

Open
@dchen5022

Description

@dchen5022

Summary

Clippy attempts to fix the warning: usage of contains_key followed by insert on a HashMap
and creates a borrow after move.

Original:

let name = format!("{}/{}", sensor_driver.namespace, sensor_driver.function);
if sensor_drivers.contains_key(&name) {
    res.error(format!("Duplicate sensor driver {} in drivers file.", name));
} else {
    sensor_drivers.insert(name, sensor_driver);
}

Clippy Fixed:

let name = format!("{}/{}", sensor_driver.namespace, sensor_driver.function);
if let std::collections::hash_map::Entry::Vacant(e) = sensor_drivers.entry(name) {
  e.insert(sensor_driver);
} else {
  res.error(format!("Duplicate sensor driver {} in drivers file.", name));
}

The Clippy generated code results in name being moved by HashMap::Entry before being borrowed by format!.

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions