Skip to content

indirect_structural_match lint misfiring on enum variant unused in const item #62614

Closed
@pnkfelix

Description

@pnkfelix

(Spawned off of #62411 (comment))

Consider this code (play):

struct Sum(u32, u32);

impl PartialEq for Sum {
    fn eq(&self, other: &Self) -> bool { self.0 + self.1 == other.0 + other.1 }
}

impl Eq for Sum { }

#[derive(PartialEq, Eq)]
enum Eek {
    TheConst,
    UnusedByTheConst(Sum)
}

const THE_CONST: Eek = Eek::TheConst;

pub fn main() {
    match Eek::UnusedByTheConst(Sum(1,2)) {
        THE_CONST => { println!("Hello"); }
        _ => { println!("Gbye"); }
    }
}

In stable and beta, it compiles without warning.

In nightly, it compiles with the following diagnostic warning (#62411):

warning: to use a constant of type `Sum` in a pattern, `Sum` must be annotated with `#[derive(PartialEq, Eq)]`
  --> src/main.rs:19:9
   |
19 |         THE_CONST => { println!("Hello"); }
   |         ^^^^^^^^^
   |
   = note: #[warn(indirect_structural_match)] on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>

This is probably not what we want for this lint: It is entirely reasonable for the user to assume that the structural_match check is based solely on the ADT's that one encounters when traversing the const expression itself, and one should ignore any unused enum variants that are only tangentially related to the const.

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions