-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Warn for overwriting HashMap entry #11978
Labels
A-lint
Area: New lints
Comments
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What it does
Inserting on a HashMap on a key that is already contained overwrites the value that was at that location.
If the return value of insert (
Option<T>
; what was previously there) is not used, that value is lost.While it is practically impossible to warn for all cases where this might occur, a few might be caught.
I suggest checking for an
insert
(orget_mut().unwrap()
) followed by anotherinsert
which throws away the return value, while there weren't any reads on the HashMap in between.Advantage
Drawbacks
In some situations overwriting the old value is exactly the plan and this warn might have false positives in these kinds of situations.
This also opens the discussion whether
HashMap::insert
's result should bemust use
, where the ease of use stands against potential errors like the loss of data as explained above.Example
The text was updated successfully, but these errors were encountered: