From 1187d333ec0bdee66583220a104c5bf4e14d16ef Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Tue, 1 Nov 2016 01:07:51 -0700 Subject: [PATCH] Add tests for `get_unwrap` lint --- tests/compile-fail/methods.rs | 70 +++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index a26a396526e9..4ace6909f8ff 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -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 = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]); + let mut some_btreemap: BTreeMap = 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() {