2022-05-24 04:54:49 -05:00
|
|
|
#![warn(clippy::get_first)]
|
2023-06-06 15:56:57 -05:00
|
|
|
#![allow(clippy::useless_vec)]
|
2023-07-03 12:42:48 -05:00
|
|
|
use std::collections::{BTreeMap, HashMap, VecDeque};
|
2022-05-24 04:54:49 -05:00
|
|
|
|
|
|
|
struct Bar {
|
|
|
|
arr: [u32; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn get(&self, pos: usize) -> Option<&u32> {
|
|
|
|
self.arr.get(pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = vec![2, 3, 5];
|
2023-10-04 11:07:54 -05:00
|
|
|
let _ = x.first();
|
|
|
|
//~^ ERROR: accessing first element with `x.get(0)`
|
2022-05-24 04:54:49 -05:00
|
|
|
let _ = x.get(1);
|
|
|
|
let _ = x[0];
|
|
|
|
|
|
|
|
let y = [2, 3, 5];
|
2023-10-04 11:07:54 -05:00
|
|
|
let _ = y.first();
|
|
|
|
//~^ ERROR: accessing first element with `y.get(0)`
|
2022-05-24 04:54:49 -05:00
|
|
|
let _ = y.get(1);
|
|
|
|
let _ = y[0];
|
|
|
|
|
|
|
|
let z = &[2, 3, 5];
|
2023-10-04 11:07:54 -05:00
|
|
|
let _ = z.first();
|
|
|
|
//~^ ERROR: accessing first element with `z.get(0)`
|
2022-05-24 04:54:49 -05:00
|
|
|
let _ = z.get(1);
|
|
|
|
let _ = z[0];
|
|
|
|
|
|
|
|
let vecdeque: VecDeque<_> = x.iter().cloned().collect();
|
2023-11-01 17:26:43 -05:00
|
|
|
let _ = vecdeque.front();
|
|
|
|
//~^ ERROR: accessing first element with `vecdeque.get(0)`
|
|
|
|
let _ = vecdeque.get(1);
|
|
|
|
|
2022-05-24 04:54:49 -05:00
|
|
|
let hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(0, 'a'), (1, 'b')]);
|
|
|
|
let btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(0, 'a'), (1, 'b')]);
|
|
|
|
let _ = hashmap.get(&0); // Do not lint, because HashMap is not slice.
|
|
|
|
let _ = btreemap.get(&0); // Do not lint, because BTreeMap is not slice.
|
|
|
|
|
|
|
|
let bar = Bar { arr: [0, 1, 2] };
|
|
|
|
let _ = bar.get(0); // Do not lint, because Bar is struct.
|
2023-10-04 11:07:54 -05:00
|
|
|
|
|
|
|
let non_primitives = [vec![1, 2], vec![3, 4]];
|
|
|
|
let _ = non_primitives.first();
|
|
|
|
//~^ ERROR: accessing first element with `non_primitives.get(0)`
|
2022-05-24 04:54:49 -05:00
|
|
|
}
|