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

Detect `.map_or(false, f)` in `manual_is_variant_and` lint

Open samueltardieu opened this issue 1 year ago • 7 comments

.map_or(false, f) on a Option or a Result was not detected by manual_is_variant_and.

This change is small (first commit), but induces a lot of (rightful) fixes (second commit) in Clippy sources.

changelog: [manual_is_variant_and]: add detection of .map_or(false, f) pattern

samueltardieu avatar Oct 06 '24 11:10 samueltardieu

r? @Manishearth

rustbot has assigned @Manishearth. 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 Oct 06 '24 11:10 rustbot

FWIW, this is also being implemented in #11796, though as a new lint and not part of manual_is_variant_and since the PR predates it

y21 avatar Oct 06 '24 15:10 y21

FWIW, this is also being implemented in #11796, though as a new lint and not part of manual_is_variant_and since the PR predates it

Oh. So what do we do? Although I think this check rather belongs to manual_is_variant_of and should be machine applicable, I don't mind dropping it if @Jacherr prefer to finalize their PR instead.

samueltardieu avatar Oct 06 '24 15:10 samueltardieu

Moreover, @Jacherr PR could be retargeted to catch instances of .is_some_and(|x| x == V) (or .is_ok_and(|x| x == V)) and propose rewriting them as == Some(V) or == Ok(V). It could be applied in some cases after the current PR, and will catch more cases of code already using .is_some_and() and .is_ok_and().

samueltardieu avatar Oct 06 '24 15:10 samueltardieu

The existing PR is pretty far through the review process and as far as I know just needs a few more changes before being ready for merging. I just need time to get round to it!

As is, I’m not sure there’s much benefit complicating the situation even further with that PR because it’s been open for almost a year now (oops..) As for these improvements, they can probably be retroactively applied after the fact (either by myself or you).

Jacherr avatar Oct 06 '24 19:10 Jacherr

Fine with me, I'll keep this PR in my own version for the time being so that I can benefit from the lint (I detected some usages of .map_or(false, …) in some of my code and got it fixed using it) until your PR is merged, and I'll close it when your is in.

samueltardieu avatar Oct 06 '24 20:10 samueltardieu

On vacation for a week. Feel free to retarget review with r? clippy

Manishearth avatar Oct 07 '24 04:10 Manishearth