rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.28k stars 1.52k forks source link

Suggest using `opt.is_none_or` instead of `!opt.is_some_and` #13436

Open antonilol opened 6 hours ago

antonilol commented 6 hours ago

What it does

Suggests using opt.is_none_or(...) instead of !opt.is_some_and(...), also suggests to invert the boolean expression inside the closure to keep the two equivalent.

I can imagine some algorithm that will try to invert the expression in the 'nicest' way, converting (just examples)

If a function pointer is passed in, convert it to a closure and put ! in front of it (|args| !function(args)).

Advantage

Drawbacks

Example

!opt.is_some_and(|x| x >= 1000)

Could be written as:

opt.is_none_or(|x| x < 1000)
antonilol commented 6 hours ago

The same can be done for !opt.is_none_or.

Also, Option::is_none_or is stabilized and planned to be included in the 1.82 release, so should not be suggested to be used on version <= 1.81.