about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-06-16 21:39:02 +0200
committery21 <30553356+y21@users.noreply.github.com>2023-06-27 22:34:31 +0200
commit1d4afc5d8201e591e0019883c0c2c6a0ae93f13b (patch)
treee9ffa5fc9a8e77d7b0fa85817986e417bcafd0f3 /tests
parent2e329050c9516bf56a17233a8471f115299d3e1f (diff)
downloadrust-1d4afc5d8201e591e0019883c0c2c6a0ae93f13b.tar.gz
rust-1d4afc5d8201e591e0019883c0c2c6a0ae93f13b.zip
new lint: `manual_range_pattern`
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/manual_range_pattern.fixed35
-rw-r--r--tests/ui/manual_range_pattern.rs35
-rw-r--r--tests/ui/manual_range_pattern.stderr51
-rw-r--r--tests/ui/unnested_or_patterns.fixed3
-rw-r--r--tests/ui/unnested_or_patterns.rs3
-rw-r--r--tests/ui/unnested_or_patterns.stderr34
-rw-r--r--tests/ui/unnested_or_patterns2.fixed7
-rw-r--r--tests/ui/unnested_or_patterns2.rs7
-rw-r--r--tests/ui/unnested_or_patterns2.stderr16
9 files changed, 162 insertions, 29 deletions
diff --git a/tests/ui/manual_range_pattern.fixed b/tests/ui/manual_range_pattern.fixed
new file mode 100644
index 00000000000..14a5f822d60
--- /dev/null
+++ b/tests/ui/manual_range_pattern.fixed
@@ -0,0 +1,35 @@
+//@run-rustfix
+
+#![allow(unused)]
+#![warn(clippy::manual_range_pattern)]
+#![feature(exclusive_range_pattern)]
+
+fn main() {
+    let f = 6;
+
+    let _ = matches!(f, 1..=10);
+    let _ = matches!(f, 1..=10);
+    let _ = matches!(f, 4 | 2 | 3 | 1 | 5 | 6 | 9 | 8 | 10); // 7 is missing
+    let _ = matches!(f, | 4);
+    let _ = matches!(f, 4 | 5);
+    let _ = matches!(f, 1 | 2147483647);
+    let _ = matches!(f, 0 | 2147483647);
+    let _ = matches!(f, -2147483647 | 2147483647);
+    let _ = matches!(f, 1 | (2..=4));
+    let _ = matches!(f, 1 | (2..4));
+    let _ = matches!(f, 1..=48324729);
+    let _ = matches!(f, 0..=48324730);
+    let _ = matches!(f, 0..=3);
+    #[allow(clippy::match_like_matches_macro)]
+    let _ = match f {
+        1..=10 => true,
+        _ => false,
+    };
+
+    macro_rules! mac {
+        ($e:expr) => {
+            matches!($e, 1..=10)
+        };
+    }
+    mac!(f);
+}
diff --git a/tests/ui/manual_range_pattern.rs b/tests/ui/manual_range_pattern.rs
new file mode 100644
index 00000000000..ea63953af04
--- /dev/null
+++ b/tests/ui/manual_range_pattern.rs
@@ -0,0 +1,35 @@
+//@run-rustfix
+
+#![allow(unused)]
+#![warn(clippy::manual_range_pattern)]
+#![feature(exclusive_range_pattern)]
+
+fn main() {
+    let f = 6;
+
+    let _ = matches!(f, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10);
+    let _ = matches!(f, 4 | 2 | 3 | 1 | 5 | 6 | 9 | 7 | 8 | 10);
+    let _ = matches!(f, 4 | 2 | 3 | 1 | 5 | 6 | 9 | 8 | 10); // 7 is missing
+    let _ = matches!(f, | 4);
+    let _ = matches!(f, 4 | 5);
+    let _ = matches!(f, 1 | 2147483647);
+    let _ = matches!(f, 0 | 2147483647);
+    let _ = matches!(f, -2147483647 | 2147483647);
+    let _ = matches!(f, 1 | (2..=4));
+    let _ = matches!(f, 1 | (2..4));
+    let _ = matches!(f, (1..=10) | (2..=13) | (14..=48324728) | 48324729);
+    let _ = matches!(f, 0 | (1..=10) | 48324730 | (2..=13) | (14..=48324728) | 48324729);
+    let _ = matches!(f, 0..=1 | 0..=2 | 0..=3);
+    #[allow(clippy::match_like_matches_macro)]
+    let _ = match f {
+        1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => true,
+        _ => false,
+    };
+
+    macro_rules! mac {
+        ($e:expr) => {
+            matches!($e, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10)
+        };
+    }
+    mac!(f);
+}
diff --git a/tests/ui/manual_range_pattern.stderr b/tests/ui/manual_range_pattern.stderr
new file mode 100644
index 00000000000..a8969ede4e0
--- /dev/null
+++ b/tests/ui/manual_range_pattern.stderr
@@ -0,0 +1,51 @@
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:10:25
+   |
+LL |     let _ = matches!(f, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10);
+   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
+   |
+   = note: `-D clippy::manual-range-pattern` implied by `-D warnings`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:11:25
+   |
+LL |     let _ = matches!(f, 4 | 2 | 3 | 1 | 5 | 6 | 9 | 7 | 8 | 10);
+   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:20:25
+   |
+LL |     let _ = matches!(f, (1..=10) | (2..=13) | (14..=48324728) | 48324729);
+   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=48324729`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:21:25
+   |
+LL |     let _ = matches!(f, 0 | (1..=10) | 48324730 | (2..=13) | (14..=48324728) | 48324729);
+   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `0..=48324730`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:22:25
+   |
+LL |     let _ = matches!(f, 0..=1 | 0..=2 | 0..=3);
+   |                         ^^^^^^^^^^^^^^^^^^^^^ help: try: `0..=3`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:25:9
+   |
+LL |         1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 => true,
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
+
+error: this OR pattern can be rewritten using a range
+  --> $DIR/manual_range_pattern.rs:31:26
+   |
+LL |             matches!($e, 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10)
+   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `1..=10`
+...
+LL |     mac!(f);
+   |     ------- in this macro invocation
+   |
+   = note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 7 previous errors
+
diff --git a/tests/ui/unnested_or_patterns.fixed b/tests/ui/unnested_or_patterns.fixed
index 4ce6e866695..ca999f241f7 100644
--- a/tests/ui/unnested_or_patterns.fixed
+++ b/tests/ui/unnested_or_patterns.fixed
@@ -6,7 +6,8 @@
     clippy::cognitive_complexity,
     clippy::match_ref_pats,
     clippy::upper_case_acronyms,
-    clippy::needless_if
+    clippy::needless_if,
+    clippy::manual_range_pattern
 )]
 #![allow(unreachable_patterns, irrefutable_let_patterns, unused)]
 
diff --git a/tests/ui/unnested_or_patterns.rs b/tests/ui/unnested_or_patterns.rs
index 07d60fac082..a09c3c66090 100644
--- a/tests/ui/unnested_or_patterns.rs
+++ b/tests/ui/unnested_or_patterns.rs
@@ -6,7 +6,8 @@
     clippy::cognitive_complexity,
     clippy::match_ref_pats,
     clippy::upper_case_acronyms,
-    clippy::needless_if
+    clippy::needless_if,
+    clippy::manual_range_pattern
 )]
 #![allow(unreachable_patterns, irrefutable_let_patterns, unused)]
 
diff --git a/tests/ui/unnested_or_patterns.stderr b/tests/ui/unnested_or_patterns.stderr
index d7b582fc8bd..b997e4ce85f 100644
--- a/tests/ui/unnested_or_patterns.stderr
+++ b/tests/ui/unnested_or_patterns.stderr
@@ -1,5 +1,5 @@
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:17:12
+  --> $DIR/unnested_or_patterns.rs:18:12
    |
 LL |     if let box 0 | box 2 = Box::new(0) {}
    |            ^^^^^^^^^^^^^
@@ -11,7 +11,7 @@ LL |     if let box (0 | 2) = Box::new(0) {}
    |            ~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:18:12
+  --> $DIR/unnested_or_patterns.rs:19:12
    |
 LL |     if let box ((0 | 1)) | box (2 | 3) | box 4 = Box::new(0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,7 +22,7 @@ LL |     if let box (0 | 1 | 2 | 3 | 4) = Box::new(0) {}
    |            ~~~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:20:12
+  --> $DIR/unnested_or_patterns.rs:21:12
    |
 LL |     if let Some(1) | C0 | Some(2) = None {}
    |            ^^^^^^^^^^^^^^^^^^^^^^
@@ -33,7 +33,7 @@ LL |     if let Some(1 | 2) | C0 = None {}
    |            ~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:21:12
+  --> $DIR/unnested_or_patterns.rs:22:12
    |
 LL |     if let &mut 0 | &mut 2 = &mut 0 {}
    |            ^^^^^^^^^^^^^^^
@@ -44,7 +44,7 @@ LL |     if let &mut (0 | 2) = &mut 0 {}
    |            ~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:22:12
+  --> $DIR/unnested_or_patterns.rs:23:12
    |
 LL |     if let x @ 0 | x @ 2 = 0 {}
    |            ^^^^^^^^^^^^^
@@ -55,7 +55,7 @@ LL |     if let x @ (0 | 2) = 0 {}
    |            ~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:23:12
+  --> $DIR/unnested_or_patterns.rs:24:12
    |
 LL |     if let (0, 1) | (0, 2) | (0, 3) = (0, 0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL |     if let (0, 1 | 2 | 3) = (0, 0) {}
    |            ~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:24:12
+  --> $DIR/unnested_or_patterns.rs:25:12
    |
 LL |     if let (1, 0) | (2, 0) | (3, 0) = (0, 0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL |     if let (1 | 2 | 3, 0) = (0, 0) {}
    |            ~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:25:12
+  --> $DIR/unnested_or_patterns.rs:26:12
    |
 LL |     if let (x, ..) | (x, 1) | (x, 2) = (0, 1) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -88,7 +88,7 @@ LL |     if let (x, ..) | (x, 1 | 2) = (0, 1) {}
    |            ~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:26:12
+  --> $DIR/unnested_or_patterns.rs:27:12
    |
 LL |     if let [0] | [1] = [0] {}
    |            ^^^^^^^^^
@@ -99,7 +99,7 @@ LL |     if let [0 | 1] = [0] {}
    |            ~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:27:12
+  --> $DIR/unnested_or_patterns.rs:28:12
    |
 LL |     if let [x, 0] | [x, 1] = [0, 1] {}
    |            ^^^^^^^^^^^^^^^
@@ -110,7 +110,7 @@ LL |     if let [x, 0 | 1] = [0, 1] {}
    |            ~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:28:12
+  --> $DIR/unnested_or_patterns.rs:29:12
    |
 LL |     if let [x, 0] | [x, 1] | [x, 2] = [0, 1] {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -121,7 +121,7 @@ LL |     if let [x, 0 | 1 | 2] = [0, 1] {}
    |            ~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:29:12
+  --> $DIR/unnested_or_patterns.rs:30:12
    |
 LL |     if let [x, ..] | [x, 1] | [x, 2] = [0, 1] {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -132,7 +132,7 @@ LL |     if let [x, ..] | [x, 1 | 2] = [0, 1] {}
    |            ~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:31:12
+  --> $DIR/unnested_or_patterns.rs:32:12
    |
 LL |     if let TS(0, x) | TS(1, x) = TS(0, 0) {}
    |            ^^^^^^^^^^^^^^^^^^^
@@ -143,7 +143,7 @@ LL |     if let TS(0 | 1, x) = TS(0, 0) {}
    |            ~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:32:12
+  --> $DIR/unnested_or_patterns.rs:33:12
    |
 LL |     if let TS(1, 0) | TS(2, 0) | TS(3, 0) = TS(0, 0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL |     if let TS(1 | 2 | 3, 0) = TS(0, 0) {}
    |            ~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:33:12
+  --> $DIR/unnested_or_patterns.rs:34:12
    |
 LL |     if let TS(x, ..) | TS(x, 1) | TS(x, 2) = TS(0, 0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -165,7 +165,7 @@ LL |     if let TS(x, ..) | TS(x, 1 | 2) = TS(0, 0) {}
    |            ~~~~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:38:12
+  --> $DIR/unnested_or_patterns.rs:39:12
    |
 LL |     if let S { x: 0, y } | S { y, x: 1 } = (S { x: 0, y: 1 }) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -176,7 +176,7 @@ LL |     if let S { x: 0 | 1, y } = (S { x: 0, y: 1 }) {}
    |            ~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns.rs:49:12
+  --> $DIR/unnested_or_patterns.rs:50:12
    |
 LL |     if let [1] | [53] = [0] {}
    |            ^^^^^^^^^^
diff --git a/tests/ui/unnested_or_patterns2.fixed b/tests/ui/unnested_or_patterns2.fixed
index a88d7f51ac0..ebaba019cfd 100644
--- a/tests/ui/unnested_or_patterns2.fixed
+++ b/tests/ui/unnested_or_patterns2.fixed
@@ -2,7 +2,12 @@
 
 #![feature(box_patterns)]
 #![warn(clippy::unnested_or_patterns)]
-#![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::needless_if)]
+#![allow(
+    clippy::cognitive_complexity,
+    clippy::match_ref_pats,
+    clippy::needless_if,
+    clippy::manual_range_pattern
+)]
 #![allow(unreachable_patterns, irrefutable_let_patterns, unused_variables)]
 
 fn main() {
diff --git a/tests/ui/unnested_or_patterns2.rs b/tests/ui/unnested_or_patterns2.rs
index f6029eee441..50b6ca585cf 100644
--- a/tests/ui/unnested_or_patterns2.rs
+++ b/tests/ui/unnested_or_patterns2.rs
@@ -2,7 +2,12 @@
 
 #![feature(box_patterns)]
 #![warn(clippy::unnested_or_patterns)]
-#![allow(clippy::cognitive_complexity, clippy::match_ref_pats, clippy::needless_if)]
+#![allow(
+    clippy::cognitive_complexity,
+    clippy::match_ref_pats,
+    clippy::needless_if,
+    clippy::manual_range_pattern
+)]
 #![allow(unreachable_patterns, irrefutable_let_patterns, unused_variables)]
 
 fn main() {
diff --git a/tests/ui/unnested_or_patterns2.stderr b/tests/ui/unnested_or_patterns2.stderr
index 41e8d3fc709..76e890b3a2e 100644
--- a/tests/ui/unnested_or_patterns2.stderr
+++ b/tests/ui/unnested_or_patterns2.stderr
@@ -1,5 +1,5 @@
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:9:12
+  --> $DIR/unnested_or_patterns2.rs:14:12
    |
 LL |     if let Some(Some(0)) | Some(Some(1)) = None {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -11,7 +11,7 @@ LL |     if let Some(Some(0 | 1)) = None {}
    |            ~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:10:12
+  --> $DIR/unnested_or_patterns2.rs:15:12
    |
 LL |     if let Some(Some(0)) | Some(Some(1) | Some(2)) = None {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,7 +22,7 @@ LL |     if let Some(Some(0 | 1 | 2)) = None {}
    |            ~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:11:12
+  --> $DIR/unnested_or_patterns2.rs:16:12
    |
 LL |     if let Some(Some(0 | 1) | Some(2)) | Some(Some(3) | Some(4)) = None {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -33,7 +33,7 @@ LL |     if let Some(Some(0 | 1 | 2 | 3 | 4)) = None {}
    |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:12:12
+  --> $DIR/unnested_or_patterns2.rs:17:12
    |
 LL |     if let Some(Some(0) | Some(1 | 2)) = None {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -44,7 +44,7 @@ LL |     if let Some(Some(0 | 1 | 2)) = None {}
    |            ~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:13:12
+  --> $DIR/unnested_or_patterns2.rs:18:12
    |
 LL |     if let ((0,),) | ((1,) | (2,),) = ((0,),) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -55,7 +55,7 @@ LL |     if let ((0 | 1 | 2,),) = ((0,),) {}
    |            ~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:14:12
+  --> $DIR/unnested_or_patterns2.rs:19:12
    |
 LL |     if let 0 | (1 | 2) = 0 {}
    |            ^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL |     if let 0 | 1 | 2 = 0 {}
    |            ~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:15:12
+  --> $DIR/unnested_or_patterns2.rs:20:12
    |
 LL |     if let box (0 | 1) | (box 2 | box (3 | 4)) = Box::new(0) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL |     if let box (0 | 1 | 2 | 3 | 4) = Box::new(0) {}
    |            ~~~~~~~~~~~~~~~~~~~~~~~
 
 error: unnested or-patterns
-  --> $DIR/unnested_or_patterns2.rs:16:12
+  --> $DIR/unnested_or_patterns2.rs:21:12
    |
 LL |     if let box box 0 | box (box 2 | box 4) = Box::new(Box::new(0)) {}
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^