about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorMichael Wright <mikerite@lavabit.com>2022-09-08 20:04:43 +0200
committerMichael Wright <mikerite@lavabit.com>2022-09-08 20:04:43 +0200
commita6d8afd958e35cb0f424de8be11c42116133e23a (patch)
treee3f61368dc190fc285d71c7fe625f533dfbb92a0 /tests
parentb30c5c05545047305ca56ce4a2a545ce90dc2821 (diff)
downloadrust-a6d8afd958e35cb0f424de8be11c42116133e23a.tar.gz
rust-a6d8afd958e35cb0f424de8be11c42116133e23a.zip
Fix `range_{plus,minus}_one` bad suggestions
Fixes #9431.

The current `range_plus_one` and `range_minus_one` suggestions
are completely incorrect when macros are involved.

This commit resolves this by disabling the lints for any range
expression that is expanded from a macro. The reasons for this
are that it is very difficult to create a correct suggestion in
this case and that false negatives are less important for
pedantic lints.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/range_plus_minus_one.fixed19
-rw-r--r--tests/ui/range_plus_minus_one.rs19
-rw-r--r--tests/ui/range_plus_minus_one.stderr18
3 files changed, 47 insertions, 9 deletions
diff --git a/tests/ui/range_plus_minus_one.fixed b/tests/ui/range_plus_minus_one.fixed
index 40d7791df28..a16a3e54d45 100644
--- a/tests/ui/range_plus_minus_one.fixed
+++ b/tests/ui/range_plus_minus_one.fixed
@@ -6,6 +6,22 @@ fn f() -> usize {
     42
 }
 
+macro_rules! macro_plus_one {
+    ($m: literal) => {
+        for i in 0..$m + 1 {
+            println!("{}", i);
+        }
+    };
+}
+
+macro_rules! macro_minus_one {
+    ($m: literal) => {
+        for i in 0..=$m - 1 {
+            println!("{}", i);
+        }
+    };
+}
+
 #[warn(clippy::range_plus_one)]
 #[warn(clippy::range_minus_one)]
 fn main() {
@@ -39,4 +55,7 @@ fn main() {
 
     let mut vec: Vec<()> = std::vec::Vec::new();
     vec.drain(..);
+
+    macro_plus_one!(5);
+    macro_minus_one!(5);
 }
diff --git a/tests/ui/range_plus_minus_one.rs b/tests/ui/range_plus_minus_one.rs
index a8ddd9b5f75..bd6cb4d21be 100644
--- a/tests/ui/range_plus_minus_one.rs
+++ b/tests/ui/range_plus_minus_one.rs
@@ -6,6 +6,22 @@ fn f() -> usize {
     42
 }
 
+macro_rules! macro_plus_one {
+    ($m: literal) => {
+        for i in 0..$m + 1 {
+            println!("{}", i);
+        }
+    };
+}
+
+macro_rules! macro_minus_one {
+    ($m: literal) => {
+        for i in 0..=$m - 1 {
+            println!("{}", i);
+        }
+    };
+}
+
 #[warn(clippy::range_plus_one)]
 #[warn(clippy::range_minus_one)]
 fn main() {
@@ -39,4 +55,7 @@ fn main() {
 
     let mut vec: Vec<()> = std::vec::Vec::new();
     vec.drain(..);
+
+    macro_plus_one!(5);
+    macro_minus_one!(5);
 }
diff --git a/tests/ui/range_plus_minus_one.stderr b/tests/ui/range_plus_minus_one.stderr
index fb4f1658597..0223696243b 100644
--- a/tests/ui/range_plus_minus_one.stderr
+++ b/tests/ui/range_plus_minus_one.stderr
@@ -1,5 +1,5 @@
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:15:14
+  --> $DIR/range_plus_minus_one.rs:31:14
    |
 LL |     for _ in 0..3 + 1 {}
    |              ^^^^^^^^ help: use: `0..=3`
@@ -7,25 +7,25 @@ LL |     for _ in 0..3 + 1 {}
    = note: `-D clippy::range-plus-one` implied by `-D warnings`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:18:14
+  --> $DIR/range_plus_minus_one.rs:34:14
    |
 LL |     for _ in 0..1 + 5 {}
    |              ^^^^^^^^ help: use: `0..=5`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:21:14
+  --> $DIR/range_plus_minus_one.rs:37:14
    |
 LL |     for _ in 1..1 + 1 {}
    |              ^^^^^^^^ help: use: `1..=1`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:27:14
+  --> $DIR/range_plus_minus_one.rs:43:14
    |
 LL |     for _ in 0..(1 + f()) {}
    |              ^^^^^^^^^^^^ help: use: `0..=f()`
 
 error: an exclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:31:13
+  --> $DIR/range_plus_minus_one.rs:47:13
    |
 LL |     let _ = ..=11 - 1;
    |             ^^^^^^^^^ help: use: `..11`
@@ -33,25 +33,25 @@ LL |     let _ = ..=11 - 1;
    = note: `-D clippy::range-minus-one` implied by `-D warnings`
 
 error: an exclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:32:13
+  --> $DIR/range_plus_minus_one.rs:48:13
    |
 LL |     let _ = ..=(11 - 1);
    |             ^^^^^^^^^^^ help: use: `..11`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:33:13
+  --> $DIR/range_plus_minus_one.rs:49:13
    |
 LL |     let _ = (1..11 + 1);
    |             ^^^^^^^^^^^ help: use: `(1..=11)`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:34:13
+  --> $DIR/range_plus_minus_one.rs:50:13
    |
 LL |     let _ = (f() + 1)..(f() + 1);
    |             ^^^^^^^^^^^^^^^^^^^^ help: use: `((f() + 1)..=f())`
 
 error: an inclusive range would be more readable
-  --> $DIR/range_plus_minus_one.rs:38:14
+  --> $DIR/range_plus_minus_one.rs:54:14
    |
 LL |     for _ in 1..ONE + ONE {}
    |              ^^^^^^^^^^^^ help: use: `1..=ONE`