2018-10-06 18:18:06 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2018-10-11 12:16:22 +02:00
|
|
|
|
2017-09-18 12:47:33 +02:00
|
|
|
|
2015-08-12 21:56:27 +02:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
use std::collections::*;
|
2016-07-01 20:55:45 +02:00
|
|
|
use std::rc::Rc;
|
2015-08-25 18:26:20 +02:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
static STATIC: [usize; 4] = [0, 1, 8, 16];
|
|
|
|
const CONST: [usize; 4] = [0, 1, 8, 16];
|
2016-03-07 23:24:11 +01:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[warn(clippy::all)]
|
2016-01-30 00:15:57 +01:00
|
|
|
fn for_loop_over_option_and_result() {
|
2016-01-29 08:34:09 +01:00
|
|
|
let option = Some(1);
|
2016-01-30 00:15:57 +01:00
|
|
|
let result = option.ok_or("x not found");
|
2017-09-05 12:53:52 +02:00
|
|
|
let v = vec![0, 1, 2];
|
2016-01-29 08:34:09 +01:00
|
|
|
|
|
|
|
// check FOR_LOOP_OVER_OPTION lint
|
2018-05-11 19:05:34 +02:00
|
|
|
for x in option {
|
2016-01-29 08:34:09 +01:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:15:57 +01:00
|
|
|
// check FOR_LOOP_OVER_RESULT lint
|
|
|
|
for x in result {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in option.ok_or("x not found") {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
// make sure LOOP_OVER_NEXT lint takes clippy::precedence when next() is the last call
|
2017-09-05 12:53:52 +02:00
|
|
|
// in the chain
|
2016-01-29 08:34:09 +01:00
|
|
|
for x in v.iter().next() {
|
2016-01-30 00:15:57 +01:00
|
|
|
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") {
|
2016-01-29 08:34:09 +01:00
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for false positives
|
|
|
|
|
|
|
|
// for loop false positive
|
|
|
|
for x in v {
|
|
|
|
println!("{}", x);
|
|
|
|
}
|
|
|
|
|
2016-01-30 00:15:57 +01:00
|
|
|
// while let false positive for Option
|
2016-01-29 08:34:09 +01:00
|
|
|
while let Some(x) = option {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-30 00:15:57 +01:00
|
|
|
|
2016-02-07 18:10:03 +01:00
|
|
|
// while let false positive for Result
|
2016-01-30 00:15:57 +01:00
|
|
|
while let Ok(x) = result {
|
|
|
|
println!("{}", x);
|
|
|
|
break;
|
|
|
|
}
|
2016-01-29 08:34:09 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 07:23:57 +02:00
|
|
|
struct Unrelated(Vec<u8>);
|
|
|
|
impl Unrelated {
|
|
|
|
fn next(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-25 18:26:20 +02:00
|
|
|
|
|
|
|
fn iter(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-17 07:23:57 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[warn(clippy::needless_range_loop, clippy::explicit_iter_loop, clippy::explicit_into_iter_loop, clippy::iter_next_loop, clippy::reverse_range_loop,
|
|
|
|
clippy::explicit_counter_loop, clippy::for_kv_map)]
|
|
|
|
#[warn(clippy::unused_collect)]
|
|
|
|
#[allow(clippy::linkedlist, clippy::shadow_unrelated, clippy::unnecessary_mut_passed, clippy::cyclomatic_complexity, clippy::similar_names)]
|
|
|
|
#[allow(clippy::many_single_char_names, unused_variables)]
|
2015-08-12 21:56:27 +02:00
|
|
|
fn main() {
|
2016-02-07 18:10:03 +01:00
|
|
|
const MAX_LEN: usize = 42;
|
|
|
|
|
2015-08-13 15:36:31 +02:00
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
2015-08-12 21:56:27 +02:00
|
|
|
let vec2 = vec![1, 2, 3, 4];
|
2016-01-14 20:58:32 +01:00
|
|
|
for i in 0..vec.len() {
|
2015-08-12 21:56:27 +02:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
2016-03-07 23:24:11 +01:00
|
|
|
|
2016-07-10 14:05:57 +02:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
let i = 42; // make a different `i`
|
|
|
|
println!("{}", vec[i]); // ok, not the `i` of the for-loop
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
let _ = vec[i];
|
|
|
|
}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2016-03-07 23:24:11 +01:00
|
|
|
// ICE #746
|
|
|
|
for j in 0..4 {
|
|
|
|
println!("{:?}", STATIC[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for j in 0..4 {
|
|
|
|
println!("{:?}", CONST[j]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:58:32 +01:00
|
|
|
for i in 0..vec.len() {
|
2015-08-12 21:56:27 +02:00
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
// not an error, indexing more than one variable
|
2015-08-12 21:56:27 +02:00
|
|
|
println!("{} {}", vec[i], vec2[i]);
|
|
|
|
}
|
2015-08-13 15:36:31 +02:00
|
|
|
|
2016-01-14 21:04:34 +01:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
println!("{}", vec2[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:58:32 +01:00
|
|
|
for i in 5..vec.len() {
|
2015-09-02 12:41:51 +02:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-02-07 18:10:03 +01:00
|
|
|
for i in 0..MAX_LEN {
|
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:40:19 +02:00
|
|
|
for i in 0..=MAX_LEN {
|
2016-03-07 16:55:12 +01:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:58:32 +01:00
|
|
|
for i in 5..10 {
|
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:40:19 +02:00
|
|
|
for i in 5..=10 {
|
2016-03-07 16:55:12 +01:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:58:32 +01:00
|
|
|
for i in 5..vec.len() {
|
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 5..10 {
|
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
2016-02-07 18:10:03 +01:00
|
|
|
for i in 10..0 {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:40:19 +02:00
|
|
|
for i in 10..=0 {
|
2016-03-07 16:55:12 +01:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:58:07 +01:00
|
|
|
for i in MAX_LEN..0 {
|
2015-09-15 02:19:05 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-02-08 14:58:07 +01:00
|
|
|
for i in 5..5 {
|
2015-09-15 02:19:05 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:40:19 +02:00
|
|
|
for i in 5..=5 {
|
2017-09-05 12:53:52 +02:00
|
|
|
// not an error, this is the range with only one element “5”
|
2016-03-07 16:55:12 +01:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in 0..10 {
|
|
|
|
// not an error, the start index is less than the end index
|
2015-09-15 02:19:05 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in -10..0 {
|
|
|
|
// not an error
|
2015-10-26 07:43:38 +01:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in (10..0).map(|x| x * 2) {
|
|
|
|
// not an error, it can't be known what arbitrary methods do to a range
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing that the empty range lint folds constants
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in 10..5 + 4 {
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in (5 + 2)..(3 - 1) {
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in (5 + 2)..(8 - 1) {
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in (2 * 2)..(2 * 3) {
|
|
|
|
// no error, 4..6 is fine
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = 42;
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in x..10 {
|
|
|
|
// no error, not constant-foldable
|
2015-09-15 07:20:56 +02:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2016-02-13 22:09:17 +01:00
|
|
|
// See #601
|
2017-09-05 12:53:52 +02:00
|
|
|
for i in 0..10 {
|
|
|
|
// no error, id_col does not exist outside the loop
|
2016-02-13 22:09:17 +01:00
|
|
|
let mut id_col = vec![0f64; 10];
|
|
|
|
id_col[i] = 1f64;
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in vec.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in vec.iter_mut() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
let out_vec = vec![1, 2, 3];
|
|
|
|
for _v in out_vec.into_iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2017-01-09 16:59:55 +01:00
|
|
|
let array = [1, 2, 3];
|
2017-01-30 14:25:25 +01:00
|
|
|
for _v in array.into_iter() {}
|
2017-01-09 16:59:55 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {} // these are fine
|
|
|
|
for _v in &mut vec {} // these are fine
|
2015-08-17 07:23:57 +02:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in [1, 2, 3].iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in (&mut [1, 2, 3]).iter() {} // no error
|
2017-01-28 14:02:49 +01:00
|
|
|
|
|
|
|
for _v in [0; 32].iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-09-06 13:36:21 +02:00
|
|
|
for _v in [0; 33].iter() {} // no error
|
2017-01-28 14:02:49 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let ll: LinkedList<()> = LinkedList::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in ll.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let vd: VecDeque<()> = VecDeque::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in vd.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in bh.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let hm: HashMap<(), ()> = HashMap::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in hm.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let bt: BTreeMap<(), ()> = BTreeMap::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in bt.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let hs: HashSet<()> = HashSet::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in hs.iter() {}
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-25 18:26:20 +02:00
|
|
|
let bs: BTreeSet<()> = BTreeSet::new();
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in bs.iter() {}
|
2017-01-28 14:02:49 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in vec.iter().next() {}
|
2015-08-17 07:23:57 +02:00
|
|
|
|
|
|
|
let u = Unrelated(vec![]);
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in u.next() {} // no error
|
|
|
|
for _v in u.iter() {} // no error
|
2015-08-30 13:10:59 +02:00
|
|
|
|
|
|
|
let mut out = vec![];
|
2017-07-10 15:30:28 +02:00
|
|
|
vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>();
|
|
|
|
let _y = vec.iter().cloned().map(|x| out.push(x)).collect::<Vec<_>>(); // this is fine
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
// Loop with explicit counter variable
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 1;
|
|
|
|
_index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
// Potential false positives
|
|
|
|
let mut _index = 0;
|
|
|
|
_index = 1;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
let mut _index = 1;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index *= 2;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index = 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index = 0;
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
for _x in 0..1 {
|
|
|
|
_index += 1;
|
|
|
|
}
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for x in &vec {
|
|
|
|
if *x == 1 {
|
|
|
|
_index += 1
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 19:25:45 +02:00
|
|
|
|
|
|
|
let mut _index = 1;
|
2017-09-05 12:53:52 +02:00
|
|
|
if false {
|
|
|
|
_index = 0
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-09-10 14:33:29 +02:00
|
|
|
|
2015-12-19 01:04:33 +01:00
|
|
|
let mut index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
{
|
|
|
|
let mut _x = &mut index;
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-11-26 00:09:01 +01:00
|
|
|
|
|
|
|
let mut index = 0;
|
2017-09-05 12:53:52 +02:00
|
|
|
for _v in &vec {
|
|
|
|
index += 1
|
|
|
|
}
|
2015-11-26 00:09:01 +01:00
|
|
|
println!("index: {}", index);
|
2016-01-29 08:34:09 +01:00
|
|
|
|
2016-01-30 00:15:57 +01:00
|
|
|
for_loop_over_option_and_result();
|
2016-01-19 21:10:00 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
let m: HashMap<u64, u64> = HashMap::new();
|
2016-01-19 21:10:00 +01:00
|
|
|
for (_, v) in &m {
|
2016-07-01 20:55:45 +02:00
|
|
|
let _v = v;
|
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
let m: Rc<HashMap<u64, u64>> = Rc::new(HashMap::new());
|
2016-07-01 20:55:45 +02:00
|
|
|
for (_, v) in &*m {
|
2016-01-19 21:10:00 +01:00
|
|
|
let _v = v;
|
2018-07-25 20:02:52 +02:00
|
|
|
// Here the `*` is not actually necessary, but the test tests that we don't
|
2017-09-05 12:53:52 +02:00
|
|
|
// suggest
|
2016-07-01 20:55:45 +02:00
|
|
|
// `in *m.values()` as we used to
|
2016-01-19 21:10:00 +01:00
|
|
|
}
|
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
let mut m: HashMap<u64, u64> = HashMap::new();
|
2016-02-26 12:45:55 +01:00
|
|
|
for (_, v) in &mut m {
|
|
|
|
let _v = v;
|
|
|
|
}
|
|
|
|
|
2017-01-10 08:33:20 +01:00
|
|
|
let m: &mut HashMap<u64, u64> = &mut HashMap::new();
|
|
|
|
for (_, v) in &mut *m {
|
|
|
|
let _v = v;
|
|
|
|
}
|
2016-02-26 12:45:55 +01:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
let m: HashMap<u64, u64> = HashMap::new();
|
2016-01-19 21:10:00 +01:00
|
|
|
let rm = &m;
|
2016-02-05 19:14:02 +01:00
|
|
|
for (k, _value) in rm {
|
2016-01-19 21:10:00 +01:00
|
|
|
let _k = k;
|
|
|
|
}
|
2016-02-05 19:14:02 +01:00
|
|
|
|
|
|
|
test_for_kv_map();
|
2017-07-10 15:30:28 +02:00
|
|
|
|
2017-09-05 12:53:52 +02:00
|
|
|
fn f<T>(_: &T, _: &T) -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn g<T>(_: &mut [T], _: usize, _: usize) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2017-07-10 15:30:28 +02:00
|
|
|
for i in 1..vec.len() {
|
|
|
|
if f(&vec[i - 1], &vec[i]) {
|
|
|
|
g(&mut vec, i - 1, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for mid in 1..vec.len() {
|
|
|
|
let (_, _) = vec.split_at(mid);
|
|
|
|
}
|
2016-02-05 19:14:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[allow(clippy::used_underscore_binding)]
|
2016-02-05 19:14:02 +01:00
|
|
|
fn test_for_kv_map() {
|
2017-09-05 12:53:52 +02:00
|
|
|
let m: HashMap<u64, u64> = HashMap::new();
|
2016-02-05 19:14:02 +01:00
|
|
|
|
|
|
|
// No error, _value is actually used
|
|
|
|
for (k, _value) in &m {
|
|
|
|
let _ = _value;
|
|
|
|
let _k = k;
|
|
|
|
}
|
2015-08-12 21:56:27 +02:00
|
|
|
}
|
2017-07-10 15:30:28 +02:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2017-09-05 12:53:52 +02:00
|
|
|
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
|
2017-07-10 15:30:28 +02:00
|
|
|
let pivot = v.len() - 1;
|
|
|
|
let mut i = 0;
|
|
|
|
for j in 0..pivot {
|
|
|
|
if v[j] <= v[pivot] {
|
|
|
|
v.swap(i, j);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.swap(i, pivot);
|
|
|
|
i
|
|
|
|
}
|
2017-09-05 12:53:52 +02:00
|
|
|
|
|
|
|
const LOOP_OFFSET: usize = 5000;
|
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[warn(clippy::needless_range_loop)]
|
2017-09-05 12:53:52 +02:00
|
|
|
pub fn manual_copy(src: &[i32], dst: &mut [i32], dst2: &mut [i32]) {
|
|
|
|
// plain manual memcpy
|
|
|
|
for i in 0..src.len() {
|
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// dst offset memcpy
|
|
|
|
for i in 0..src.len() {
|
|
|
|
dst[i + 10] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// src offset memcpy
|
|
|
|
for i in 0..src.len() {
|
|
|
|
dst[i] = src[i + 10];
|
|
|
|
}
|
|
|
|
|
|
|
|
// src offset memcpy
|
|
|
|
for i in 11..src.len() {
|
|
|
|
dst[i] = src[i - 10];
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite entire dst
|
|
|
|
for i in 0..dst.len() {
|
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// manual copy with branch - can't easily convert to memcpy!
|
|
|
|
for i in 0..src.len() {
|
|
|
|
dst[i] = src[i];
|
|
|
|
if dst[i] > 5 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiple copies - suggest two memcpy statements
|
|
|
|
for i in 10..256 {
|
|
|
|
dst[i] = src[i - 5];
|
|
|
|
dst2[i + 500] = src[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a reversal - the copy lint shouldn't be triggered
|
|
|
|
for i in 10..LOOP_OFFSET {
|
|
|
|
dst[i + LOOP_OFFSET] = src[LOOP_OFFSET - i];
|
|
|
|
}
|
|
|
|
|
|
|
|
let some_var = 5;
|
|
|
|
// Offset in variable
|
|
|
|
for i in 10..LOOP_OFFSET {
|
|
|
|
dst[i + LOOP_OFFSET] = src[i - some_var];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non continuous copy - don't trigger lint
|
|
|
|
for i in 0..10 {
|
|
|
|
dst[i + i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
let src_vec = vec![1, 2, 3, 4, 5];
|
|
|
|
let mut dst_vec = vec![0, 0, 0, 0, 0];
|
|
|
|
|
|
|
|
// make sure vectors are supported
|
|
|
|
for i in 0..src_vec.len() {
|
|
|
|
dst_vec[i] = src_vec[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// lint should not trigger when either
|
|
|
|
// source or destination type is not
|
|
|
|
// slice-like, like DummyStruct
|
|
|
|
struct DummyStruct(i32);
|
|
|
|
|
|
|
|
impl ::std::ops::Index<usize> for DummyStruct {
|
|
|
|
type Output = i32;
|
|
|
|
|
|
|
|
fn index(&self, _: usize) -> &i32 {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let src = DummyStruct(5);
|
|
|
|
let mut dst_vec = vec![0; 10];
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
dst_vec[i] = src[i];
|
|
|
|
}
|
2018-10-16 06:12:59 +02:00
|
|
|
|
|
|
|
// Simplify suggestion (issue #3004)
|
|
|
|
let src = [0, 1, 2, 3, 4];
|
|
|
|
let mut dst = [0, 0, 0, 0, 0, 0];
|
|
|
|
let from = 1;
|
|
|
|
|
|
|
|
for i in from..from + src.len() {
|
|
|
|
dst[i] = src[i - from];
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in from..from + 3 {
|
|
|
|
dst[i] = src[i - from];
|
|
|
|
}
|
2017-09-05 12:53:52 +02:00
|
|
|
}
|
2017-09-17 00:53:55 +02:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[warn(clippy::needless_range_loop)]
|
2017-09-17 00:53:55 +02:00
|
|
|
pub fn manual_clone(src: &[String], dst: &mut [String]) {
|
|
|
|
for i in 0..src.len() {
|
|
|
|
dst[i] = src[i].clone();
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 22:04:35 +02:00
|
|
|
|
2018-07-28 17:34:52 +02:00
|
|
|
#[warn(clippy::needless_range_loop)]
|
2017-10-17 22:04:35 +02:00
|
|
|
pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
|
|
|
|
// Same source and destination - don't trigger lint
|
|
|
|
for i in 0..dst.len() {
|
|
|
|
dst[d + i] = dst[s + i];
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 04:56:03 +01:00
|
|
|
|
|
|
|
mod issue_2496 {
|
|
|
|
pub trait Handle {
|
|
|
|
fn new_for_index(index: usize) -> Self;
|
|
|
|
fn index(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test<H: Handle>() -> H {
|
|
|
|
for x in 0..5 {
|
|
|
|
let next_handle = H::new_for_index(x);
|
|
|
|
println!("{}", next_handle.index());
|
|
|
|
}
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 04:14:01 +02:00
|
|
|
|
|
|
|
mod issue_1219 {
|
2018-09-06 15:20:25 +02:00
|
|
|
#[warn(clippy::explicit_counter_loop)]
|
2018-09-06 04:14:01 +02:00
|
|
|
pub fn test() {
|
2018-09-07 14:32:56 +02:00
|
|
|
// should not trigger the lint because variable is used after the loop #473
|
|
|
|
let vec = vec![1,2,3];
|
|
|
|
let mut index = 0;
|
|
|
|
for _v in &vec { index += 1 }
|
|
|
|
println!("index: {}", index);
|
2018-09-06 15:20:25 +02:00
|
|
|
|
2018-09-07 14:32:56 +02:00
|
|
|
// should not trigger the lint because the count is conditional #1219
|
2018-09-06 15:20:25 +02:00
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
|
|
|
if ch == 'a' {
|
2018-09-07 14:32:56 +02:00
|
|
|
continue;
|
2018-09-06 15:20:25 +02:00
|
|
|
}
|
|
|
|
count += 1;
|
2018-09-07 14:32:56 +02:00
|
|
|
println!("{}", count);
|
2018-09-06 15:20:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:32:56 +02:00
|
|
|
// should not trigger the lint because the count is conditional
|
2018-09-06 15:20:25 +02:00
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
|
|
|
if ch == 'a' {
|
|
|
|
count += 1;
|
|
|
|
}
|
2018-09-07 14:32:56 +02:00
|
|
|
println!("{}", count);
|
2018-09-06 15:20:25 +02:00
|
|
|
}
|
2018-09-08 04:58:19 +02:00
|
|
|
|
|
|
|
// should trigger the lint because the count is not conditional
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
|
|
|
count += 1;
|
|
|
|
if ch == 'a' {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
2018-09-08 05:46:36 +02:00
|
|
|
|
|
|
|
// should trigger the lint because the count is not conditional
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
|
|
|
count += 1;
|
|
|
|
for i in 0..2 {
|
|
|
|
let _ = 123;
|
|
|
|
}
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let text = "banana";
|
|
|
|
let mut count = 0;
|
|
|
|
for ch in text.chars() {
|
|
|
|
count += 1;
|
|
|
|
for i in 0..2 {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
println!("{}", count);
|
|
|
|
}
|
2018-09-06 04:14:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 15:57:52 +02:00
|
|
|
|
|
|
|
mod issue_3308 {
|
|
|
|
#[warn(clippy::explicit_counter_loop)]
|
|
|
|
pub fn test() {
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
let erasures = vec![];
|
|
|
|
for i in 0..10 {
|
|
|
|
while erasures.contains(&(i + skips)) {
|
|
|
|
skips += 1;
|
|
|
|
}
|
|
|
|
println!("{}", skips);
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
for i in 0..10 {
|
|
|
|
let mut j = 0;
|
|
|
|
while j < 5 {
|
|
|
|
skips += 1;
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
println!("{}", skips);
|
|
|
|
}
|
|
|
|
|
|
|
|
// should not trigger the lint because the count is incremented multiple times
|
|
|
|
let mut skips = 0;
|
|
|
|
for i in 0..10 {
|
|
|
|
for j in 0..5 {
|
|
|
|
skips += 1;
|
|
|
|
}
|
|
|
|
println!("{}", skips);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|