2019-09-20 06:25:05 +00:00
|
|
|
// run-rustfix
|
|
|
|
|
|
|
|
#![allow(dead_code, unused)]
|
2015-08-25 16:26:20 +00:00
|
|
|
|
2019-09-20 06:25:05 +00:00
|
|
|
use std::collections::*;
|
2016-03-07 22:24:11 +00:00
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#[warn(clippy::all)]
|
2015-08-17 05:23:57 +00:00
|
|
|
struct Unrelated(Vec<u8>);
|
|
|
|
impl Unrelated {
|
|
|
|
fn next(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-25 16:26:20 +00:00
|
|
|
|
|
|
|
fn iter(&self) -> std::slice::Iter<u8> {
|
|
|
|
self.0.iter()
|
|
|
|
}
|
2015-08-17 05:23:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
#[warn(
|
|
|
|
clippy::needless_range_loop,
|
|
|
|
clippy::explicit_iter_loop,
|
|
|
|
clippy::explicit_into_iter_loop,
|
|
|
|
clippy::iter_next_loop,
|
|
|
|
clippy::for_kv_map
|
|
|
|
)]
|
2022-01-27 14:12:45 +00:00
|
|
|
#[allow(
|
|
|
|
clippy::linkedlist,
|
|
|
|
clippy::unnecessary_mut_passed,
|
|
|
|
clippy::similar_names,
|
|
|
|
clippy::needless_borrow
|
|
|
|
)]
|
2021-09-28 17:03:12 +00:00
|
|
|
#[allow(unused_variables)]
|
2015-08-12 19:56:27 +00:00
|
|
|
fn main() {
|
2015-08-13 13:36:31 +00:00
|
|
|
let mut vec = vec![1, 2, 3, 4];
|
2016-01-14 19:58:32 +00:00
|
|
|
|
2016-02-13 21:09:17 +00:00
|
|
|
// See #601
|
2017-09-05 10:53:52 +00:00
|
|
|
for i in 0..10 {
|
|
|
|
// no error, id_col does not exist outside the loop
|
2016-02-13 21:09:17 +00:00
|
|
|
let mut id_col = vec![0f64; 10];
|
|
|
|
id_col[i] = 1f64;
|
|
|
|
}
|
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in vec.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in vec.iter_mut() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
let out_vec = vec![1, 2, 3];
|
|
|
|
for _v in out_vec.into_iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {} // these are fine
|
|
|
|
for _v in &mut vec {} // these are fine
|
2015-08-17 05:23:57 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in [1, 2, 3].iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in (&mut [1, 2, 3]).iter() {} // no error
|
2017-01-28 13:02:49 +00:00
|
|
|
|
|
|
|
for _v in [0; 32].iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-09-06 11:36:21 +00:00
|
|
|
for _v in [0; 33].iter() {} // no error
|
2017-01-28 13:02:49 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let ll: LinkedList<()> = LinkedList::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in ll.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let vd: VecDeque<()> = VecDeque::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in vd.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let bh: BinaryHeap<()> = BinaryHeap::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in bh.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let hm: HashMap<(), ()> = HashMap::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in hm.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let bt: BTreeMap<(), ()> = BTreeMap::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in bt.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let hs: HashSet<()> = HashSet::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in hs.iter() {}
|
2017-02-08 13:58:07 +00:00
|
|
|
|
2015-08-25 16:26:20 +00:00
|
|
|
let bs: BTreeSet<()> = BTreeSet::new();
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in bs.iter() {}
|
2017-01-28 13:02:49 +00:00
|
|
|
|
2015-08-17 05:23:57 +00:00
|
|
|
let u = Unrelated(vec![]);
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in u.next() {} // no error
|
|
|
|
for _v in u.iter() {} // no error
|
2015-08-30 11:10:59 +00:00
|
|
|
|
|
|
|
let mut out = vec![];
|
2017-07-10 13:30:28 +00: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 17:25:45 +00:00
|
|
|
|
|
|
|
// Loop with explicit counter variable
|
|
|
|
|
|
|
|
// Potential false positives
|
|
|
|
let mut _index = 0;
|
|
|
|
_index = 1;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
let mut _index = 1;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index *= 2;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index = 1;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
let mut _index = 0;
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
_index += 1;
|
|
|
|
_index = 0;
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
for _x in 0..1 {
|
|
|
|
_index += 1;
|
|
|
|
}
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for x in &vec {
|
|
|
|
if *x == 1 {
|
|
|
|
_index += 1
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
if true {
|
|
|
|
_index = 1
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-08-23 17:25:45 +00:00
|
|
|
|
|
|
|
let mut _index = 1;
|
2017-09-05 10:53:52 +00:00
|
|
|
if false {
|
|
|
|
_index = 0
|
|
|
|
};
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-09-10 12:33:29 +00:00
|
|
|
|
2015-12-19 00:04:33 +00:00
|
|
|
let mut index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
{
|
|
|
|
let mut _x = &mut index;
|
|
|
|
}
|
|
|
|
for _v in &vec {
|
|
|
|
_index += 1
|
|
|
|
}
|
2015-11-25 23:09:01 +00:00
|
|
|
|
|
|
|
let mut index = 0;
|
2017-09-05 10:53:52 +00:00
|
|
|
for _v in &vec {
|
|
|
|
index += 1
|
|
|
|
}
|
2015-11-25 23:09:01 +00:00
|
|
|
println!("index: {}", index);
|
2016-01-29 07:34:09 +00:00
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
fn f<T>(_: &T, _: &T) -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn g<T>(_: &mut [T], _: usize, _: usize) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2017-07-10 13:30:28 +00: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 18:14:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 10:53:52 +00:00
|
|
|
fn partition<T: PartialOrd + Send>(v: &mut [T]) -> usize {
|
2017-07-10 13:30:28 +00: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 10:53:52 +00:00
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#[warn(clippy::needless_range_loop)]
|
2017-10-17 20:04:35 +00: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 03:56:03 +00: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!()
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 05:09:09 +00:00
|
|
|
|
|
|
|
// explicit_into_iter_loop bad suggestions
|
|
|
|
#[warn(clippy::explicit_into_iter_loop, clippy::explicit_iter_loop)]
|
|
|
|
mod issue_4958 {
|
|
|
|
fn takes_iterator<T>(iterator: &T)
|
|
|
|
where
|
|
|
|
for<'a> &'a T: IntoIterator<Item = &'a String>,
|
|
|
|
{
|
|
|
|
for i in iterator.into_iter() {
|
|
|
|
println!("{}", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct T;
|
|
|
|
impl IntoIterator for &T {
|
|
|
|
type Item = ();
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
vec![].into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn more_tests() {
|
|
|
|
let t = T;
|
|
|
|
let r = &t;
|
|
|
|
let rr = &&t;
|
|
|
|
|
|
|
|
// This case is handled by `explicit_iter_loop`. No idea why.
|
|
|
|
for _ in t.into_iter() {}
|
|
|
|
|
|
|
|
for _ in r.into_iter() {}
|
|
|
|
|
|
|
|
// No suggestion for this.
|
|
|
|
// We'd have to suggest `for _ in *rr {}` which is less clear.
|
|
|
|
for _ in rr.into_iter() {}
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
// explicit_into_iter_loop
|
|
|
|
#[warn(clippy::explicit_into_iter_loop)]
|
|
|
|
mod issue_6900 {
|
|
|
|
struct S;
|
|
|
|
impl S {
|
|
|
|
#[allow(clippy::should_implement_trait)]
|
|
|
|
pub fn into_iter<T>(self) -> I<T> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct I<T>(T);
|
|
|
|
impl<T> Iterator for I<T> {
|
|
|
|
type Item = T;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
for _ in S.into_iter::<u32>() {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|