Add tests for get_unwrap lint

This commit is contained in:
Devon Hollowood 2016-11-01 01:07:51 -07:00
parent c5e270a1aa
commit 1187d333ec

View File

@ -10,6 +10,7 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::ops::Mul;
use std::iter::FromIterator;
struct T;
@ -388,6 +389,75 @@ fn iter_skip_next() {
let _ = foo.filter().skip(42).next();
}
struct GetFalsePositive {
arr: [u32; 3],
}
impl GetFalsePositive {
fn get(&self, pos: usize) -> Option<&u32> { self.arr.get(pos) }
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { self.arr.get_mut(pos) }
}
/// Checks implementation of `GET_UNWRAP` lint
fn get_unwrap() {
let mut some_slice = &mut [0, 1, 2, 3];
let mut some_vec = vec![0, 1, 2, 3];
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
{ // Test `get().unwrap()`
let _ = some_slice.get(0).unwrap();
//~^ERROR called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_slice[0]
let _ = some_vec.get(0).unwrap();
//~^ERROR called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_vec[0]
let _ = some_vecdeque.get(0).unwrap();
//~^ERROR called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_vecdeque[0]
let _ = some_hashmap.get(&1).unwrap();
//~^ERROR called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_hashmap[&1]
let _ = some_btreemap.get(&1).unwrap();
//~^ERROR called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_btreemap[&1]
let _ = false_positive.get(0).unwrap();
}
{ // Test `get_mut().unwrap()`
*some_slice.get_mut(0).unwrap() = 1;
//~^ERROR called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_slice[0]
*some_vec.get_mut(0).unwrap() = 1;
//~^ERROR called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_vec[0]
*some_vecdeque.get_mut(0).unwrap() = 1;
//~^ERROR called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_vecdeque[0]
*some_hashmap.get_mut(&1).unwrap() = 'b';
//~^ERROR called `.get_mut().unwrap()` on a HashMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_hashmap[&1]
*some_btreemap.get_mut(&1).unwrap() = 'b';
//~^ERROR called `.get_mut().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
//~|HELP try this
//~|SUGGESTION some_btreemap[&1]
*false_positive.get_mut(0).unwrap() = 1;
}
}
#[allow(similar_names)]
fn main() {