Add new lint map_or_same_constant - #17577
Conversation
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
|
☔ The latest upstream changes (possibly #17537) made this pull request unmergeable. Please resolve the merge conflicts. |
| #![warn(clippy::map_or_same_constant)] | ||
| #![allow(clippy::unnecessary_map_or)] | ||
|
|
||
| fn main() { |
There was a problem hiding this comment.
Looking of the code, all of them are covered.
could you add testcases for map_or/map_or_else being:
- macros + proc macro
- constant, but including an side effect like
black_box map_ornot being theOption::map_or, but coming from an trait/impl
| #![warn(clippy::map_or_same_constant)] | ||
| #![allow(clippy::unnecessary_map_or)] | ||
|
|
There was a problem hiding this comment.
Could you expect instead of allow instead?
Also, when do they overlap, can we improve this?
Ideally, the same code should not lint twice, since that sounds like a source for confusion, or?
Summary
This PR adds a new lint
map_or_same_constantin thesuspiciouscategory.It warns when
Option::map_or,Result::map_or, or theirmap_or_elsevariants use the same constant result for both branches (e.g.opt.map_or(false, |_| false)oropt.map_or_else(|| 0, |_| 0)).closes #17544
changelog: [
map_or_same_constant]: Add new lint to check formap_or/map_or_elsewhere both branches return the same constant value