Skip to content

Lint useless or-pattern alternatives under unreachable_patterns - #161613

Draft
iMostfa wants to merge 1 commit into
rust-lang:mainfrom
iMostfa:useless-or-pattern-alternatives
Draft

Lint useless or-pattern alternatives under unreachable_patterns#161613
iMostfa wants to merge 1 commit into
rust-lang:mainfrom
iMostfa:useless-or-pattern-alternatives

Conversation

@iMostfa

@iMostfa iMostfa commented Aug 23, 2026

Copy link
Copy Markdown

rustc warns when an or-pattern alternative is unreachable (_ | 0), but not when it is reachable yet useless (0 | _): removing it wouldn't change the result of the match.

Teach the usefulness algorithm to detect such alternatives and lint them under unreachable_patterns, with a dedicated "useless pattern" message since they are not unreachable. Being semantic rather than syntactic, this also catches what clippy can't: coverage by a union of siblings (0..=1 | 1..=2 | 2..=3) and duplicates in guarded arms (0 | 0 if g). Alternatives that bind variables are not linted, since which alternative matches determines the bound values ((0, x) | (x, _)).

The lint found two real cases in rustc's own sources, fixed here. Existing consumers of rustc_pattern_analysis (rust-analyzer) keep compiling unchanged.

Fixes #160772

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 23, 2026
@iMostfa
iMostfa force-pushed the useless-or-pattern-alternatives branch from 60dab36 to 8dc23e4 Compare August 23, 2026 17:48
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling rustc_metadata v0.0.0 (/checkout/compiler/rustc_metadata)
warning: useless pattern
   --> compiler/rustc_const_eval/src/const_eval/valtrees.rs:201:11
    |
201 |         | ty::Infer(ty::FreshIntTy(_))
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ all the values this pattern matches are already matched by the rest of the or-pattern
...
206 |         | ty::Infer(_)
    |           ------------ matches all the values this pattern does
    |
    = note: `#[warn(unreachable_patterns)]` (part of `#[warn(unused)]`) on by default

warning: useless pattern
   --> compiler/rustc_const_eval/src/const_eval/valtrees.rs:202:11
    |
202 |         | ty::Infer(ty::FreshFloatTy(_))
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ all the values this pattern matches are already matched by the rest of the or-pattern
...
206 |         | ty::Infer(_)
    |           ------------ matches all the values this pattern does

warning: useless pattern
   --> compiler/rustc_const_eval/src/const_eval/valtrees.rs:367:11
    |
367 |         | ty::Infer(ty::FreshIntTy(_))
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ all the values this pattern matches are already matched by the rest of the or-pattern
...
373 |         | ty::Infer(_)
    |           ------------ matches all the values this pattern does

warning: useless pattern
   --> compiler/rustc_const_eval/src/const_eval/valtrees.rs:368:11
    |
368 |         | ty::Infer(ty::FreshFloatTy(_))
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ all the values this pattern matches are already matched by the rest of the or-pattern
...
373 |         | ty::Infer(_)
    |           ------------ matches all the values this pattern does

[RUSTC-TIMING] rustc_middle test:false 139.131

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #161706) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Left side of Or-Patterns is not marked as useless even when covered completely by right side

3 participants