mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Merge #3392
3392: UI test cleanup: Extract for_loop_over_x tests r=matthiaskrgr a=phansch cc #2038 Co-authored-by: Philipp Hansch <dev@phansch.net>
This commit is contained in:
commit
6a165e5a92
4 changed files with 318 additions and 306 deletions
|
@ -7,10 +7,6 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use std::collections::*;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
@ -18,60 +14,6 @@ static STATIC: [usize; 4] = [0, 1, 8, 16];
|
|||
const CONST: [usize; 4] = [0, 1, 8, 16];
|
||||
|
||||
#[warn(clippy::all)]
|
||||
fn for_loop_over_option_and_result() {
|
||||
let option = Some(1);
|
||||
let result = option.ok_or("x not found");
|
||||
let v = vec![0, 1, 2];
|
||||
|
||||
// check FOR_LOOP_OVER_OPTION lint
|
||||
for x in option {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// check FOR_LOOP_OVER_RESULT lint
|
||||
for x in result {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
for x in option.ok_or("x not found") {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
|
||||
// in the chain
|
||||
for x in v.iter().next() {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// make sure we lint when next() is not the last call in the chain
|
||||
for x in v.iter().next().and(Some(0)) {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
for x in v.iter().next().ok_or("x not found") {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// check for false positives
|
||||
|
||||
// for loop false positive
|
||||
for x in v {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// while let false positive for Option
|
||||
while let Some(x) = option {
|
||||
println!("{}", x);
|
||||
break;
|
||||
}
|
||||
|
||||
// while let false positive for Result
|
||||
while let Ok(x) = result {
|
||||
println!("{}", x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct Unrelated(Vec<u8>);
|
||||
impl Unrelated {
|
||||
fn next(&self) -> std::slice::Iter<u8> {
|
||||
|
@ -379,8 +321,6 @@ fn main() {
|
|||
}
|
||||
println!("index: {}", index);
|
||||
|
||||
for_loop_over_option_and_result();
|
||||
|
||||
let m: HashMap<u64, u64> = HashMap::new();
|
||||
for (_, v) in &m {
|
||||
let _v = v;
|
||||
|
|
|
@ -1,489 +1,421 @@
|
|||
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop.rs:27:14
|
||||
|
|
||||
27 | for x in option {
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: `-D clippy::for-loop-over-option` implied by `-D warnings`
|
||||
= help: consider replacing `for x in option` with `if let Some(x) = option`
|
||||
|
||||
error: for loop over `result`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop.rs:32:14
|
||||
|
|
||||
32 | for x in result {
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: `-D clippy::for-loop-over-result` implied by `-D warnings`
|
||||
= help: consider replacing `for x in result` with `if let Ok(x) = result`
|
||||
|
||||
error: for loop over `option.ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop.rs:36:14
|
||||
|
|
||||
36 | for x in option.ok_or("x not found") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
|
||||
|
||||
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
|
||||
--> $DIR/for_loop.rs:42:14
|
||||
|
|
||||
42 | for x in v.iter().next() {
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::iter-next-loop` implied by `-D warnings`
|
||||
|
||||
error: for loop over `v.iter().next().and(Some(0))`, which is an `Option`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop.rs:47:14
|
||||
|
|
||||
47 | for x in v.iter().next().and(Some(0)) {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
|
||||
|
||||
error: for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop.rs:51:14
|
||||
|
|
||||
51 | for x in v.iter().next().ok_or("x not found") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
|
||||
|
||||
error: this loop never actually loops
|
||||
--> $DIR/for_loop.rs:63:5
|
||||
|
|
||||
63 | / while let Some(x) = option {
|
||||
64 | | println!("{}", x);
|
||||
65 | | break;
|
||||
66 | | }
|
||||
| |_____^
|
||||
|
|
||||
= note: `-D clippy::never-loop` implied by `-D warnings`
|
||||
|
||||
error: this loop never actually loops
|
||||
--> $DIR/for_loop.rs:69:5
|
||||
|
|
||||
69 | / while let Ok(x) = result {
|
||||
70 | | println!("{}", x);
|
||||
71 | | break;
|
||||
72 | | }
|
||||
| |_____^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:96:14
|
||||
--> $DIR/for_loop.rs:38:14
|
||||
|
|
||||
96 | for i in 0..vec.len() {
|
||||
38 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::needless-range-loop` implied by `-D warnings`
|
||||
help: consider using an iterator
|
||||
|
|
||||
96 | for <item> in &vec {
|
||||
38 | for <item> in &vec {
|
||||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:105:14
|
||||
|
|
||||
105 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:47:14
|
||||
|
|
||||
47 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
105 | for <item> in &vec {
|
||||
| ^^^^^^ ^^^^
|
||||
|
|
||||
47 | for <item> in &vec {
|
||||
| ^^^^^^ ^^^^
|
||||
|
||||
error: the loop variable `j` is only used to index `STATIC`.
|
||||
--> $DIR/for_loop.rs:110:14
|
||||
|
|
||||
110 | for j in 0..4 {
|
||||
| ^^^^
|
||||
--> $DIR/for_loop.rs:52:14
|
||||
|
|
||||
52 | for j in 0..4 {
|
||||
| ^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
110 | for <item> in &STATIC {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
|
||||
52 | for <item> in &STATIC {
|
||||
| ^^^^^^ ^^^^^^^
|
||||
|
||||
error: the loop variable `j` is only used to index `CONST`.
|
||||
--> $DIR/for_loop.rs:114:14
|
||||
|
|
||||
114 | for j in 0..4 {
|
||||
| ^^^^
|
||||
--> $DIR/for_loop.rs:56:14
|
||||
|
|
||||
56 | for j in 0..4 {
|
||||
| ^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
114 | for <item> in &CONST {
|
||||
| ^^^^^^ ^^^^^^
|
||||
|
|
||||
56 | for <item> in &CONST {
|
||||
| ^^^^^^ ^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/for_loop.rs:118:14
|
||||
|
|
||||
118 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:60:14
|
||||
|
|
||||
60 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
118 | for (i, <item>) in vec.iter().enumerate() {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
60 | for (i, <item>) in vec.iter().enumerate() {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec2`.
|
||||
--> $DIR/for_loop.rs:126:14
|
||||
|
|
||||
126 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:68:14
|
||||
|
|
||||
68 | for i in 0..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
126 | for <item> in vec2.iter().take(vec.len()) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
68 | for <item> in vec2.iter().take(vec.len()) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:130:14
|
||||
|
|
||||
130 | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:72:14
|
||||
|
|
||||
72 | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
130 | for <item> in vec.iter().skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
72 | for <item> in vec.iter().skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:134:14
|
||||
|
|
||||
134 | for i in 0..MAX_LEN {
|
||||
| ^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:76:14
|
||||
|
|
||||
76 | for i in 0..MAX_LEN {
|
||||
| ^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
134 | for <item> in vec.iter().take(MAX_LEN) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
76 | for <item> in vec.iter().take(MAX_LEN) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:138:14
|
||||
|
|
||||
138 | for i in 0..=MAX_LEN {
|
||||
| ^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:80:14
|
||||
|
|
||||
80 | for i in 0..=MAX_LEN {
|
||||
| ^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
138 | for <item> in vec.iter().take(MAX_LEN + 1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
80 | for <item> in vec.iter().take(MAX_LEN + 1) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:142:14
|
||||
|
|
||||
142 | for i in 5..10 {
|
||||
| ^^^^^
|
||||
--> $DIR/for_loop.rs:84:14
|
||||
|
|
||||
84 | for i in 5..10 {
|
||||
| ^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
142 | for <item> in vec.iter().take(10).skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
84 | for <item> in vec.iter().take(10).skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is only used to index `vec`.
|
||||
--> $DIR/for_loop.rs:146:14
|
||||
|
|
||||
146 | for i in 5..=10 {
|
||||
| ^^^^^^
|
||||
--> $DIR/for_loop.rs:88:14
|
||||
|
|
||||
88 | for i in 5..=10 {
|
||||
| ^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
146 | for <item> in vec.iter().take(10 + 1).skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
88 | for <item> in vec.iter().take(10 + 1).skip(5) {
|
||||
| ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/for_loop.rs:150:14
|
||||
|
|
||||
150 | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
--> $DIR/for_loop.rs:92:14
|
||||
|
|
||||
92 | for i in 5..vec.len() {
|
||||
| ^^^^^^^^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
150 | for (i, <item>) in vec.iter().enumerate().skip(5) {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
92 | for (i, <item>) in vec.iter().enumerate().skip(5) {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: the loop variable `i` is used to index `vec`
|
||||
--> $DIR/for_loop.rs:154:14
|
||||
|
|
||||
154 | for i in 5..10 {
|
||||
| ^^^^^
|
||||
--> $DIR/for_loop.rs:96:14
|
||||
|
|
||||
96 | for i in 5..10 {
|
||||
| ^^^^^
|
||||
help: consider using an iterator
|
||||
|
|
||||
154 | for (i, <item>) in vec.iter().enumerate().take(10).skip(5) {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
96 | for (i, <item>) in vec.iter().enumerate().take(10).skip(5) {
|
||||
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:158:14
|
||||
--> $DIR/for_loop.rs:100:14
|
||||
|
|
||||
158 | for i in 10..0 {
|
||||
100 | for i in 10..0 {
|
||||
| ^^^^^
|
||||
|
|
||||
= note: `-D clippy::reverse-range-loop` implied by `-D warnings`
|
||||
help: consider using the following if you are attempting to iterate over this range in reverse
|
||||
|
|
||||
158 | for i in (0..10).rev() {
|
||||
100 | for i in (0..10).rev() {
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:162:14
|
||||
--> $DIR/for_loop.rs:104:14
|
||||
|
|
||||
162 | for i in 10..=0 {
|
||||
104 | for i in 10..=0 {
|
||||
| ^^^^^^
|
||||
help: consider using the following if you are attempting to iterate over this range in reverse
|
||||
|
|
||||
162 | for i in (0...10).rev() {
|
||||
104 | for i in (0...10).rev() {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:166:14
|
||||
--> $DIR/for_loop.rs:108:14
|
||||
|
|
||||
166 | for i in MAX_LEN..0 {
|
||||
108 | for i in MAX_LEN..0 {
|
||||
| ^^^^^^^^^^
|
||||
help: consider using the following if you are attempting to iterate over this range in reverse
|
||||
|
|
||||
166 | for i in (0..MAX_LEN).rev() {
|
||||
108 | for i in (0..MAX_LEN).rev() {
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:170:14
|
||||
--> $DIR/for_loop.rs:112:14
|
||||
|
|
||||
170 | for i in 5..5 {
|
||||
112 | for i in 5..5 {
|
||||
| ^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:195:14
|
||||
--> $DIR/for_loop.rs:137:14
|
||||
|
|
||||
195 | for i in 10..5 + 4 {
|
||||
137 | for i in 10..5 + 4 {
|
||||
| ^^^^^^^^^
|
||||
help: consider using the following if you are attempting to iterate over this range in reverse
|
||||
|
|
||||
195 | for i in (5 + 4..10).rev() {
|
||||
137 | for i in (5 + 4..10).rev() {
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:199:14
|
||||
--> $DIR/for_loop.rs:141:14
|
||||
|
|
||||
199 | for i in (5 + 2)..(3 - 1) {
|
||||
141 | for i in (5 + 2)..(3 - 1) {
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
help: consider using the following if you are attempting to iterate over this range in reverse
|
||||
|
|
||||
199 | for i in ((3 - 1)..(5 + 2)).rev() {
|
||||
141 | for i in ((3 - 1)..(5 + 2)).rev() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: this range is empty so this for loop will never run
|
||||
--> $DIR/for_loop.rs:203:14
|
||||
--> $DIR/for_loop.rs:145:14
|
||||
|
|
||||
203 | for i in (5 + 2)..(8 - 1) {
|
||||
145 | for i in (5 + 2)..(8 - 1) {
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:225:15
|
||||
--> $DIR/for_loop.rs:167:15
|
||||
|
|
||||
225 | for _v in vec.iter() {}
|
||||
167 | for _v in vec.iter() {}
|
||||
| ^^^^^^^^^^ help: to write this more concisely, try: `&vec`
|
||||
|
|
||||
= note: `-D clippy::explicit-iter-loop` implied by `-D warnings`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:227:15
|
||||
--> $DIR/for_loop.rs:169:15
|
||||
|
|
||||
227 | for _v in vec.iter_mut() {}
|
||||
169 | for _v in vec.iter_mut() {}
|
||||
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&mut vec`
|
||||
|
||||
error: it is more concise to loop over containers instead of using explicit iteration methods`
|
||||
--> $DIR/for_loop.rs:230:15
|
||||
--> $DIR/for_loop.rs:172:15
|
||||
|
|
||||
230 | for _v in out_vec.into_iter() {}
|
||||
172 | for _v in out_vec.into_iter() {}
|
||||
| ^^^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `out_vec`
|
||||
|
|
||||
= note: `-D clippy::explicit-into-iter-loop` implied by `-D warnings`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:233:15
|
||||
--> $DIR/for_loop.rs:175:15
|
||||
|
|
||||
233 | for _v in array.into_iter() {}
|
||||
175 | for _v in array.into_iter() {}
|
||||
| ^^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&array`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:238:15
|
||||
--> $DIR/for_loop.rs:180:15
|
||||
|
|
||||
238 | for _v in [1, 2, 3].iter() {}
|
||||
180 | for _v in [1, 2, 3].iter() {}
|
||||
| ^^^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[1, 2, 3]`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:242:15
|
||||
--> $DIR/for_loop.rs:184:15
|
||||
|
|
||||
242 | for _v in [0; 32].iter() {}
|
||||
184 | for _v in [0; 32].iter() {}
|
||||
| ^^^^^^^^^^^^^^ help: to write this more concisely, try: `&[0; 32]`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:247:15
|
||||
--> $DIR/for_loop.rs:189:15
|
||||
|
|
||||
247 | for _v in ll.iter() {}
|
||||
189 | for _v in ll.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&ll`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:250:15
|
||||
--> $DIR/for_loop.rs:192:15
|
||||
|
|
||||
250 | for _v in vd.iter() {}
|
||||
192 | for _v in vd.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&vd`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:253:15
|
||||
--> $DIR/for_loop.rs:195:15
|
||||
|
|
||||
253 | for _v in bh.iter() {}
|
||||
195 | for _v in bh.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bh`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:256:15
|
||||
--> $DIR/for_loop.rs:198:15
|
||||
|
|
||||
256 | for _v in hm.iter() {}
|
||||
198 | for _v in hm.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&hm`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:259:15
|
||||
--> $DIR/for_loop.rs:201:15
|
||||
|
|
||||
259 | for _v in bt.iter() {}
|
||||
201 | for _v in bt.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bt`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:262:15
|
||||
--> $DIR/for_loop.rs:204:15
|
||||
|
|
||||
262 | for _v in hs.iter() {}
|
||||
204 | for _v in hs.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&hs`
|
||||
|
||||
error: it is more concise to loop over references to containers instead of using explicit iteration methods
|
||||
--> $DIR/for_loop.rs:265:15
|
||||
--> $DIR/for_loop.rs:207:15
|
||||
|
|
||||
265 | for _v in bs.iter() {}
|
||||
207 | for _v in bs.iter() {}
|
||||
| ^^^^^^^^^ help: to write this more concisely, try: `&bs`
|
||||
|
||||
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
|
||||
--> $DIR/for_loop.rs:267:15
|
||||
--> $DIR/for_loop.rs:209:15
|
||||
|
|
||||
267 | for _v in vec.iter().next() {}
|
||||
209 | for _v in vec.iter().next() {}
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::iter-next-loop` implied by `-D warnings`
|
||||
|
||||
error: you are collect()ing an iterator and throwing away the result. Consider using an explicit for loop to exhaust the iterator
|
||||
--> $DIR/for_loop.rs:274:5
|
||||
--> $DIR/for_loop.rs:216:5
|
||||
|
|
||||
274 | vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
|
||||
216 | vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D clippy::unused-collect` implied by `-D warnings`
|
||||
|
||||
error: you seem to want to iterate on a map's values
|
||||
--> $DIR/for_loop.rs:385:19
|
||||
--> $DIR/for_loop.rs:325:19
|
||||
|
|
||||
385 | for (_, v) in &m {
|
||||
325 | for (_, v) in &m {
|
||||
| ^^
|
||||
|
|
||||
= note: `-D clippy::for-kv-map` implied by `-D warnings`
|
||||
help: use the corresponding method
|
||||
|
|
||||
385 | for v in m.values() {
|
||||
325 | for v in m.values() {
|
||||
| ^ ^^^^^^^^^^
|
||||
|
||||
error: you seem to want to iterate on a map's values
|
||||
--> $DIR/for_loop.rs:390:19
|
||||
--> $DIR/for_loop.rs:330:19
|
||||
|
|
||||
390 | for (_, v) in &*m {
|
||||
330 | for (_, v) in &*m {
|
||||
| ^^^
|
||||
help: use the corresponding method
|
||||
|
|
||||
390 | for v in (*m).values() {
|
||||
330 | for v in (*m).values() {
|
||||
| ^ ^^^^^^^^^^^^^
|
||||
|
||||
error: you seem to want to iterate on a map's values
|
||||
--> $DIR/for_loop.rs:398:19
|
||||
--> $DIR/for_loop.rs:338:19
|
||||
|
|
||||
398 | for (_, v) in &mut m {
|
||||
338 | for (_, v) in &mut m {
|
||||
| ^^^^^^
|
||||
help: use the corresponding method
|
||||
|
|
||||
398 | for v in m.values_mut() {
|
||||
338 | for v in m.values_mut() {
|
||||
| ^ ^^^^^^^^^^^^^^
|
||||
|
||||
error: you seem to want to iterate on a map's values
|
||||
--> $DIR/for_loop.rs:403:19
|
||||
--> $DIR/for_loop.rs:343:19
|
||||
|
|
||||
403 | for (_, v) in &mut *m {
|
||||
343 | for (_, v) in &mut *m {
|
||||
| ^^^^^^^
|
||||
help: use the corresponding method
|
||||
|
|
||||
403 | for v in (*m).values_mut() {
|
||||
343 | for v in (*m).values_mut() {
|
||||
| ^ ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: you seem to want to iterate on a map's keys
|
||||
--> $DIR/for_loop.rs:409:24
|
||||
--> $DIR/for_loop.rs:349:24
|
||||
|
|
||||
409 | for (k, _value) in rm {
|
||||
349 | for (k, _value) in rm {
|
||||
| ^^
|
||||
help: use the corresponding method
|
||||
|
|
||||
409 | for k in rm.keys() {
|
||||
349 | for k in rm.keys() {
|
||||
| ^ ^^^^^^^^^
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:462:14
|
||||
--> $DIR/for_loop.rs:402:14
|
||||
|
|
||||
462 | for i in 0..src.len() {
|
||||
402 | for i in 0..src.len() {
|
||||
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`
|
||||
|
|
||||
= note: `-D clippy::manual-memcpy` implied by `-D warnings`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:467:14
|
||||
--> $DIR/for_loop.rs:407:14
|
||||
|
|
||||
467 | for i in 0..src.len() {
|
||||
407 | for i in 0..src.len() {
|
||||
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[10..(src.len() + 10)].clone_from_slice(&src[..])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:472:14
|
||||
--> $DIR/for_loop.rs:412:14
|
||||
|
|
||||
472 | for i in 0..src.len() {
|
||||
412 | for i in 0..src.len() {
|
||||
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[10..])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:477:14
|
||||
--> $DIR/for_loop.rs:417:14
|
||||
|
|
||||
477 | for i in 11..src.len() {
|
||||
417 | for i in 11..src.len() {
|
||||
| ^^^^^^^^^^^^^ help: try replacing the loop by: `dst[11..src.len()].clone_from_slice(&src[(11 - 10)..(src.len() - 10)])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:482:14
|
||||
--> $DIR/for_loop.rs:422:14
|
||||
|
|
||||
482 | for i in 0..dst.len() {
|
||||
422 | for i in 0..dst.len() {
|
||||
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst.clone_from_slice(&src[..dst.len()])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:495:14
|
||||
--> $DIR/for_loop.rs:435:14
|
||||
|
|
||||
495 | for i in 10..256 {
|
||||
435 | for i in 10..256 {
|
||||
| ^^^^^^^
|
||||
help: try replacing the loop by
|
||||
|
|
||||
495 | for i in dst[10..256].clone_from_slice(&src[(10 - 5)..(256 - 5)])
|
||||
496 | dst2[(10 + 500)..(256 + 500)].clone_from_slice(&src[10..256]) {
|
||||
435 | for i in dst[10..256].clone_from_slice(&src[(10 - 5)..(256 - 5)])
|
||||
436 | dst2[(10 + 500)..(256 + 500)].clone_from_slice(&src[10..256]) {
|
||||
|
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:507:14
|
||||
--> $DIR/for_loop.rs:447:14
|
||||
|
|
||||
507 | for i in 10..LOOP_OFFSET {
|
||||
447 | for i in 10..LOOP_OFFSET {
|
||||
| ^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[(10 + LOOP_OFFSET)..(LOOP_OFFSET + LOOP_OFFSET)].clone_from_slice(&src[(10 - some_var)..(LOOP_OFFSET - some_var)])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:520:14
|
||||
--> $DIR/for_loop.rs:460:14
|
||||
|
|
||||
520 | for i in 0..src_vec.len() {
|
||||
460 | for i in 0..src_vec.len() {
|
||||
| ^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst_vec[..src_vec.len()].clone_from_slice(&src_vec[..])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:549:14
|
||||
--> $DIR/for_loop.rs:489:14
|
||||
|
|
||||
549 | for i in from..from + src.len() {
|
||||
489 | for i in from..from + src.len() {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + src.len()].clone_from_slice(&src[0..(from + src.len() - from)])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:553:14
|
||||
--> $DIR/for_loop.rs:493:14
|
||||
|
|
||||
553 | for i in from..from + 3 {
|
||||
493 | for i in from..from + 3 {
|
||||
| ^^^^^^^^^^^^^^ help: try replacing the loop by: `dst[from..from + 3].clone_from_slice(&src[0..(from + 3 - from)])`
|
||||
|
||||
error: it looks like you're manually copying between slices
|
||||
--> $DIR/for_loop.rs:560:14
|
||||
--> $DIR/for_loop.rs:500:14
|
||||
|
|
||||
560 | for i in 0..src.len() {
|
||||
500 | for i in 0..src.len() {
|
||||
| ^^^^^^^^^^^^ help: try replacing the loop by: `dst[..src.len()].clone_from_slice(&src[..])`
|
||||
|
||||
error: aborting due to 59 previous errors
|
||||
error: aborting due to 51 previous errors
|
||||
|
||||
|
|
68
tests/ui/for_loop_over_option_result.rs
Normal file
68
tests/ui/for_loop_over_option_result.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![warn(clippy::for_loop_over_option, clippy::for_loop_over_result)]
|
||||
|
||||
/// Tests for_loop_over_result and for_loop_over_option
|
||||
|
||||
fn for_loop_over_option_and_result() {
|
||||
let option = Some(1);
|
||||
let result = option.ok_or("x not found");
|
||||
let v = vec![0, 1, 2];
|
||||
|
||||
// check FOR_LOOP_OVER_OPTION lint
|
||||
for x in option {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// check FOR_LOOP_OVER_RESULT lint
|
||||
for x in result {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
for x in option.ok_or("x not found") {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
|
||||
// in the chain
|
||||
for x in v.iter().next() {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// make sure we lint when next() is not the last call in the chain
|
||||
for x in v.iter().next().and(Some(0)) {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
for x in v.iter().next().ok_or("x not found") {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// check for false positives
|
||||
|
||||
// for loop false positive
|
||||
for x in v {
|
||||
println!("{}", x);
|
||||
}
|
||||
|
||||
// while let false positive for Option
|
||||
while let Some(x) = option {
|
||||
println!("{}", x);
|
||||
break;
|
||||
}
|
||||
|
||||
// while let false positive for Result
|
||||
while let Ok(x) = result {
|
||||
println!("{}", x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
72
tests/ui/for_loop_over_option_result.stderr
Normal file
72
tests/ui/for_loop_over_option_result.stderr
Normal file
|
@ -0,0 +1,72 @@
|
|||
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop_over_option_result.rs:20:14
|
||||
|
|
||||
20 | for x in option {
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: `-D clippy::for-loop-over-option` implied by `-D warnings`
|
||||
= help: consider replacing `for x in option` with `if let Some(x) = option`
|
||||
|
||||
error: for loop over `result`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop_over_option_result.rs:25:14
|
||||
|
|
||||
25 | for x in result {
|
||||
| ^^^^^^
|
||||
|
|
||||
= note: `-D clippy::for-loop-over-result` implied by `-D warnings`
|
||||
= help: consider replacing `for x in result` with `if let Ok(x) = result`
|
||||
|
||||
error: for loop over `option.ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop_over_option_result.rs:29:14
|
||||
|
|
||||
29 | for x in option.ok_or("x not found") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
|
||||
|
||||
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
|
||||
--> $DIR/for_loop_over_option_result.rs:35:14
|
||||
|
|
||||
35 | for x in v.iter().next() {
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: #[deny(clippy::iter_next_loop)] on by default
|
||||
|
||||
error: for loop over `v.iter().next().and(Some(0))`, which is an `Option`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop_over_option_result.rs:40:14
|
||||
|
|
||||
40 | for x in v.iter().next().and(Some(0)) {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
|
||||
|
||||
error: for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
|
||||
--> $DIR/for_loop_over_option_result.rs:44:14
|
||||
|
|
||||
44 | for x in v.iter().next().ok_or("x not found") {
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
|
||||
|
||||
error: this loop never actually loops
|
||||
--> $DIR/for_loop_over_option_result.rs:56:5
|
||||
|
|
||||
56 | / while let Some(x) = option {
|
||||
57 | | println!("{}", x);
|
||||
58 | | break;
|
||||
59 | | }
|
||||
| |_____^
|
||||
|
|
||||
= note: #[deny(clippy::never_loop)] on by default
|
||||
|
||||
error: this loop never actually loops
|
||||
--> $DIR/for_loop_over_option_result.rs:62:5
|
||||
|
|
||||
62 | / while let Ok(x) = result {
|
||||
63 | | println!("{}", x);
|
||||
64 | | break;
|
||||
65 | | }
|
||||
| |_____^
|
||||
|
||||
error: aborting due to 8 previous errors
|
||||
|
Loading…
Reference in a new issue