Skip to content

False positive in non_exhaustive_omitted_patterns: matches! #135137

Closed as duplicate of#117304
@dtolnay

Description

@dtolnay
#![feature(non_exhaustive_omitted_patterns_lint)]
#![warn(non_exhaustive_omitted_patterns)]

use core::sync::atomic::Ordering;

pub fn is_relaxed(ordering: Ordering) -> bool {
    matches!(ordering, Ordering::Relaxed)
}
warning: some variants are not matched explicitly
 --> src/lib.rs:7:14
  |
7 |     matches!(ordering, Ordering::Relaxed)
  |              ^^^^^^^^ patterns `std::sync::atomic::Ordering::Release`, `std::sync::atomic::Ordering::Acquire`, `std::sync::atomic::Ordering::AcqRel` and 1 more not covered
  |
  = help: ensure that all variants are matched explicitly by adding the suggested match arms
  = note: the matched value is of type `std::sync::atomic::Ordering` and the `non_exhaustive_omitted_patterns` attribute was found
note: the lint level is defined here
 --> src/lib.rs:2:9
  |
2 | #![warn(non_exhaustive_omitted_patterns)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I believe the lint should not trigger on this code. For comparison, it correctly does not trigger on this alternative implementation of the same function:

pub fn is_relaxed(ordering: Ordering) -> bool {
    if let Ordering::Relaxed = ordering {
        true
    } else {
        false
    }
}

Conceptually, for the purpose of exhaustiveness, matches! more closely resembles if let than match, even if the standard library implementation currently involves match internally in order to support guard expressions.

The non_exhaustive_omitted_patterns should only trigger on this variation of this function.

pub fn is_relaxed(ordering: Ordering) -> bool {
    match ordering {
        Ordering::Relaxed => true,
        _ => false, // warning: non exhaustive omitted patterns
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.F-non_exhaustive_omitted_patterns_lint`#![feature(non_exhaustive_omitted_patterns_lint)]`T-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