Clippy gives incorrect suggestion of entry api when using contains_key followed by insert. The suggestion given causes compilation error because of key being used by else block which has already got moved when entry method is called in the if condition.
warning: usage of `contains_key` followed by `insert` on a `HashMap`
--> src/main.rs:18:9
|
18 | / if self.values.contains_key(&name) {
19 | | self.values.insert(name, value);
20 | | true
21 | | } else if let Some(enclosing) = &mut self.enclosing {
... |
24 | | false
25 | | }
| |_________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
= note: `#[warn(clippy::map_entry)]` on by default
help: try
|
18 ~ if let std::collections::hash_map::Entry::Occupied(mut e) = self.values.entry(name) {
19 + e.insert(value);
20 + true
21 + } else if let Some(enclosing) = &mut self.enclosing {
22 + enclosing.assign(name, value)
23 + } else {
24 + false
25 + }
|
Accepting this suggestion using --fix gives error.
after fixes were automatically applied the compiler reported errors within these files:
* src/main.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0382]: use of moved value: `name`
--> src/main.rs:22:30
|
17 | fn assign(&mut self, name: String, value: usize) -> bool {
| ---- move occurs because `name` has type `std::string::String`, which does not implement the `Copy` trait
18 | if let std::collections::hash_map::Entry::Occupied(mut e) = self.values.entry(name) {
| ---- value moved here
...
22 | enclosing.assign(name, value)
| ^^^^ value used here after move
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.
Summary
Clippy gives incorrect suggestion of
entry
api when usingcontains_key
followed byinsert
. The suggestion given causes compilation error because of key being used by else block which has already got moved whenentry
method is called in the if condition.Reproducer
When running clippy with the following input:
gives:
Accepting this suggestion using
--fix
gives error.Version
Additional Labels
No response