pub static CONST_PATTERNS_WITHOUT_PARTIAL_EQ: &Lint
Expand description

The const_patterns_without_partial_eq lint detects constants that are used in patterns, whose type does not implement PartialEq.

Example

#![deny(const_patterns_without_partial_eq)]

trait EnumSetType {
   type Repr;
}

enum Enum8 { }
impl EnumSetType for Enum8 {
    type Repr = u8;
}

#[derive(PartialEq, Eq)]
struct EnumSet<T: EnumSetType> {
    __enumset_underlying: T::Repr,
}

const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };

fn main() {
    match CONST_SET {
        CONST_SET => { /* ok */ }
        _ => panic!("match fell through?"),
    }
}

{{produces}}

Explanation

Previous versions of Rust accepted constants in patterns, even if those constants’ types did not have PartialEq implemented. The compiler falls back to comparing the value field-by-field. In the future we’d like to ensure that pattern matching always follows PartialEq semantics, so that trait bound will become a requirement for matching on constants.