rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Add new lint `hashset_insert_after_contains`

Open lochetti opened this issue 1 year ago • 3 comments

This PR closes https://github.com/rust-lang/rust-clippy/issues/11103.

This is my first PR creating a new lint (and the second attempt of creating this PR, the first one I was not able to continue because of personal reasons). Thanks for the patience :)

The idea of the lint is to find insert in hashmanps inside if staments that are checking if the hashmap contains the same value that is being inserted. This is not necessary since you could simply call the insert and check for the bool returned if you still need the if statement.

changelog: new lint: [hashset_insert_after_contains]

lochetti avatar May 31 '24 16:05 lochetti

r? @llogiq

rustbot has assigned @llogiq. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar May 31 '24 16:05 rustbot

:umbrella: The latest upstream changes (presumably #12849) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jun 11 '24 23:06 bors

Sorry this is taking so long, I am unfortunately quite busy at the moment. I'll ping you once I've run the check so you don't need to rebase more than once.

llogiq avatar Jun 15 '24 11:06 llogiq

I finally came around to do a lintcheck run, and it looked OK. So r=me after a rebase and the docs fix @bitfield suggested.

llogiq avatar Jul 03 '24 09:07 llogiq

@bors r=llogiq

lochetti avatar Jul 03 '24 19:07 lochetti

@lochetti: :key: Insufficient privileges: Not in reviewers

bors avatar Jul 03 '24 19:07 bors

I finally came around to do a lintcheck run, and it looked OK. So r=me after a rebase and the docs fix @bitfield suggested.

Oops. It looks like I can't r=you... :)

lochetti avatar Jul 03 '24 19:07 lochetti

No problem.

@bors r+

llogiq avatar Jul 04 '24 05:07 llogiq

:pushpin: Commit 4e71fc4302898e28d7e4c570edf2f3d1feffe116 has been approved by llogiq

It is now in the queue for this repository.

bors avatar Jul 04 '24 05:07 bors

:hourglass: Testing commit 4e71fc4302898e28d7e4c570edf2f3d1feffe116 with merge d2400a49a43669025c37031791c1d200852ef160...

bors avatar Jul 04 '24 05:07 bors

:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: llogiq Pushing d2400a49a43669025c37031791c1d200852ef160 to master...

bors avatar Jul 04 '24 05:07 bors

Awesome work! One question -- would it make sense to generalize the name of this lint to insert_after_contains ? This way it can also cover HashMap and possibly other dictionary-like patterns?

nyurik avatar Jul 04 '24 07:07 nyurik

@nyurik: Currently the lint only catches HashMaps, but I'd welcome a PR to change that along with the name.

llogiq avatar Jul 04 '24 19:07 llogiq

I could do a quick PR to rename the lint, but implementing it for other types might take a bit longer, and might not even make the cutoff for the next release (at which point the lint name would become permanent)

nyurik avatar Jul 05 '24 16:07 nyurik

Oh, my apologies, this was already renamed in the code to set_contains_or_insert. Some options of a generalized name:

  • contains_or_insert (short and to the point?)
  • collection_contains_or_insert
  • ???

nyurik avatar Jul 05 '24 16:07 nyurik

Renamed in #13053

nyurik avatar Jul 05 '24 17:07 nyurik