2015-08-25 11:26:20 -05:00
|
|
|
use std::collections::*;
|
2016-07-01 13:55:45 -05:00
|
|
|
use std::rc::Rc;
|
2015-08-25 11:26:20 -05:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
static STATIC: [usize; 4] = [0, 1, 8, 16];
|
|
|
|
const CONST: [usize; 4] = [0, 1, 8, 16];
|
2016-03-07 16:24:11 -06:00
|
|
|
|
2018-07-28 10:34:52 -05:00
|
|
|
#[warn(clippy::all)]
|
2015-08-17 00:23:57 -05:00
|
|
|
struct Unrelated(Vec<u8>);
|
|
|
|
impl Unrelated {
|
|
|
|
fn next(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-25 11:26:20 -05:00
|
|
|
|
|
|
|
fn iter(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-17 00:23:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
#[warn(
|
|
|
|
clippy::needless_range_loop,
|
|
|
|
clippy::explicit_iter_loop,
|
|
|
|
clippy::explicit_into_iter_loop,
|
|
|
|
clippy::iter_next_loop,
|
|
|
|
clippy::reverse_range_loop,
|
|
|
|
clippy::for_kv_map
|
|
|
|
)]
|
2018-07-28 10:34:52 -05:00
|
|
|
#[warn(clippy::unused_collect)]
|
2018-12-09 16:26:16 -06:00
|
|
|
#[allow(
|
|
|
|
clippy::linkedlist,
|
|
|
|
clippy::shadow_unrelated,
|
|
|
|
clippy::unnecessary_mut_passed,
|
2019-02-22 19:19:50 -06:00
|
|
|
clippy::cognitive_complexity,
|
2018-12-09 16:26:16 -06:00
|
|
|
clippy::similar_names
|
|
|
|
)]
|
2018-10-20 15:03:38 -05:00
|
|
|
#[allow(clippy::many_single_char_names, unused_variables, clippy::into_iter_on_array)]
|
2015-08-12 14:56:27 -05:00
|
|
|
fn main() {
|
2016-02-07 11:10:03 -06:00
|
|
|
const MAX_LEN: usize = 42;
|
|
|
|
|
2015-08-13 08:36:31 -05:00
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
2015-08-12 14:56:27 -05:00
|
|
|
let vec2 = vec![1, 2, 3, 4];
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 0..vec.len() {
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
2016-03-07 16:24:11 -06:00
|
|
|
|
2016-07-10 07:05:57 -05: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 05:53:52 -05:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
let _ = vec[i];
|
|
|
|
}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-03-07 16:24:11 -06:00
|
|
|
// ICE #746
|
|
|
|
for j in 0..4 {
|
|
|
|
println!("{:?}", STATIC[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for j in 0..4 {
|
|
|
|
println!("{:?}", CONST[j]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 0..vec.len() {
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
// not an error, indexing more than one variable
|
2015-08-12 14:56:27 -05:00
|
|
|
println!("{} {}", vec[i], vec2[i]);
|
|
|
|
}
|
2015-08-13 08:36:31 -05:00
|
|
|
|
2016-01-14 14:04:34 -06:00
|
|
|
for i in 0..vec.len() {
|
|
|
|
println!("{}", vec2[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..vec.len() {
|
2015-09-02 05:41:51 -05:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
for i in 0..MAX_LEN {
|
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:40:19 -05:00
|
|
|
for i in 0..=MAX_LEN {
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..10 {
|
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:40:19 -05:00
|
|
|
for i in 5..=10 {
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", vec[i]);
|
|
|
|
}
|
|
|
|
|
2016-01-14 13:58:32 -06:00
|
|
|
for i in 5..vec.len() {
|
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 5..10 {
|
|
|
|
println!("{} {}", vec[i], i);
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:10:03 -06:00
|
|
|
for i in 10..0 {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:40:19 -05:00
|
|
|
for i in 10..=0 {
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:58:07 -06:00
|
|
|
for i in MAX_LEN..0 {
|
2015-09-14 19:19:05 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:58:07 -06:00
|
|
|
for i in 5..5 {
|
2015-09-14 19:19:05 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-28 12:40:19 -05:00
|
|
|
for i in 5..=5 {
|
2017-09-05 05:53:52 -05:00
|
|
|
// not an error, this is the range with only one element “5”
|
2016-03-07 09:55:12 -06:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in 0..10 {
|
|
|
|
// not an error, the start index is less than the end index
|
2015-09-14 19:19:05 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in -10..0 {
|
|
|
|
// not an error
|
2015-10-26 01:43:38 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05: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 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing that the empty range lint folds constants
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in 10..5 + 4 {
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in (5 + 2)..(3 - 1) {
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in (5 + 2)..(8 - 1) {
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in (2 * 2)..(2 * 3) {
|
|
|
|
// no error, 4..6 is fine
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = 42;
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in x..10 {
|
|
|
|
// no error, not constant-foldable
|
2015-09-15 00:20:56 -05:00
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
|
2016-02-13 15:09:17 -06:00
|
|
|
// See #601
|
2017-09-05 05:53:52 -05:00
|
|
|
for i in 0..10 {
|
|
|
|
// no error, id_col does not exist outside the loop
|
2016-02-13 15:09:17 -06:00
|
|
|
let mut id_col = vec![0f64; 10];
|
|
|
|
id_col[i] = 1f64;
|
|
|
|
}
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in vec.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in vec.iter_mut() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
let out_vec = vec![1, 2, 3];
|
|
|
|
for _v in out_vec.into_iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2017-01-09 09:59:55 -06:00
|
|
|
let array = [1, 2, 3];
|
2017-01-30 07:25:25 -06:00
|
|
|
for _v in array.into_iter() {}
|
2017-01-09 09:59:55 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {} // these are fine
|
|
|
|
for _v in &mut vec {} // these are fine
|
2015-08-17 00:23:57 -05:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in [1, 2, 3].iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in (&mut [1, 2, 3]).iter() {} // no error
|
2017-01-28 07:02:49 -06:00
|
|
|
|
|
|
|
for _v in [0; 32].iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-09-06 06:36:21 -05:00
|
|
|
for _v in [0; 33].iter() {} // no error
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let ll: LinkedList<()> = LinkedList::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in ll.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let vd: VecDeque<()> = VecDeque::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in vd.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in bh.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let hm: HashMap<(), ()> = HashMap::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in hm.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bt: BTreeMap<(), ()> = BTreeMap::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in bt.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let hs: HashSet<()> = HashSet::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in hs.iter() {}
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2015-08-25 11:26:20 -05:00
|
|
|
let bs: BTreeSet<()> = BTreeSet::new();
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in bs.iter() {}
|
2017-01-28 07:02:49 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in vec.iter().next() {}
|
2015-08-17 00:23:57 -05:00
|
|
|
|
|
|
|
let u = Unrelated(vec![]);
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in u.next() {} // no error
|
|
|
|
for _v in u.iter() {} // no error
|
2015-08-30 06:10:59 -05:00
|
|
|
|
|
|
|
let mut out = vec![];
|
2017-07-10 08:30:28 -05: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 12:25:45 -05:00
|
|
|
|
|
|
|
// Loop with explicit counter variable
|
|
|
|
|
|
|
|
// Potential false positives
|
|
|
|
let mut _index = 0;
|
|
|
|
_index = 1;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
let mut _index = 1;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index *= 2;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index = 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index = 0;
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
for _x in 0..1 {
|
|
|
|
_index += 1;
|
|
|
|
}
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for x in &vec {
|
|
|
|
if *x == 1 {
|
|
|
|
_index += 1
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 12:25:45 -05:00
|
|
|
|
|
|
|
let mut _index = 1;
|
2017-09-05 05:53:52 -05:00
|
|
|
if false {
|
|
|
|
_index = 0
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-09-10 07:33:29 -05:00
|
|
|
|
2015-12-18 18:04:33 -06:00
|
|
|
let mut index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
{
|
|
|
|
let mut _x = &mut index;
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-11-25 17:09:01 -06:00
|
|
|
|
|
|
|
let mut index = 0;
|
2017-09-05 05:53:52 -05:00
|
|
|
for _v in &vec {
|
|
|
|
index += 1
|
|
|
|
}
|
2015-11-25 17:09:01 -06:00
|
|
|
println!("index: {}", index);
|
2016-01-29 01:34:09 -06:00
|
|
|
|
2017-09-05 05:53:52 -05:00
|
|
|
fn f<T>(_: &T, _: &T) -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn g<T>(_: &mut [T], _: usize, _: usize) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2017-07-10 08:30:28 -05: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 12:14:02 -06:00
|
|
|
}
|
|
|
|
|
2017-07-10 08:30:28 -05:00
|
|
|
#[allow(dead_code)]
|
2017-09-05 05:53:52 -05:00
|
|
|
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
|
2017-07-10 08:30:28 -05: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 05:53:52 -05:00
|
|
|
|
2018-07-28 10:34:52 -05:00
|
|
|
#[warn(clippy::needless_range_loop)]
|
2017-10-17 15:04:35 -05: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-04 21:56:03 -06: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!()
|
|
|
|
}
|
|
|
|
}
|