Skip to content

False Positive in match_same_arms with cfg!() #9775

Closed
@Dr-Emann

Description

@Dr-Emann

Summary

match_same_arms appears to use the expanded value of the cfg! macro: when multiple match arms check for different cfgs, the lint reports that the arms are the same. Just because the values happen to be the same in this execution, doesn't mean they can be collapsed.

Lint Name

match_same_arms

Reproducer

I tried this code:

#[deny(clippy::match_same_arms)]
pub fn is_enabled(i: u32) -> bool {
    match i {
        0 => cfg!(enable_i_0),
        1 => cfg!(enable_i_1),
        2 => cfg!(enable_i_2),
        _ => false,
    }
}

I saw this happen:

error: this match arm has an identical body to another arm
 --> src/lib.rs:6:9
  |
6 |         1 => cfg!(enable_i_1),
  |         -^^^^^^^^^^^^^^^^^^^^
  |         |
  |         help: try merging the arm patterns: `1 | 0`
  |
  = help: or try changing either arm body
note: other arm here
 --> src/lib.rs:5:9
  |
5 |         0 => cfg!(enable_i_0),
  |         ^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(clippy::match_same_arms)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^

error: this match arm has an identical body to another arm
 --> src/lib.rs:7:9
  |
7 |         2 => cfg!(enable_i_2),
  |         -^^^^^^^^^^^^^^^^^^^^
  |         |
  |         help: try merging the arm patterns: `2 | 0`
  |
  = help: or try changing either arm body
note: other arm here
 --> src/lib.rs:5:9
  |
5 |         0 => cfg!(enable_i_0),
  |         ^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms

error: this match arm has an identical body to another arm
 --> src/lib.rs:6:9
  |
6 |         1 => cfg!(enable_i_1),
  |         -^^^^^^^^^^^^^^^^^^^^
  |         |
  |         help: try merging the arm patterns: `1 | 2`
  |
  = help: or try changing either arm body
note: other arm here
 --> src/lib.rs:7:9
  |
7 |         2 => cfg!(enable_i_2),
  |         ^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms

I expected to see this happen:

No error

Version

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: aarch64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions