Add #[must_use] to {HashMap,HashSet,BTreeMap,BTreeSet}::insert #93107
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I propose adding
#[must_use]
to methodsinsert
ofHashMap
,HashSet
,BTreeMap
,BTreeSet
.Because in my code I always want to check whether such key existed before. And it is easy to forget such check. My current workaround is so: I forbid direct calls to
HashMap::insert
and similar functions usingclippy::disallowed_methods
and wrap this functions using wrappers with#[must_use]
. I propose simply to add#[must_use]
to std.Yes, I am aware about
try_insert
( #82766 ). But this functions is misnamed: prefixtry_
is usually used for functions, which can report allocation error, such asHashMap::try_reserve
. Also, this doesn't fix the problem forHashSet