Skip to content

Commit 77f6794

Browse files
committed
Declare new lint
1 parent a047284 commit 77f6794

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+31
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ declare_lint_pass! {
6767
MISSING_FRAGMENT_SPECIFIER,
6868
MUST_NOT_SUSPEND,
6969
NAMED_ARGUMENTS_USED_POSITIONALLY,
70+
NON_CONTIGUOUS_RANGE_ENDPOINTS,
7071
NON_EXHAUSTIVE_OMITTED_PATTERNS,
7172
ORDER_DEPENDENT_TRAIT_OBJECTS,
7273
OVERLAPPING_RANGE_ENDPOINTS,
@@ -812,6 +813,36 @@ declare_lint! {
812813
"detects range patterns with overlapping endpoints"
813814
}
814815

816+
declare_lint! {
817+
/// The `non_contiguous_range_endpoints` lint detects likely off-by-one errors when using
818+
/// exclusive [range patterns].
819+
///
820+
/// [range patterns]: https://doc.rust-lang.org/nightly/reference/patterns.html#range-patterns
821+
///
822+
/// ### Example
823+
///
824+
/// ```rust
825+
/// # #![feature(exclusive_range_pattern)]
826+
/// let x = 123u32;
827+
/// match x {
828+
/// 0..100 => { println!("small"); }
829+
/// 101..1000 => { println!("large"); }
830+
/// _ => { println!("larger"); }
831+
/// }
832+
/// ```
833+
///
834+
/// {{produces}}
835+
///
836+
/// ### Explanation
837+
///
838+
/// It is likely a mistake to have range patterns in a match expression that miss out a single
839+
/// number. Check that the beginning and end values are what you expect, and keep in mind that
840+
/// with `..=` the right bound is inclusive, and with `..` it is exclusive.
841+
pub NON_CONTIGUOUS_RANGE_ENDPOINTS,
842+
Warn,
843+
"detects off-by-one errors with exclusive range patterns"
844+
}
845+
815846
declare_lint! {
816847
/// The `bindings_with_variant_name` lint detects pattern bindings with
817848
/// the same name as one of the matched variants.

0 commit comments

Comments
 (0)