-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Summary
Hey! I'm a Rust newbie and I'm working on porting an existing Python library to Rust. I tried running clippy on the project, it was able to improve some code but it seems to have choked when I tried to run it after some changes were applied. The tool told me to open an issue on https://github.com/rust-lang/rust/issues, but that page has a link specifically for clippy bugs, so here I am.
Reproducer
The specific repository and commit that should be able to reproduce the issue: Decompollaborate/mapfile_parser@5a3f70c
Link to pastebin containing full output of clippy: https://pastebin.com/WG20z4Ay
My understanding is the following code is the culprit (permalink)
let mut aux_dict = HashMap::new();
// ...
let mut path: PathBuf;
// ...
if !aux_dict.contains_key(&path) {
aux_dict.insert(path, vec![file]);
} else {
aux_dict.get_mut(&path).unwrap().push(file);
}
clippy tried to convert it to some very verbose Entry::Vacant
, but it failed in the process
I was told either one of the following would be an ideal way of writing the insertion:
match aux_dict.entry(path) {
Entry::Vacant(e) => {
e.insert(vec![file]);
}
Entry::Occupied(e) => {
e.into_mut().push(file);
}
}
or
aux_dict.entry(path).or_default().push(file);
Version
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2
Additional Labels
No response