From 91a3db95a1a5939054d72d2ce69c5647c3cfb93b Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 29 Aug 2019 16:06:44 -0700 Subject: Add check for overlapping ranges to unreachable patterns lint --- src/test/ui/check_match/issue-43253.rs | 17 +++++---- src/test/ui/check_match/issue-43253.stderr | 42 +++++++++++++++++++--- src/test/ui/exhaustive_integer_patterns.rs | 8 ++--- src/test/ui/exhaustive_integer_patterns.stderr | 32 +++++++++++++---- src/test/ui/match/match-range-fail-dominate.rs | 24 +++++++------ src/test/ui/match/match-range-fail-dominate.stderr | 40 ++++++++++++--------- src/test/ui/precise_pointer_size_matching.rs | 4 +-- src/test/ui/precise_pointer_size_matching.stderr | 24 ++++++++++++- 8 files changed, 138 insertions(+), 53 deletions(-) (limited to 'src/test/ui') diff --git a/src/test/ui/check_match/issue-43253.rs b/src/test/ui/check_match/issue-43253.rs index a4d6e9b777f..548300da034 100644 --- a/src/test/ui/check_match/issue-43253.rs +++ b/src/test/ui/check_match/issue-43253.rs @@ -1,4 +1,4 @@ -// build-pass (FIXME(62277): could be check-pass?) +// check-pass #![feature(exclusive_range_pattern)] #![warn(unreachable_patterns)] @@ -13,7 +13,7 @@ fn main() { match 10 { 1..10 => {}, - 9..=10 => {}, + 9..=10 => {}, //~ WARNING multiple patterns covering the same range _ => {}, } @@ -23,22 +23,25 @@ fn main() { _ => {}, } - // These cases should generate an "unreachable pattern" warning. + // These cases should generate "unreachable pattern" warnings. match 10 { 1..10 => {}, - 9 => {}, + 9 => {}, //~ WARNING unreachable pattern _ => {}, } match 10 { 1..10 => {}, - 8..=9 => {}, + 8..=9 => {}, //~ WARNING multiple patterns covering the same range _ => {}, } match 10 { - 1..10 => {}, - 9..=9 => {}, + 5..7 => {}, + 6 => {}, //~ WARNING unreachable pattern + 1..10 => {}, //~ WARNING multiple patterns covering the same range + 9..=9 => {}, //~ WARNING unreachable pattern + 6 => {}, //~ WARNING unreachable pattern _ => {}, } } diff --git a/src/test/ui/check_match/issue-43253.stderr b/src/test/ui/check_match/issue-43253.stderr index d961f623e1f..24dfa52beb6 100644 --- a/src/test/ui/check_match/issue-43253.stderr +++ b/src/test/ui/check_match/issue-43253.stderr @@ -1,8 +1,10 @@ -warning: unreachable pattern - --> $DIR/issue-43253.rs:29:9 +warning: multiple patterns covering the same range + --> $DIR/issue-43253.rs:16:9 | -LL | 9 => {}, - | ^ +LL | 1..10 => {}, + | ----- this range overlaps on `9i32` +LL | 9..=10 => {}, + | ^^^^^^ overlapping patterns | note: lint level defined here --> $DIR/issue-43253.rs:4:9 @@ -11,14 +13,44 @@ LL | #![warn(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ warning: unreachable pattern + --> $DIR/issue-43253.rs:29:9 + | +LL | 9 => {}, + | ^ + +warning: multiple patterns covering the same range --> $DIR/issue-43253.rs:35:9 | +LL | 1..10 => {}, + | ----- this range overlaps on `8i32..=9i32` LL | 8..=9 => {}, - | ^^^^^ + | ^^^^^ overlapping patterns warning: unreachable pattern --> $DIR/issue-43253.rs:41:9 | +LL | 6 => {}, + | ^ + +warning: multiple patterns covering the same range + --> $DIR/issue-43253.rs:42:9 + | +LL | 5..7 => {}, + | ---- this range overlaps on `5i32..=6i32` +LL | 6 => {}, + | - this range overlaps on `6i32` +LL | 1..10 => {}, + | ^^^^^ overlapping patterns + +warning: unreachable pattern + --> $DIR/issue-43253.rs:43:9 + | LL | 9..=9 => {}, | ^^^^^ +warning: unreachable pattern + --> $DIR/issue-43253.rs:44:9 + | +LL | 6 => {}, + | ^ + diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs index 2570bc8a560..ce2b44b8ef1 100644 --- a/src/test/ui/exhaustive_integer_patterns.rs +++ b/src/test/ui/exhaustive_integer_patterns.rs @@ -19,7 +19,7 @@ fn main() { 0 ..= 32 => {} 33 => {} 34 .. 128 => {} - 100 ..= 200 => {} + 100 ..= 200 => {} //~ ERROR multiple patterns covering the same range 200 => {} //~ ERROR unreachable pattern 201 ..= 255 => {} } @@ -41,7 +41,7 @@ fn main() { match x { //~ ERROR non-exhaustive patterns -7 => {} -5..=120 => {} - -2..=20 => {} //~ ERROR unreachable pattern + -2..=20 => {} //~ ERROR multiple patterns covering the same range 125 => {} } @@ -135,9 +135,9 @@ fn main() { (125 .. 128, false) => {} } - match 0u8 { // ok + match 0u8 { 0 .. 2 => {} - 1 ..= 2 => {} + 1 ..= 2 => {} //~ ERROR multiple patterns covering the same range _ => {} } diff --git a/src/test/ui/exhaustive_integer_patterns.stderr b/src/test/ui/exhaustive_integer_patterns.stderr index 6c4b7b0cc03..ef3faceaa7e 100644 --- a/src/test/ui/exhaustive_integer_patterns.stderr +++ b/src/test/ui/exhaustive_integer_patterns.stderr @@ -1,8 +1,10 @@ -error: unreachable pattern - --> $DIR/exhaustive_integer_patterns.rs:23:9 +error: multiple patterns covering the same range + --> $DIR/exhaustive_integer_patterns.rs:22:9 | -LL | 200 => {} - | ^^^ +LL | 34 .. 128 => {} + | --------- this range overlaps on `100u8..=127u8` +LL | 100 ..= 200 => {} + | ^^^^^^^^^^^ overlapping patterns | note: lint level defined here --> $DIR/exhaustive_integer_patterns.rs:4:9 @@ -10,6 +12,12 @@ note: lint level defined here LL | #![deny(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ +error: unreachable pattern + --> $DIR/exhaustive_integer_patterns.rs:23:9 + | +LL | 200 => {} + | ^^^ + error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered --> $DIR/exhaustive_integer_patterns.rs:28:11 | @@ -26,11 +34,13 @@ LL | match x { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: unreachable pattern +error: multiple patterns covering the same range --> $DIR/exhaustive_integer_patterns.rs:44:9 | +LL | -5..=120 => {} + | -------- this range overlaps on `-2i8..=20i8` LL | -2..=20 => {} - | ^^^^^^^ + | ^^^^^^^ overlapping patterns error[E0004]: non-exhaustive patterns: `std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered --> $DIR/exhaustive_integer_patterns.rs:41:11 @@ -80,6 +90,14 @@ LL | match (0u8, true) { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms +error: multiple patterns covering the same range + --> $DIR/exhaustive_integer_patterns.rs:140:9 + | +LL | 0 .. 2 => {} + | ------ this range overlaps on `1u8` +LL | 1 ..= 2 => {} + | ^^^^^^^ overlapping patterns + error[E0004]: non-exhaustive patterns: `std::u128::MAX` not covered --> $DIR/exhaustive_integer_patterns.rs:145:11 | @@ -104,6 +122,6 @@ LL | match 0u128 { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 13 previous errors +error: aborting due to 15 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/match/match-range-fail-dominate.rs b/src/test/ui/match/match-range-fail-dominate.rs index a0cc773d20e..4c0f57ffbee 100644 --- a/src/test/ui/match/match-range-fail-dominate.rs +++ b/src/test/ui/match/match-range-fail-dominate.rs @@ -1,39 +1,41 @@ -//error-pattern: unreachable -//error-pattern: unreachable -//error-pattern: unreachable -//error-pattern: unreachable -//error-pattern: unreachable - #![deny(unreachable_patterns)] fn main() { match 5 { 1 ..= 10 => { } - 5 ..= 6 => { } + 5 ..= 6 => { } //~ ERROR multiple patterns covering the same range _ => {} }; match 5 { 3 ..= 6 => { } - 4 ..= 6 => { } + 4 ..= 6 => { } //~ ERROR multiple patterns covering the same range _ => {} }; match 5 { 4 ..= 6 => { } - 4 ..= 6 => { } + 4 ..= 6 => { } //~ ERROR multiple patterns covering the same range _ => {} }; match 'c' { 'A' ..= 'z' => {} - 'a' ..= 'z' => {} + 'a' ..= 'z' => {} //~ ERROR multiple patterns covering the same range _ => {} }; match 1.0f64 { 0.01f64 ..= 6.5f64 => {} - 0.02f64 => {} + //~^ WARNING floating-point types cannot be used in patterns + //~| WARNING floating-point types cannot be used in patterns + //~| WARNING floating-point types cannot be used in patterns + //~| WARNING this was previously accepted by the compiler + //~| WARNING this was previously accepted by the compiler + //~| WARNING this was previously accepted by the compiler + 0.02f64 => {} //~ ERROR unreachable pattern + //~^ WARNING floating-point types cannot be used in patterns + //~| WARNING this was previously accepted by the compiler _ => {} }; } diff --git a/src/test/ui/match/match-range-fail-dominate.stderr b/src/test/ui/match/match-range-fail-dominate.stderr index d0ff4930a45..af667be317d 100644 --- a/src/test/ui/match/match-range-fail-dominate.stderr +++ b/src/test/ui/match/match-range-fail-dominate.stderr @@ -1,35 +1,43 @@ -error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:12:7 +error: multiple patterns covering the same range + --> $DIR/match-range-fail-dominate.rs:6:7 | +LL | 1 ..= 10 => { } + | -------- this range overlaps on `5i32..=6i32` LL | 5 ..= 6 => { } - | ^^^^^^^ + | ^^^^^^^ overlapping patterns | note: lint level defined here - --> $DIR/match-range-fail-dominate.rs:7:9 + --> $DIR/match-range-fail-dominate.rs:1:9 | LL | #![deny(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ -error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:18:7 +error: multiple patterns covering the same range + --> $DIR/match-range-fail-dominate.rs:12:7 | +LL | 3 ..= 6 => { } + | ------- this range overlaps on `4i32..=6i32` LL | 4 ..= 6 => { } - | ^^^^^^^ + | ^^^^^^^ overlapping patterns -error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:24:7 +error: multiple patterns covering the same range + --> $DIR/match-range-fail-dominate.rs:18:7 | LL | 4 ..= 6 => { } - | ^^^^^^^ + | ------- this range overlaps on `4i32..=6i32` +LL | 4 ..= 6 => { } + | ^^^^^^^ overlapping patterns -error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:30:7 +error: multiple patterns covering the same range + --> $DIR/match-range-fail-dominate.rs:24:7 | +LL | 'A' ..= 'z' => {} + | ----------- this range overlaps on `'a'..='z'` LL | 'a' ..= 'z' => {} - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ overlapping patterns warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:35:7 + --> $DIR/match-range-fail-dominate.rs:29:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ @@ -39,7 +47,7 @@ LL | 0.01f64 ..= 6.5f64 => {} = note: for more information, see issue #41620 warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:35:19 + --> $DIR/match-range-fail-dominate.rs:29:19 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^ @@ -63,7 +71,7 @@ LL | 0.02f64 => {} | ^^^^^^^ warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:35:7 + --> $DIR/match-range-fail-dominate.rs:29:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ diff --git a/src/test/ui/precise_pointer_size_matching.rs b/src/test/ui/precise_pointer_size_matching.rs index 759b63b188b..239197af2ca 100644 --- a/src/test/ui/precise_pointer_size_matching.rs +++ b/src/test/ui/precise_pointer_size_matching.rs @@ -23,11 +23,11 @@ fn main() { match 0isize { //~ ERROR non-exhaustive patterns 1 ..= 8 => {} - -5 ..= 20 => {} + -5 ..= 20 => {} //~ ERROR multiple patterns covering the same range } match 0usize { //~ ERROR non-exhaustive patterns 1 ..= 8 => {} - 5 ..= 20 => {} + 5 ..= 20 => {} //~ ERROR multiple patterns covering the same range } } diff --git a/src/test/ui/precise_pointer_size_matching.stderr b/src/test/ui/precise_pointer_size_matching.stderr index 2c2c2aa04c2..06b0e73c5a3 100644 --- a/src/test/ui/precise_pointer_size_matching.stderr +++ b/src/test/ui/precise_pointer_size_matching.stderr @@ -1,3 +1,17 @@ +error: multiple patterns covering the same range + --> $DIR/precise_pointer_size_matching.rs:26:9 + | +LL | 1 ..= 8 => {} + | ------- this range overlaps on `1isize..=8isize` +LL | -5 ..= 20 => {} + | ^^^^^^^^^ overlapping patterns + | +note: lint level defined here + --> $DIR/precise_pointer_size_matching.rs:11:9 + | +LL | #![deny(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ + error[E0004]: non-exhaustive patterns: `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:24:11 | @@ -6,6 +20,14 @@ LL | match 0isize { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms +error: multiple patterns covering the same range + --> $DIR/precise_pointer_size_matching.rs:31:9 + | +LL | 1 ..= 8 => {} + | ------- this range overlaps on `5usize..=8usize` +LL | 5 ..= 20 => {} + | ^^^^^^^^ overlapping patterns + error[E0004]: non-exhaustive patterns: `0usize` and `21usize..=std::usize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:29:11 | @@ -14,6 +36,6 @@ LL | match 0usize { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0004`. -- cgit 1.4.1-3-g733a5 From 220b9b29c26953ff14eb94be70e9741365783aa3 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 29 Aug 2019 17:16:39 -0700 Subject: Move overlapping patterns to its own lint --- src/libcore/ascii.rs | 2 +- src/librustc/lint/builtin.rs | 7 +++++++ src/librustc_lint/lib.rs | 1 + src/librustc_mir/hair/pattern/_match.rs | 2 +- src/test/ui/check_match/issue-43253.rs | 2 +- src/test/ui/check_match/issue-43253.stderr | 8 +++++++- src/test/ui/exhaustive_integer_patterns.rs | 2 +- src/test/ui/exhaustive_integer_patterns.stderr | 8 +++++++- src/test/ui/match/match-range-fail-dominate.rs | 2 +- src/test/ui/match/match-range-fail-dominate.stderr | 12 +++++++++--- src/test/ui/precise_pointer_size_matching.rs | 2 +- src/test/ui/precise_pointer_size_matching.stderr | 6 +++--- 12 files changed, 40 insertions(+), 14 deletions(-) (limited to 'src/test/ui') diff --git a/src/libcore/ascii.rs b/src/libcore/ascii.rs index 0492fbd55fa..04aefd57ca2 100644 --- a/src/libcore/ascii.rs +++ b/src/libcore/ascii.rs @@ -101,7 +101,7 @@ pub fn escape_default(c: u8) -> EscapeDefault { b'\'' => ([b'\\', b'\'', 0, 0], 2), b'"' => ([b'\\', b'"', 0, 0], 2), // The three arms above are in the following range - #[allow(unreachable_patterns)] + #[allow(overlapping_patterns)] b'\x20' ..= b'\x7e' => ([c, 0, 0, 0], 1), _ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4), }; diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 983e3a9922e..4c28f6372fe 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -80,6 +80,12 @@ declare_lint! { "detects unreachable patterns" } +declare_lint! { + pub OVERLAPPING_PATTERNS, + Warn, + "detects overlapping patterns" +} + declare_lint! { pub UNUSED_MACROS, Warn, @@ -423,6 +429,7 @@ declare_lint_pass! { DEAD_CODE, UNREACHABLE_CODE, UNREACHABLE_PATTERNS, + OVERLAPPING_PATTERNS, UNUSED_MACROS, WARNINGS, UNUSED_FEATURES, diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 0e054013cd7..e3860e229d6 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -255,6 +255,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { UNUSED_MUT, UNREACHABLE_CODE, UNREACHABLE_PATTERNS, + OVERLAPPING_PATTERNS, UNUSED_MUST_USE, UNUSED_UNSAFE, PATH_STATEMENTS, diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 542f0ba5c9c..1b3c824458e 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -1741,7 +1741,7 @@ fn split_grouped_constructors<'p, 'tcx>( if let (true, Some(hir_id)) = (!overlaps.is_empty(), hir_id) { let mut err = tcx.struct_span_lint_hir( - lint::builtin::UNREACHABLE_PATTERNS, + lint::builtin::OVERLAPPING_PATTERNS, hir_id, ctor_range.span, "multiple patterns covering the same range", diff --git a/src/test/ui/check_match/issue-43253.rs b/src/test/ui/check_match/issue-43253.rs index 548300da034..5c6834459f0 100644 --- a/src/test/ui/check_match/issue-43253.rs +++ b/src/test/ui/check_match/issue-43253.rs @@ -1,7 +1,7 @@ // check-pass - #![feature(exclusive_range_pattern)] #![warn(unreachable_patterns)] +#![warn(overlapping_patterns)] fn main() { // These cases should generate no warning. diff --git a/src/test/ui/check_match/issue-43253.stderr b/src/test/ui/check_match/issue-43253.stderr index 24dfa52beb6..2f6888c0118 100644 --- a/src/test/ui/check_match/issue-43253.stderr +++ b/src/test/ui/check_match/issue-43253.stderr @@ -9,7 +9,7 @@ LL | 9..=10 => {}, note: lint level defined here --> $DIR/issue-43253.rs:4:9 | -LL | #![warn(unreachable_patterns)] +LL | #![warn(overlapping_patterns)] | ^^^^^^^^^^^^^^^^^^^^ warning: unreachable pattern @@ -17,6 +17,12 @@ warning: unreachable pattern | LL | 9 => {}, | ^ + | +note: lint level defined here + --> $DIR/issue-43253.rs:3:9 + | +LL | #![warn(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ warning: multiple patterns covering the same range --> $DIR/issue-43253.rs:35:9 diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs index ce2b44b8ef1..ef88ce94aa6 100644 --- a/src/test/ui/exhaustive_integer_patterns.rs +++ b/src/test/ui/exhaustive_integer_patterns.rs @@ -1,7 +1,7 @@ #![feature(precise_pointer_size_matching)] #![feature(exclusive_range_pattern)] - #![deny(unreachable_patterns)] +#![deny(overlapping_patterns)] use std::{char, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128}; diff --git a/src/test/ui/exhaustive_integer_patterns.stderr b/src/test/ui/exhaustive_integer_patterns.stderr index ef3faceaa7e..59cd3fffa8d 100644 --- a/src/test/ui/exhaustive_integer_patterns.stderr +++ b/src/test/ui/exhaustive_integer_patterns.stderr @@ -9,7 +9,7 @@ LL | 100 ..= 200 => {} note: lint level defined here --> $DIR/exhaustive_integer_patterns.rs:4:9 | -LL | #![deny(unreachable_patterns)] +LL | #![deny(overlapping_patterns)] | ^^^^^^^^^^^^^^^^^^^^ error: unreachable pattern @@ -17,6 +17,12 @@ error: unreachable pattern | LL | 200 => {} | ^^^ + | +note: lint level defined here + --> $DIR/exhaustive_integer_patterns.rs:3:9 + | +LL | #![deny(unreachable_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered --> $DIR/exhaustive_integer_patterns.rs:28:11 diff --git a/src/test/ui/match/match-range-fail-dominate.rs b/src/test/ui/match/match-range-fail-dominate.rs index 4c0f57ffbee..bdbb1f050a7 100644 --- a/src/test/ui/match/match-range-fail-dominate.rs +++ b/src/test/ui/match/match-range-fail-dominate.rs @@ -1,4 +1,4 @@ -#![deny(unreachable_patterns)] +#![deny(unreachable_patterns, overlapping_patterns)] fn main() { match 5 { diff --git a/src/test/ui/match/match-range-fail-dominate.stderr b/src/test/ui/match/match-range-fail-dominate.stderr index af667be317d..b14e9b5008d 100644 --- a/src/test/ui/match/match-range-fail-dominate.stderr +++ b/src/test/ui/match/match-range-fail-dominate.stderr @@ -7,10 +7,10 @@ LL | 5 ..= 6 => { } | ^^^^^^^ overlapping patterns | note: lint level defined here - --> $DIR/match-range-fail-dominate.rs:1:9 + --> $DIR/match-range-fail-dominate.rs:1:31 | -LL | #![deny(unreachable_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ +LL | #![deny(unreachable_patterns, overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ error: multiple patterns covering the same range --> $DIR/match-range-fail-dominate.rs:12:7 @@ -69,6 +69,12 @@ error: unreachable pattern | LL | 0.02f64 => {} | ^^^^^^^ + | +note: lint level defined here + --> $DIR/match-range-fail-dominate.rs:1:9 + | +LL | #![deny(unreachable_patterns, overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ warning: floating-point types cannot be used in patterns --> $DIR/match-range-fail-dominate.rs:29:7 diff --git a/src/test/ui/precise_pointer_size_matching.rs b/src/test/ui/precise_pointer_size_matching.rs index 239197af2ca..be2a140ddc3 100644 --- a/src/test/ui/precise_pointer_size_matching.rs +++ b/src/test/ui/precise_pointer_size_matching.rs @@ -8,7 +8,7 @@ #![feature(precise_pointer_size_matching)] #![feature(exclusive_range_pattern)] -#![deny(unreachable_patterns)] +#![deny(unreachable_patterns, overlapping_patterns)] use std::{usize, isize}; diff --git a/src/test/ui/precise_pointer_size_matching.stderr b/src/test/ui/precise_pointer_size_matching.stderr index 06b0e73c5a3..4930047fc1e 100644 --- a/src/test/ui/precise_pointer_size_matching.stderr +++ b/src/test/ui/precise_pointer_size_matching.stderr @@ -7,10 +7,10 @@ LL | -5 ..= 20 => {} | ^^^^^^^^^ overlapping patterns | note: lint level defined here - --> $DIR/precise_pointer_size_matching.rs:11:9 + --> $DIR/precise_pointer_size_matching.rs:11:31 | -LL | #![deny(unreachable_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ +LL | #![deny(unreachable_patterns, overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ error[E0004]: non-exhaustive patterns: `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:24:11 -- cgit 1.4.1-3-g733a5 From 89b19ccfdc6117f4df4d8c765464d6d929d483e0 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Fri, 30 Aug 2019 13:37:59 -0700 Subject: Continue to emit unreachable pattern on cases caught by overlapping patterns --- src/librustc_mir/hair/pattern/check_match.rs | 4 -- src/test/ui/check_match/issue-43253.stderr | 6 +++ src/test/ui/exhaustive_integer_patterns.rs | 4 +- src/test/ui/exhaustive_integer_patterns.stderr | 26 +++++++---- src/test/ui/match/match-range-fail-dominate.rs | 16 +++++-- src/test/ui/match/match-range-fail-dominate.stderr | 54 ++++++++++++++++------ 6 files changed, 76 insertions(+), 34 deletions(-) (limited to 'src/test/ui') diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index d6ec155c963..7bc4bf291ee 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -429,10 +429,6 @@ fn check_arms<'tcx>( hir::MatchSource::ForLoopDesugar | hir::MatchSource::Normal => { - if let box PatternKind::Range(..) = pat.kind { - // Covered by `overlapping_patterns` with more context - break; - } let mut err = cx.tcx.struct_span_lint_hir( lint::builtin::UNREACHABLE_PATTERNS, hir_pat.hir_id, diff --git a/src/test/ui/check_match/issue-43253.stderr b/src/test/ui/check_match/issue-43253.stderr index 2f6888c0118..ca4cd897322 100644 --- a/src/test/ui/check_match/issue-43253.stderr +++ b/src/test/ui/check_match/issue-43253.stderr @@ -32,6 +32,12 @@ LL | 1..10 => {}, LL | 8..=9 => {}, | ^^^^^ overlapping patterns +warning: unreachable pattern + --> $DIR/issue-43253.rs:35:9 + | +LL | 8..=9 => {}, + | ^^^^^ + warning: unreachable pattern --> $DIR/issue-43253.rs:41:9 | diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs index ef88ce94aa6..a0c1b013f39 100644 --- a/src/test/ui/exhaustive_integer_patterns.rs +++ b/src/test/ui/exhaustive_integer_patterns.rs @@ -41,7 +41,9 @@ fn main() { match x { //~ ERROR non-exhaustive patterns -7 => {} -5..=120 => {} - -2..=20 => {} //~ ERROR multiple patterns covering the same range + -2..=20 => {} + //~^ ERROR unreachable pattern + //~| ERROR multiple patterns covering the same range 125 => {} } diff --git a/src/test/ui/exhaustive_integer_patterns.stderr b/src/test/ui/exhaustive_integer_patterns.stderr index 59cd3fffa8d..44fbc969225 100644 --- a/src/test/ui/exhaustive_integer_patterns.stderr +++ b/src/test/ui/exhaustive_integer_patterns.stderr @@ -48,6 +48,12 @@ LL | -5..=120 => {} LL | -2..=20 => {} | ^^^^^^^ overlapping patterns +error: unreachable pattern + --> $DIR/exhaustive_integer_patterns.rs:44:9 + | +LL | -2..=20 => {} + | ^^^^^^^ + error[E0004]: non-exhaustive patterns: `std::i8::MIN..=-8i8`, `-6i8`, `121i8..=124i8` and 1 more not covered --> $DIR/exhaustive_integer_patterns.rs:41:11 | @@ -57,7 +63,7 @@ LL | match x { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `std::i8::MIN` not covered - --> $DIR/exhaustive_integer_patterns.rs:82:11 + --> $DIR/exhaustive_integer_patterns.rs:84:11 | LL | match 0i8 { | ^^^ pattern `std::i8::MIN` not covered @@ -65,7 +71,7 @@ LL | match 0i8 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `0i16` not covered - --> $DIR/exhaustive_integer_patterns.rs:90:11 + --> $DIR/exhaustive_integer_patterns.rs:92:11 | LL | match 0i16 { | ^^^^ pattern `0i16` not covered @@ -73,7 +79,7 @@ LL | match 0i16 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:108:11 + --> $DIR/exhaustive_integer_patterns.rs:110:11 | LL | match 0u8 { | ^^^ pattern `128u8..=std::u8::MAX` not covered @@ -81,7 +87,7 @@ LL | match 0u8 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered - --> $DIR/exhaustive_integer_patterns.rs:120:11 + --> $DIR/exhaustive_integer_patterns.rs:122:11 | LL | match (0u8, Some(())) { | ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered @@ -89,7 +95,7 @@ LL | match (0u8, Some(())) { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `(126u8..=127u8, false)` not covered - --> $DIR/exhaustive_integer_patterns.rs:125:11 + --> $DIR/exhaustive_integer_patterns.rs:127:11 | LL | match (0u8, true) { | ^^^^^^^^^^^ pattern `(126u8..=127u8, false)` not covered @@ -97,7 +103,7 @@ LL | match (0u8, true) { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error: multiple patterns covering the same range - --> $DIR/exhaustive_integer_patterns.rs:140:9 + --> $DIR/exhaustive_integer_patterns.rs:142:9 | LL | 0 .. 2 => {} | ------ this range overlaps on `1u8` @@ -105,7 +111,7 @@ LL | 1 ..= 2 => {} | ^^^^^^^ overlapping patterns error[E0004]: non-exhaustive patterns: `std::u128::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:145:11 + --> $DIR/exhaustive_integer_patterns.rs:147:11 | LL | match 0u128 { | ^^^^^ pattern `std::u128::MAX` not covered @@ -113,7 +119,7 @@ LL | match 0u128 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `5u128..=std::u128::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:149:11 + --> $DIR/exhaustive_integer_patterns.rs:151:11 | LL | match 0u128 { | ^^^^^ pattern `5u128..=std::u128::MAX` not covered @@ -121,13 +127,13 @@ LL | match 0u128 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `0u128..=3u128` not covered - --> $DIR/exhaustive_integer_patterns.rs:153:11 + --> $DIR/exhaustive_integer_patterns.rs:155:11 | LL | match 0u128 { | ^^^^^ pattern `0u128..=3u128` not covered | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 15 previous errors +error: aborting due to 16 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/match/match-range-fail-dominate.rs b/src/test/ui/match/match-range-fail-dominate.rs index bdbb1f050a7..b6a89b8910c 100644 --- a/src/test/ui/match/match-range-fail-dominate.rs +++ b/src/test/ui/match/match-range-fail-dominate.rs @@ -3,25 +3,33 @@ fn main() { match 5 { 1 ..= 10 => { } - 5 ..= 6 => { } //~ ERROR multiple patterns covering the same range + 5 ..= 6 => { } + //~^ ERROR unreachable pattern + //~| ERROR multiple patterns covering the same range _ => {} }; match 5 { 3 ..= 6 => { } - 4 ..= 6 => { } //~ ERROR multiple patterns covering the same range + 4 ..= 6 => { } + //~^ ERROR unreachable pattern + //~| ERROR multiple patterns covering the same range _ => {} }; match 5 { 4 ..= 6 => { } - 4 ..= 6 => { } //~ ERROR multiple patterns covering the same range + 4 ..= 6 => { } + //~^ ERROR unreachable pattern + //~| ERROR multiple patterns covering the same range _ => {} }; match 'c' { 'A' ..= 'z' => {} - 'a' ..= 'z' => {} //~ ERROR multiple patterns covering the same range + 'a' ..= 'z' => {} + //~^ ERROR unreachable pattern + //~| ERROR multiple patterns covering the same range _ => {} }; diff --git a/src/test/ui/match/match-range-fail-dominate.stderr b/src/test/ui/match/match-range-fail-dominate.stderr index b14e9b5008d..4a76e05fab5 100644 --- a/src/test/ui/match/match-range-fail-dominate.stderr +++ b/src/test/ui/match/match-range-fail-dominate.stderr @@ -12,32 +12,62 @@ note: lint level defined here LL | #![deny(unreachable_patterns, overlapping_patterns)] | ^^^^^^^^^^^^^^^^^^^^ +error: unreachable pattern + --> $DIR/match-range-fail-dominate.rs:6:7 + | +LL | 5 ..= 6 => { } + | ^^^^^^^ + | +note: lint level defined here + --> $DIR/match-range-fail-dominate.rs:1:9 + | +LL | #![deny(unreachable_patterns, overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ + error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:12:7 + --> $DIR/match-range-fail-dominate.rs:14:7 | LL | 3 ..= 6 => { } | ------- this range overlaps on `4i32..=6i32` LL | 4 ..= 6 => { } | ^^^^^^^ overlapping patterns +error: unreachable pattern + --> $DIR/match-range-fail-dominate.rs:14:7 + | +LL | 4 ..= 6 => { } + | ^^^^^^^ + error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:18:7 + --> $DIR/match-range-fail-dominate.rs:22:7 | LL | 4 ..= 6 => { } | ------- this range overlaps on `4i32..=6i32` LL | 4 ..= 6 => { } | ^^^^^^^ overlapping patterns +error: unreachable pattern + --> $DIR/match-range-fail-dominate.rs:22:7 + | +LL | 4 ..= 6 => { } + | ^^^^^^^ + error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:24:7 + --> $DIR/match-range-fail-dominate.rs:30:7 | LL | 'A' ..= 'z' => {} | ----------- this range overlaps on `'a'..='z'` LL | 'a' ..= 'z' => {} | ^^^^^^^^^^^ overlapping patterns +error: unreachable pattern + --> $DIR/match-range-fail-dominate.rs:30:7 + | +LL | 'a' ..= 'z' => {} + | ^^^^^^^^^^^ + warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:29:7 + --> $DIR/match-range-fail-dominate.rs:37:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ @@ -47,7 +77,7 @@ LL | 0.01f64 ..= 6.5f64 => {} = note: for more information, see issue #41620 warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:29:19 + --> $DIR/match-range-fail-dominate.rs:37:19 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^ @@ -56,7 +86,7 @@ LL | 0.01f64 ..= 6.5f64 => {} = note: for more information, see issue #41620 warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:36:7 + --> $DIR/match-range-fail-dominate.rs:44:7 | LL | 0.02f64 => {} | ^^^^^^^ @@ -65,19 +95,13 @@ LL | 0.02f64 => {} = note: for more information, see issue #41620 error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:36:7 + --> $DIR/match-range-fail-dominate.rs:44:7 | LL | 0.02f64 => {} | ^^^^^^^ - | -note: lint level defined here - --> $DIR/match-range-fail-dominate.rs:1:9 - | -LL | #![deny(unreachable_patterns, overlapping_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:29:7 + --> $DIR/match-range-fail-dominate.rs:37:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ @@ -85,5 +109,5 @@ LL | 0.01f64 ..= 6.5f64 => {} = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #41620 -error: aborting due to 5 previous errors +error: aborting due to 9 previous errors -- cgit 1.4.1-3-g733a5 From 73d6efc43e0629b2028e7d031306088f44f84782 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 6 Oct 2019 21:47:01 -0700 Subject: Only emit overlapping patterns lint if the overlap is partial --- src/libcore/ascii.rs | 2 - src/librustc_mir/hair/pattern/_match.rs | 35 +++++++++++++- src/librustc_target/abi/mod.rs | 2 - src/test/ui/check_match/issue-43253.stderr | 18 ------- src/test/ui/exhaustive_integer_patterns.rs | 1 - src/test/ui/exhaustive_integer_patterns.stderr | 28 ++++------- src/test/ui/issues/issue-13867.rs | 1 + src/test/ui/issues/issue-21475.rs | 2 +- src/test/ui/issues/issue-26251.rs | 2 + src/test/ui/match/match-range-fail-dominate.rs | 4 -- src/test/ui/match/match-range-fail-dominate.stderr | 56 ++++------------------ src/test/ui/precise_pointer_size_matching.rs | 2 +- src/test/ui/precise_pointer_size_matching.stderr | 22 +++------ .../ui/rfcs/rfc-2005-default-binding-mode/range.rs | 2 +- 14 files changed, 66 insertions(+), 111 deletions(-) (limited to 'src/test/ui') diff --git a/src/libcore/ascii.rs b/src/libcore/ascii.rs index 04aefd57ca2..4087333e2cf 100644 --- a/src/libcore/ascii.rs +++ b/src/libcore/ascii.rs @@ -100,8 +100,6 @@ pub fn escape_default(c: u8) -> EscapeDefault { b'\\' => ([b'\\', b'\\', 0, 0], 2), b'\'' => ([b'\\', b'\'', 0, 0], 2), b'"' => ([b'\\', b'"', 0, 0], 2), - // The three arms above are in the following range - #[allow(overlapping_patterns)] b'\x20' ..= b'\x7e' => ([c, 0, 0, 0], 1), _ => ([b'\\', b'x', hexify(c >> 4), hexify(c & 0xf)], 4), }; diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 04a42d85aff..580ded50b05 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -1075,6 +1075,38 @@ impl<'tcx> IntRange<'tcx> { None } } + + fn suspicious_intersection(&self, other: &Self) -> bool { + let (lo, hi) = (*self.range.start(), *self.range.end()); + let (other_lo, other_hi) = (*other.range.start(), *other.range.end()); + + // `false` in the following cases: + // 1 ---- + // 2 ---------- + // + // 1 ---------- + // 2 ---- + // + // 1 ---- + // 2 ---- + // + // 1 ---- + // 2 ---- + + // `true` in the following cases: + // 1 --------- + // 2 ---------- + lo < other_lo && hi > other_lo && hi < other_hi || + // 1 --------- + // 2 ---------- + lo > other_lo && lo < other_hi && hi > other_hi || + // 1 ---- + // 2 ---- + lo == other_hi && other_lo < lo || + // 1 ---- + // 2 ----- + hi == other_lo && lo < other_lo + } } // A request for missing constructor data in terms of either: @@ -1718,7 +1750,8 @@ fn split_grouped_constructors<'p, 'tcx>( }) .flat_map(|(range, row_len)| { let intersection = ctor_range.intersection(&range); - if let (Some(range), 1) = (&intersection, row_len) { + let should_lint = ctor_range.suspicious_intersection(&range); + if let (Some(range), 1, true) = (&intersection, row_len, should_lint) { // FIXME: for now, only check for overlapping ranges on simple range // patterns. Otherwise with the current logic the following is detected // as overlapping: diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 2feee73847d..fde5c5bed4d 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -488,7 +488,6 @@ impl Integer { /// Finds the smallest Integer type which can represent the signed value. pub fn fit_signed(x: i128) -> Integer { - #[cfg_attr(not(stage0), allow(overlapping_patterns))] match x { -0x0000_0000_0000_0080..=0x0000_0000_0000_007f => I8, -0x0000_0000_0000_8000..=0x0000_0000_0000_7fff => I16, @@ -500,7 +499,6 @@ impl Integer { /// Finds the smallest Integer type which can represent the unsigned value. pub fn fit_unsigned(x: u128) -> Integer { - #[cfg_attr(not(stage0), allow(overlapping_patterns))] match x { 0..=0x0000_0000_0000_00ff => I8, 0..=0x0000_0000_0000_ffff => I16, diff --git a/src/test/ui/check_match/issue-43253.stderr b/src/test/ui/check_match/issue-43253.stderr index ca4cd897322..cb4a0486eef 100644 --- a/src/test/ui/check_match/issue-43253.stderr +++ b/src/test/ui/check_match/issue-43253.stderr @@ -24,14 +24,6 @@ note: lint level defined here LL | #![warn(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ -warning: multiple patterns covering the same range - --> $DIR/issue-43253.rs:35:9 - | -LL | 1..10 => {}, - | ----- this range overlaps on `8i32..=9i32` -LL | 8..=9 => {}, - | ^^^^^ overlapping patterns - warning: unreachable pattern --> $DIR/issue-43253.rs:35:9 | @@ -44,16 +36,6 @@ warning: unreachable pattern LL | 6 => {}, | ^ -warning: multiple patterns covering the same range - --> $DIR/issue-43253.rs:42:9 - | -LL | 5..7 => {}, - | ---- this range overlaps on `5i32..=6i32` -LL | 6 => {}, - | - this range overlaps on `6i32` -LL | 1..10 => {}, - | ^^^^^ overlapping patterns - warning: unreachable pattern --> $DIR/issue-43253.rs:43:9 | diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs index a0c1b013f39..442b2f77257 100644 --- a/src/test/ui/exhaustive_integer_patterns.rs +++ b/src/test/ui/exhaustive_integer_patterns.rs @@ -43,7 +43,6 @@ fn main() { -5..=120 => {} -2..=20 => {} //~^ ERROR unreachable pattern - //~| ERROR multiple patterns covering the same range 125 => {} } diff --git a/src/test/ui/exhaustive_integer_patterns.stderr b/src/test/ui/exhaustive_integer_patterns.stderr index 44fbc969225..4c5806f9606 100644 --- a/src/test/ui/exhaustive_integer_patterns.stderr +++ b/src/test/ui/exhaustive_integer_patterns.stderr @@ -40,14 +40,6 @@ LL | match x { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: multiple patterns covering the same range - --> $DIR/exhaustive_integer_patterns.rs:44:9 - | -LL | -5..=120 => {} - | -------- this range overlaps on `-2i8..=20i8` -LL | -2..=20 => {} - | ^^^^^^^ overlapping patterns - error: unreachable pattern --> $DIR/exhaustive_integer_patterns.rs:44:9 | @@ -63,7 +55,7 @@ LL | match x { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `std::i8::MIN` not covered - --> $DIR/exhaustive_integer_patterns.rs:84:11 + --> $DIR/exhaustive_integer_patterns.rs:83:11 | LL | match 0i8 { | ^^^ pattern `std::i8::MIN` not covered @@ -71,7 +63,7 @@ LL | match 0i8 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `0i16` not covered - --> $DIR/exhaustive_integer_patterns.rs:92:11 + --> $DIR/exhaustive_integer_patterns.rs:91:11 | LL | match 0i16 { | ^^^^ pattern `0i16` not covered @@ -79,7 +71,7 @@ LL | match 0i16 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `128u8..=std::u8::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:110:11 + --> $DIR/exhaustive_integer_patterns.rs:109:11 | LL | match 0u8 { | ^^^ pattern `128u8..=std::u8::MAX` not covered @@ -87,7 +79,7 @@ LL | match 0u8 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered - --> $DIR/exhaustive_integer_patterns.rs:122:11 + --> $DIR/exhaustive_integer_patterns.rs:121:11 | LL | match (0u8, Some(())) { | ^^^^^^^^^^^^^^^ patterns `(0u8, Some(_))` and `(2u8..=std::u8::MAX, Some(_))` not covered @@ -95,7 +87,7 @@ LL | match (0u8, Some(())) { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `(126u8..=127u8, false)` not covered - --> $DIR/exhaustive_integer_patterns.rs:127:11 + --> $DIR/exhaustive_integer_patterns.rs:126:11 | LL | match (0u8, true) { | ^^^^^^^^^^^ pattern `(126u8..=127u8, false)` not covered @@ -103,7 +95,7 @@ LL | match (0u8, true) { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error: multiple patterns covering the same range - --> $DIR/exhaustive_integer_patterns.rs:142:9 + --> $DIR/exhaustive_integer_patterns.rs:141:9 | LL | 0 .. 2 => {} | ------ this range overlaps on `1u8` @@ -111,7 +103,7 @@ LL | 1 ..= 2 => {} | ^^^^^^^ overlapping patterns error[E0004]: non-exhaustive patterns: `std::u128::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:147:11 + --> $DIR/exhaustive_integer_patterns.rs:146:11 | LL | match 0u128 { | ^^^^^ pattern `std::u128::MAX` not covered @@ -119,7 +111,7 @@ LL | match 0u128 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `5u128..=std::u128::MAX` not covered - --> $DIR/exhaustive_integer_patterns.rs:151:11 + --> $DIR/exhaustive_integer_patterns.rs:150:11 | LL | match 0u128 { | ^^^^^ pattern `5u128..=std::u128::MAX` not covered @@ -127,13 +119,13 @@ LL | match 0u128 { = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms error[E0004]: non-exhaustive patterns: `0u128..=3u128` not covered - --> $DIR/exhaustive_integer_patterns.rs:155:11 + --> $DIR/exhaustive_integer_patterns.rs:154:11 | LL | match 0u128 { | ^^^^^ pattern `0u128..=3u128` not covered | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 16 previous errors +error: aborting due to 15 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/issues/issue-13867.rs b/src/test/ui/issues/issue-13867.rs index e66368f9ba8..80920568563 100644 --- a/src/test/ui/issues/issue-13867.rs +++ b/src/test/ui/issues/issue-13867.rs @@ -1,6 +1,7 @@ // run-pass // Test that codegen works correctly when there are multiple refutable // patterns in match expression. +#![allow(overlapping_patterns)] enum Foo { diff --git a/src/test/ui/issues/issue-21475.rs b/src/test/ui/issues/issue-21475.rs index 16d003aba7c..ab0a1886963 100644 --- a/src/test/ui/issues/issue-21475.rs +++ b/src/test/ui/issues/issue-21475.rs @@ -1,5 +1,5 @@ // run-pass -#![allow(unused_imports)] +#![allow(unused_imports, overlapping_patterns)] // pretty-expanded FIXME #23616 use m::{START, END}; diff --git a/src/test/ui/issues/issue-26251.rs b/src/test/ui/issues/issue-26251.rs index 0434ef9e5a9..edb06fea8ad 100644 --- a/src/test/ui/issues/issue-26251.rs +++ b/src/test/ui/issues/issue-26251.rs @@ -1,4 +1,6 @@ // run-pass +#![allow(overlapping_patterns)] + fn main() { let x = 'a'; diff --git a/src/test/ui/match/match-range-fail-dominate.rs b/src/test/ui/match/match-range-fail-dominate.rs index b6a89b8910c..7de7b7e79be 100644 --- a/src/test/ui/match/match-range-fail-dominate.rs +++ b/src/test/ui/match/match-range-fail-dominate.rs @@ -5,7 +5,6 @@ fn main() { 1 ..= 10 => { } 5 ..= 6 => { } //~^ ERROR unreachable pattern - //~| ERROR multiple patterns covering the same range _ => {} }; @@ -13,7 +12,6 @@ fn main() { 3 ..= 6 => { } 4 ..= 6 => { } //~^ ERROR unreachable pattern - //~| ERROR multiple patterns covering the same range _ => {} }; @@ -21,7 +19,6 @@ fn main() { 4 ..= 6 => { } 4 ..= 6 => { } //~^ ERROR unreachable pattern - //~| ERROR multiple patterns covering the same range _ => {} }; @@ -29,7 +26,6 @@ fn main() { 'A' ..= 'z' => {} 'a' ..= 'z' => {} //~^ ERROR unreachable pattern - //~| ERROR multiple patterns covering the same range _ => {} }; diff --git a/src/test/ui/match/match-range-fail-dominate.stderr b/src/test/ui/match/match-range-fail-dominate.stderr index 4a76e05fab5..c15186d2558 100644 --- a/src/test/ui/match/match-range-fail-dominate.stderr +++ b/src/test/ui/match/match-range-fail-dominate.stderr @@ -1,17 +1,3 @@ -error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:6:7 - | -LL | 1 ..= 10 => { } - | -------- this range overlaps on `5i32..=6i32` -LL | 5 ..= 6 => { } - | ^^^^^^^ overlapping patterns - | -note: lint level defined here - --> $DIR/match-range-fail-dominate.rs:1:31 - | -LL | #![deny(unreachable_patterns, overlapping_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ - error: unreachable pattern --> $DIR/match-range-fail-dominate.rs:6:7 | @@ -24,50 +10,26 @@ note: lint level defined here LL | #![deny(unreachable_patterns, overlapping_patterns)] | ^^^^^^^^^^^^^^^^^^^^ -error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:14:7 - | -LL | 3 ..= 6 => { } - | ------- this range overlaps on `4i32..=6i32` -LL | 4 ..= 6 => { } - | ^^^^^^^ overlapping patterns - error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:14:7 + --> $DIR/match-range-fail-dominate.rs:13:7 | LL | 4 ..= 6 => { } | ^^^^^^^ -error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:22:7 - | -LL | 4 ..= 6 => { } - | ------- this range overlaps on `4i32..=6i32` -LL | 4 ..= 6 => { } - | ^^^^^^^ overlapping patterns - error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:22:7 + --> $DIR/match-range-fail-dominate.rs:20:7 | LL | 4 ..= 6 => { } | ^^^^^^^ -error: multiple patterns covering the same range - --> $DIR/match-range-fail-dominate.rs:30:7 - | -LL | 'A' ..= 'z' => {} - | ----------- this range overlaps on `'a'..='z'` -LL | 'a' ..= 'z' => {} - | ^^^^^^^^^^^ overlapping patterns - error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:30:7 + --> $DIR/match-range-fail-dominate.rs:27:7 | LL | 'a' ..= 'z' => {} | ^^^^^^^^^^^ warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:37:7 + --> $DIR/match-range-fail-dominate.rs:33:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ @@ -77,7 +39,7 @@ LL | 0.01f64 ..= 6.5f64 => {} = note: for more information, see issue #41620 warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:37:19 + --> $DIR/match-range-fail-dominate.rs:33:19 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^ @@ -86,7 +48,7 @@ LL | 0.01f64 ..= 6.5f64 => {} = note: for more information, see issue #41620 warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:44:7 + --> $DIR/match-range-fail-dominate.rs:40:7 | LL | 0.02f64 => {} | ^^^^^^^ @@ -95,13 +57,13 @@ LL | 0.02f64 => {} = note: for more information, see issue #41620 error: unreachable pattern - --> $DIR/match-range-fail-dominate.rs:44:7 + --> $DIR/match-range-fail-dominate.rs:40:7 | LL | 0.02f64 => {} | ^^^^^^^ warning: floating-point types cannot be used in patterns - --> $DIR/match-range-fail-dominate.rs:37:7 + --> $DIR/match-range-fail-dominate.rs:33:7 | LL | 0.01f64 ..= 6.5f64 => {} | ^^^^^^^ @@ -109,5 +71,5 @@ LL | 0.01f64 ..= 6.5f64 => {} = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #41620 -error: aborting due to 9 previous errors +error: aborting due to 5 previous errors diff --git a/src/test/ui/precise_pointer_size_matching.rs b/src/test/ui/precise_pointer_size_matching.rs index be2a140ddc3..ee91088c0c3 100644 --- a/src/test/ui/precise_pointer_size_matching.rs +++ b/src/test/ui/precise_pointer_size_matching.rs @@ -23,7 +23,7 @@ fn main() { match 0isize { //~ ERROR non-exhaustive patterns 1 ..= 8 => {} - -5 ..= 20 => {} //~ ERROR multiple patterns covering the same range + -5 ..= 20 => {} } match 0usize { //~ ERROR non-exhaustive patterns diff --git a/src/test/ui/precise_pointer_size_matching.stderr b/src/test/ui/precise_pointer_size_matching.stderr index 4930047fc1e..98d347e06b6 100644 --- a/src/test/ui/precise_pointer_size_matching.stderr +++ b/src/test/ui/precise_pointer_size_matching.stderr @@ -1,17 +1,3 @@ -error: multiple patterns covering the same range - --> $DIR/precise_pointer_size_matching.rs:26:9 - | -LL | 1 ..= 8 => {} - | ------- this range overlaps on `1isize..=8isize` -LL | -5 ..= 20 => {} - | ^^^^^^^^^ overlapping patterns - | -note: lint level defined here - --> $DIR/precise_pointer_size_matching.rs:11:31 - | -LL | #![deny(unreachable_patterns, overlapping_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ - error[E0004]: non-exhaustive patterns: `std::isize::MIN..=-6isize` and `21isize..=std::isize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:24:11 | @@ -27,6 +13,12 @@ LL | 1 ..= 8 => {} | ------- this range overlaps on `5usize..=8usize` LL | 5 ..= 20 => {} | ^^^^^^^^ overlapping patterns + | +note: lint level defined here + --> $DIR/precise_pointer_size_matching.rs:11:31 + | +LL | #![deny(unreachable_patterns, overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ error[E0004]: non-exhaustive patterns: `0usize` and `21usize..=std::usize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:29:11 @@ -36,6 +28,6 @@ LL | match 0usize { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/rfcs/rfc-2005-default-binding-mode/range.rs b/src/test/ui/rfcs/rfc-2005-default-binding-mode/range.rs index 580e67513b3..f8abd1b96d8 100644 --- a/src/test/ui/rfcs/rfc-2005-default-binding-mode/range.rs +++ b/src/test/ui/rfcs/rfc-2005-default-binding-mode/range.rs @@ -3,7 +3,7 @@ pub fn main() { let i = 5; match &&&&i { 1 ..= 3 => panic!(), - 3 ..= 8 => {}, + 4 ..= 8 => {}, _ => panic!(), } } -- cgit 1.4.1-3-g733a5 From 593cdcccf28361df155c37916dcfcbe1bf19d9a5 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Wed, 16 Oct 2019 12:22:23 -0700 Subject: Lint only on single element overlap --- src/librustc_mir/hair/pattern/_match.rs | 35 +++++++----------------- src/test/ui/exhaustive_integer_patterns.rs | 2 +- src/test/ui/exhaustive_integer_patterns.stderr | 22 +++++---------- src/test/ui/issues/issue-13867.rs | 2 -- src/test/ui/precise_pointer_size_matching.rs | 2 +- src/test/ui/precise_pointer_size_matching.stderr | 16 +---------- 6 files changed, 20 insertions(+), 59 deletions(-) (limited to 'src/test/ui') diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 580ded50b05..1d83b104177 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -1077,35 +1077,20 @@ impl<'tcx> IntRange<'tcx> { } fn suspicious_intersection(&self, other: &Self) -> bool { - let (lo, hi) = (*self.range.start(), *self.range.end()); - let (other_lo, other_hi) = (*other.range.start(), *other.range.end()); - // `false` in the following cases: - // 1 ---- - // 2 ---------- - // - // 1 ---------- - // 2 ---- + // 1 ---- // 1 ---------- // 1 ---- // 1 ---- + // 2 ---------- // 2 ---- // 2 ---- // 2 ---- // - // 1 ---- - // 2 ---- + // The following are currently `false`, but could be `true` in the future (#64007): + // 1 --------- // 1 --------- + // 2 ---------- // 2 ---------- // - // 1 ---- - // 2 ---- - // `true` in the following cases: - // 1 --------- - // 2 ---------- - lo < other_lo && hi > other_lo && hi < other_hi || - // 1 --------- - // 2 ---------- - lo > other_lo && lo < other_hi && hi > other_hi || - // 1 ---- - // 2 ---- - lo == other_hi && other_lo < lo || - // 1 ---- - // 2 ----- - hi == other_lo && lo < other_lo + // 1 ------- // 1 ------- + // 2 -------- // 2 ------- + let (lo, hi) = (*self.range.start(), *self.range.end()); + let (other_lo, other_hi) = (*other.range.start(), *other.range.end()); + (lo == other_hi || hi == other_lo) } } diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs index 442b2f77257..59f74919897 100644 --- a/src/test/ui/exhaustive_integer_patterns.rs +++ b/src/test/ui/exhaustive_integer_patterns.rs @@ -19,7 +19,7 @@ fn main() { 0 ..= 32 => {} 33 => {} 34 .. 128 => {} - 100 ..= 200 => {} //~ ERROR multiple patterns covering the same range + 100 ..= 200 => {} 200 => {} //~ ERROR unreachable pattern 201 ..= 255 => {} } diff --git a/src/test/ui/exhaustive_integer_patterns.stderr b/src/test/ui/exhaustive_integer_patterns.stderr index 4c5806f9606..7a3a36a820c 100644 --- a/src/test/ui/exhaustive_integer_patterns.stderr +++ b/src/test/ui/exhaustive_integer_patterns.stderr @@ -1,17 +1,3 @@ -error: multiple patterns covering the same range - --> $DIR/exhaustive_integer_patterns.rs:22:9 - | -LL | 34 .. 128 => {} - | --------- this range overlaps on `100u8..=127u8` -LL | 100 ..= 200 => {} - | ^^^^^^^^^^^ overlapping patterns - | -note: lint level defined here - --> $DIR/exhaustive_integer_patterns.rs:4:9 - | -LL | #![deny(overlapping_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ - error: unreachable pattern --> $DIR/exhaustive_integer_patterns.rs:23:9 | @@ -101,6 +87,12 @@ LL | 0 .. 2 => {} | ------ this range overlaps on `1u8` LL | 1 ..= 2 => {} | ^^^^^^^ overlapping patterns + | +note: lint level defined here + --> $DIR/exhaustive_integer_patterns.rs:4:9 + | +LL | #![deny(overlapping_patterns)] + | ^^^^^^^^^^^^^^^^^^^^ error[E0004]: non-exhaustive patterns: `std::u128::MAX` not covered --> $DIR/exhaustive_integer_patterns.rs:146:11 @@ -126,6 +118,6 @@ LL | match 0u128 { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 15 previous errors +error: aborting due to 14 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/issues/issue-13867.rs b/src/test/ui/issues/issue-13867.rs index 80920568563..9510aae7753 100644 --- a/src/test/ui/issues/issue-13867.rs +++ b/src/test/ui/issues/issue-13867.rs @@ -1,8 +1,6 @@ // run-pass // Test that codegen works correctly when there are multiple refutable // patterns in match expression. -#![allow(overlapping_patterns)] - enum Foo { FooUint(usize), diff --git a/src/test/ui/precise_pointer_size_matching.rs b/src/test/ui/precise_pointer_size_matching.rs index ee91088c0c3..54aeb8616d9 100644 --- a/src/test/ui/precise_pointer_size_matching.rs +++ b/src/test/ui/precise_pointer_size_matching.rs @@ -28,6 +28,6 @@ fn main() { match 0usize { //~ ERROR non-exhaustive patterns 1 ..= 8 => {} - 5 ..= 20 => {} //~ ERROR multiple patterns covering the same range + 5 ..= 20 => {} } } diff --git a/src/test/ui/precise_pointer_size_matching.stderr b/src/test/ui/precise_pointer_size_matching.stderr index 98d347e06b6..2c2c2aa04c2 100644 --- a/src/test/ui/precise_pointer_size_matching.stderr +++ b/src/test/ui/precise_pointer_size_matching.stderr @@ -6,20 +6,6 @@ LL | match 0isize { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: multiple patterns covering the same range - --> $DIR/precise_pointer_size_matching.rs:31:9 - | -LL | 1 ..= 8 => {} - | ------- this range overlaps on `5usize..=8usize` -LL | 5 ..= 20 => {} - | ^^^^^^^^ overlapping patterns - | -note: lint level defined here - --> $DIR/precise_pointer_size_matching.rs:11:31 - | -LL | #![deny(unreachable_patterns, overlapping_patterns)] - | ^^^^^^^^^^^^^^^^^^^^ - error[E0004]: non-exhaustive patterns: `0usize` and `21usize..=std::usize::MAX` not covered --> $DIR/precise_pointer_size_matching.rs:29:11 | @@ -28,6 +14,6 @@ LL | match 0usize { | = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0004`. -- cgit 1.4.1-3-g733a5