move get_unwrap tests
This commit is contained in:
parent
90b428e88d
commit
35882b09da
46
tests/ui/get_unwrap.rs
Normal file
46
tests/ui/get_unwrap.rs
Normal file
@ -0,0 +1,46 @@
|
||||
#![allow(unused_mut)]
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
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) }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
|
||||
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 _ = boxed_slice.get(1).unwrap();
|
||||
let _ = some_slice.get(0).unwrap();
|
||||
let _ = some_vec.get(0).unwrap();
|
||||
let _ = some_vecdeque.get(0).unwrap();
|
||||
let _ = some_hashmap.get(&1).unwrap();
|
||||
let _ = some_btreemap.get(&1).unwrap();
|
||||
let _ = false_positive.get(0).unwrap();
|
||||
}
|
||||
|
||||
{ // Test `get_mut().unwrap()`
|
||||
*boxed_slice.get_mut(0).unwrap() = 1;
|
||||
*some_slice.get_mut(0).unwrap() = 1;
|
||||
*some_vec.get_mut(0).unwrap() = 1;
|
||||
*some_vecdeque.get_mut(0).unwrap() = 1;
|
||||
// Check false positives
|
||||
*some_hashmap.get_mut(&1).unwrap() = 'b';
|
||||
*some_btreemap.get_mut(&1).unwrap() = 'b';
|
||||
*false_positive.get_mut(0).unwrap() = 1;
|
||||
}
|
||||
}
|
62
tests/ui/get_unwrap.stderr
Normal file
62
tests/ui/get_unwrap.stderr
Normal file
@ -0,0 +1,62 @@
|
||||
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:27:17
|
||||
|
|
||||
27 | let _ = boxed_slice.get(1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
|
||||
|
|
||||
= note: `-D get-unwrap` implied by `-D warnings`
|
||||
|
||||
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:28:17
|
||||
|
|
||||
28 | let _ = some_slice.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:29:17
|
||||
|
|
||||
29 | let _ = some_vec.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:30:17
|
||||
|
|
||||
30 | let _ = some_vecdeque.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:31:17
|
||||
|
|
||||
31 | let _ = some_hashmap.get(&1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]`
|
||||
|
||||
error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:32:17
|
||||
|
|
||||
32 | let _ = some_btreemap.get(&1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:37:10
|
||||
|
|
||||
37 | *boxed_slice.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut boxed_slice[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:38:10
|
||||
|
|
||||
38 | *some_slice.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_slice[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:39:10
|
||||
|
|
||||
39 | *some_vec.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vec[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
|
||||
--> $DIR/get_unwrap.rs:40:10
|
||||
|
|
||||
40 | *some_vecdeque.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vecdeque[0]`
|
||||
|
@ -349,48 +349,6 @@ 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 boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
|
||||
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 _ = boxed_slice.get(1).unwrap();
|
||||
let _ = some_slice.get(0).unwrap();
|
||||
let _ = some_vec.get(0).unwrap();
|
||||
let _ = some_vecdeque.get(0).unwrap();
|
||||
let _ = some_hashmap.get(&1).unwrap();
|
||||
let _ = some_btreemap.get(&1).unwrap();
|
||||
let _ = false_positive.get(0).unwrap();
|
||||
}
|
||||
|
||||
{ // Test `get_mut().unwrap()`
|
||||
*boxed_slice.get_mut(0).unwrap() = 1;
|
||||
*some_slice.get_mut(0).unwrap() = 1;
|
||||
*some_vec.get_mut(0).unwrap() = 1;
|
||||
*some_vecdeque.get_mut(0).unwrap() = 1;
|
||||
// Check false positives
|
||||
*some_hashmap.get_mut(&1).unwrap() = 'b';
|
||||
*some_btreemap.get_mut(&1).unwrap() = 'b';
|
||||
*false_positive.get_mut(0).unwrap() = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[allow(similar_names)]
|
||||
fn main() {
|
||||
let opt = Some(0);
|
||||
|
@ -437,72 +437,10 @@ error: called `skip(x).next()` on an iterator. This is more succinctly expressed
|
||||
346 | let _ = &some_vec[..].iter().skip(3).next();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:372:17
|
||||
|
|
||||
372 | let _ = boxed_slice.get(1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
|
||||
|
|
||||
= note: `-D get-unwrap` implied by `-D warnings`
|
||||
|
||||
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:373:17
|
||||
|
|
||||
373 | let _ = some_slice.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:374:17
|
||||
|
|
||||
374 | let _ = some_vec.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:375:17
|
||||
|
|
||||
375 | let _ = some_vecdeque.get(0).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]`
|
||||
|
||||
error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:376:17
|
||||
|
|
||||
376 | let _ = some_hashmap.get(&1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]`
|
||||
|
||||
error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:377:17
|
||||
|
|
||||
377 | let _ = some_btreemap.get(&1).unwrap();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:382:10
|
||||
|
|
||||
382 | *boxed_slice.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut boxed_slice[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:383:10
|
||||
|
|
||||
383 | *some_slice.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_slice[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:384:10
|
||||
|
|
||||
384 | *some_vec.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vec[0]`
|
||||
|
||||
error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
|
||||
--> $DIR/methods.rs:385:10
|
||||
|
|
||||
385 | *some_vecdeque.get_mut(0).unwrap() = 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vecdeque[0]`
|
||||
|
||||
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
|
||||
--> $DIR/methods.rs:397:13
|
||||
--> $DIR/methods.rs:355:13
|
||||
|
|
||||
397 | let _ = opt.unwrap();
|
||||
355 | let _ = opt.unwrap();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: `-D option-unwrap-used` implied by `-D warnings`
|
||||
|
Loading…
x
Reference in New Issue
Block a user