Skip to content

Lint against overlapping pattern ranges and pattern ranges with small holes caught by _ #63987

Closed
@estebank

Description

@estebank

For both exclusive(nightly) and inclusive range patterns, we don't have any lint about overlapping ranges. We probably should complain about 100..=500 being partially covered already. This would minimize the likelihood of the following off-by-one error going unnoticed:

fn main() {
    let x: u32 = 301;
    match x {
        0..=200 => println!("a"),
        200..=300 => println!("b"),
        300..=400 => println!("c"),
        100..=500 => println!("x"),
        _ => println!("d"),
    }
}

It would also be interesting to have a 1 or 2 value wide holes detection in the presence of a _ pattern to detect confusion between exclusive and inclusive ranges:

#![feature(exclusive_range_pattern)]

fn main() {
    let x: u32 = 301;
    match x {
        0..200 => println!("a"),
        201..300 => println!("b"),
        301..400 => println!("c"),
        _ => println!("d"),
    }
}

We already have range coverage checks when not using _:

error[E0004]: non-exhaustive patterns: `200u32`, `300u32` and `400u32..=std::u32::MAX` not covered
 --> src/main.rs:5:11
  |
5 |     match x {
  |           ^ patterns `200u32`, `300u32` and `400u32..=std::u32::MAX` not covered
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

We might want to just lint against _ and instead ask to be explicit to handle the "small holes" case.

CC #37854 for the exclusive_range_pattern case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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