1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
13 use std::collections::BTreeMap;
14 use std::collections::HashMap;
15 use std::collections::VecDeque;
16 use std::iter::FromIterator;
18 struct GetFalsePositive {
22 impl GetFalsePositive {
23 fn get(&self, pos: usize) -> Option<&u32> {
26 fn get_mut(&mut self, pos: usize) -> Option<&mut u32> {
32 let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
33 let mut some_slice = &mut [0, 1, 2, 3];
34 let mut some_vec = vec![0, 1, 2, 3];
35 let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
36 let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
37 let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
38 let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
41 // Test `get().unwrap()`
42 let _ = &boxed_slice[1];
43 let _ = &some_slice[0];
45 let _ = &some_vecdeque[0];
46 let _ = &some_hashmap[&1];
47 let _ = &some_btreemap[&1];
48 let _ = false_positive.get(0).unwrap();
50 let _: u8 = boxed_slice[1];
54 // Test `get_mut().unwrap()`
59 // Check false positives
60 *some_hashmap.get_mut(&1).unwrap() = 'b';
61 *some_btreemap.get_mut(&1).unwrap() = 'b';
62 *false_positive.get_mut(0).unwrap() = 1;
66 // Test `get().unwrap().foo()` and `get_mut().unwrap().bar()`
67 let _ = some_vec[0..1].to_vec();
68 let _ = some_vec[0..1].to_vec();