From 77f679430c0453a97fb00b974128b701b853b5ae Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 14 Jan 2024 22:25:00 +0100 Subject: [PATCH] Declare new lint --- compiler/rustc_lint_defs/src/builtin.rs | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 94f8bbe2437..f2560b35aa2 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -67,6 +67,7 @@ declare_lint_pass! { MISSING_FRAGMENT_SPECIFIER, MUST_NOT_SUSPEND, NAMED_ARGUMENTS_USED_POSITIONALLY, + NON_CONTIGUOUS_RANGE_ENDPOINTS, NON_EXHAUSTIVE_OMITTED_PATTERNS, ORDER_DEPENDENT_TRAIT_OBJECTS, OVERLAPPING_RANGE_ENDPOINTS, @@ -812,6 +813,36 @@ declare_lint! { "detects range patterns with overlapping endpoints" } +declare_lint! { + /// The `non_contiguous_range_endpoints` lint detects likely off-by-one errors when using + /// exclusive [range patterns]. + /// + /// [range patterns]: https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns + /// + /// ### Example + /// + /// ```rust + /// # #![feature(exclusive_range_pattern)] + /// let x = 123u32; + /// match x { + /// 0..100 => { println!("small"); } + /// 101..1000 => { println!("large"); } + /// _ => { println!("larger"); } + /// } + /// ``` + /// + /// {{produces}} + /// + /// ### Explanation + /// + /// It is likely a mistake to have range patterns in a match expression that miss out a single + /// number. Check that the beginning and end values are what you expect, and keep in mind that + /// with `..=` the right bound is inclusive, and with `..` it is exclusive. + pub NON_CONTIGUOUS_RANGE_ENDPOINTS, + Warn, + "detects off-by-one errors with exclusive range patterns" +} + declare_lint! { /// The `bindings_with_variant_name` lint detects pattern bindings with /// the same name as one of the matched variants.