map_entry
warning for HashMap
suggests code that does not compile
#14224
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
Summary
The
map_entry
clippy warning can suggest code that does not compile. See below.Lint Name
map_entry
Reproducer
If clippy sees an
contains_key
followed by aninsert
onHashMap
, it will automatically (and correctly) suggest usingentry()
instead. However, if doesn't seem to take into account all branches. and can suggest code that does not compile.Note that this code is definitely not idiomatic, but the reason I know about this is that a student of mine (new to Rust) wrote something similar to the above code and got something similar to the following error message:
This suggestion is incorrect because
key
is moved and the other branch tries to borrow&key
, thus this function will not compile.I think the issue might be that if the
insert
following thecontains_key
is in a specific branch, clippy does not consider the other branches.I'm wondering if it is possible to make clippy smart enough to suggest something like this (which would be the "idiomatic" way to write this code):
I'm not super familiar with the internals of clippy but I'm assuming that this might be pretty hard. So maybe instead just make sure that it doesn't suggest code that is never going to compile in this case? Honestly not too sure what the solution should be here.
However, I would still treat this seriously because the entry API is not the most immediately intuitive to new Rust programmers, and I think clippy should do its best to not suggest things that will give new Rustaceans more headaches than they probably already have (though of course I understand this is super hard).
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: