-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Clippy fix for "contains_key
followed by insert
" produces "borrow of moved value" error
#9925
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
Comments
This issue still exists. Maybe |
The following simple program reproduces this in use std::collections::HashMap;
fn main() {
let mut hm:HashMap<String,bool> = HashMap::new();
let key = "hello".to_string();
if hm.contains_key(&key) {
let bval = hm.get_mut(&key).unwrap();
*bval = false;
} else {
hm.insert(key, true);
}
} causing this error after fix:
As @maxi0604 suggested, perhaps if the key is not |
When running
cargo clippy --fix
The code fails because I use the thing I test for in the hash map in the else. This seems fairly sensible so I am surprised this wasn't picked up already. Maybe I'm missing something silly.
Ether way the output suggested creating a issue so I have done
Meta
rustc --version --verbose
:To reproduce clone https://gitlab.com/girderstream/girderstream/-/commit/edc55aa923ad4fdb51e4494998c4b681b6fcb260 and run
cargo clippy --fix
This is the diff clippy suggests with
cargo clippy --fix --broken-code
https://gitlab.com/girderstream/girderstream/-/commit/f7cbc3b097d859805d1f27b56b0973fb3004e963which can be fixed with https://gitlab.com/girderstream/girderstream/-/commit/83422f677efb4eba8eb7a54da5cecc6022992d38 but is not always a good idea to add a
clone
.Its a shame you cant pass in a borrow to HashMap.entry()
The text was updated successfully, but these errors were encountered: