]> git.lizzy.rs Git - rust.git/blob - tests/ui/get_unwrap.fixed
Auto merge of #3638 - mikerite:fix-3625, r=oli-obk
[rust.git] / tests / ui / get_unwrap.fixed
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 // run-rustfix
11 #![allow(unused_mut)]
12
13 use std::collections::BTreeMap;
14 use std::collections::HashMap;
15 use std::collections::VecDeque;
16 use std::iter::FromIterator;
17
18 struct GetFalsePositive {
19     arr: [u32; 3],
20 }
21
22 impl GetFalsePositive {
23     fn get(&self, pos: usize) -> Option<&u32> {
24         self.arr.get(pos)
25     }
26     fn get_mut(&mut self, pos: usize) -> Option<&mut u32> {
27         self.arr.get_mut(pos)
28     }
29 }
30
31 fn main() {
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] };
39
40     {
41         // Test `get().unwrap()`
42         let _ = &boxed_slice[1];
43         let _ = &some_slice[0];
44         let _ = &some_vec[0];
45         let _ = &some_vecdeque[0];
46         let _ = &some_hashmap[&1];
47         let _ = &some_btreemap[&1];
48         let _ = false_positive.get(0).unwrap();
49         // Test with deref
50         let _: u8 = boxed_slice[1];
51     }
52
53     {
54         // Test `get_mut().unwrap()`
55         boxed_slice[0] = 1;
56         some_slice[0] = 1;
57         some_vec[0] = 1;
58         some_vecdeque[0] = 1;
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;
63     }
64
65     {
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();
69     }
70 }