Skip to content

Add new lint map_or_same_constant - #17577

Draft
ivanlomeli wants to merge 1 commit into
rust-lang:masterfrom
ivanlomeli:map_or_same_constant
Draft

Add new lint map_or_same_constant#17577
ivanlomeli wants to merge 1 commit into
rust-lang:masterfrom
ivanlomeli:map_or_same_constant

Conversation

@ivanlomeli

@ivanlomeli ivanlomeli commented Aug 17, 2026

Copy link
Copy Markdown

Summary

This PR adds a new lint map_or_same_constant in the suspicious category.
It warns when Option::map_or, Result::map_or, or their map_or_else variants use the same constant result for both branches (e.g. opt.map_or(false, |_| false) or opt.map_or_else(|| 0, |_| 0)).

closes #17544


changelog: [map_or_same_constant]: Add new lint to check for map_or / map_or_else where both branches return the same constant value

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 17, 2026
@rustbot

rustbot commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 17, 2026
@rustbot

rustbot commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@ivanlomeli
ivanlomeli marked this pull request as draft August 17, 2026 10:54
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 17, 2026
@rustbot

rustbot commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #17537) made this pull request unmergeable. Please resolve the merge conflicts.

@CommanderStorm CommanderStorm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code wise mostly LGTM, but there is some missing test coverage for the cases you covered 😉

The actual thing that we need to work out clearer it seems is how it interacts with unnecessary_map_or.

View changes since this review

#![warn(clippy::map_or_same_constant)]
#![allow(clippy::unnecessary_map_or)]

fn main() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_or not being the Option::map_or, but coming from an trait/impl

Comment on lines +1 to +3
#![warn(clippy::map_or_same_constant)]
#![allow(clippy::unnecessary_map_or)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint map_or branches that return the same constant

3 participants