Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dchen5022 opened this issue Nov 3, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@dchen5022
Copy link

dchen5022 commented Nov 3, 2024

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

@dchen5022 dchen5022 added the C-bug Category: Clippy is not doing the correct thing label Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

1 participant