fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.1 KiB107 linesraw
1#![allow(
2    unused_mut,
3    clippy::from_iter_instead_of_collect,
4    clippy::get_first,
5    clippy::useless_vec
6)]
7#![warn(clippy::unwrap_used)]
8#![deny(clippy::get_unwrap)]
9
10use std::collections::{BTreeMap, HashMap, VecDeque};
11
12struct GetFalsePositive {
13    arr: [u32; 3],
14}
15
16impl GetFalsePositive {
17    fn get(&self, pos: usize) -> Option<&u32> {
18        self.arr.get(pos)
19    }
20    fn get_mut(&mut self, pos: usize) -> Option<&mut u32> {
21        self.arr.get_mut(pos)
22    }
23}
24
25fn main() {
26    let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
27    let mut some_slice = &mut [0, 1, 2, 3];
28    let mut some_vec = vec![0, 1, 2, 3];
29    let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
30    let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
31    let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
32    let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
33
34    {
35        // Test `get().unwrap()`
36        let _ = &boxed_slice[1];
37        let _ = &some_slice[0];
38        let _ = &some_vec[0];
39        let _ = &some_vecdeque[0];
40        let _ = &some_hashmap[&1];
41        let _ = &some_btreemap[&1];
42        #[allow(clippy::unwrap_used)]
43        let _ = false_positive.get(0).unwrap();
44        // Test with deref
45        let _: u8 = boxed_slice[1];
46    }
47
48    {
49        // Test `get_mut().unwrap()`
50        boxed_slice[0] = 1;
51        some_slice[0] = 1;
52        some_vec[0] = 1;
53        some_vecdeque[0] = 1;
54        // Check false positives
55        #[allow(clippy::unwrap_used)]
56        {
57            *some_hashmap.get_mut(&1).unwrap() = 'b';
58            *some_btreemap.get_mut(&1).unwrap() = 'b';
59            *false_positive.get_mut(0).unwrap() = 1;
60        }
61    }
62
63    {
64        // Test `get().unwrap().foo()` and `get_mut().unwrap().bar()`
65        let _ = some_vec[0..1].to_vec();
66        let _ = some_vec[0..1].to_vec();
67    }
68}
69mod issue9909 {
70    #![allow(clippy::identity_op, clippy::unwrap_used, dead_code)]
71
72    fn reduced() {
73        let f = &[1, 2, 3];
74
75        // include a borrow in the suggestion, even if the argument is not just a numeric literal
76        let _x: &i32 = &f[1 + 2];
77
78        // don't include a borrow here
79        let _x = f[1 + 2].to_string();
80
81        // don't include a borrow here
82        let _x = f[1 + 2].abs();
83    }
84
85    // original code:
86    fn linidx(row: usize, col: usize) -> usize {
87        row * 1 + col * 3
88    }
89
90    fn main_() {
91        let mut mat = [1.0f32, 5.0, 9.0, 2.0, 6.0, 10.0, 3.0, 7.0, 11.0, 4.0, 8.0, 12.0];
92
93        for i in 0..2 {
94            for j in i + 1..3 {
95                if mat[linidx(j, 3)] > mat[linidx(i, 3)] {
96                    for k in 0..4 {
97                        let (x, rest) = mat.split_at_mut(linidx(i, k) + 1);
98                        let a = x.last_mut().unwrap();
99                        let b = &mut rest[linidx(j, k) - linidx(i, k) - 1];
100                        ::std::mem::swap(a, b);
101                    }
102                }
103            }
104        }
105        assert_eq!([9.0, 5.0, 1.0, 10.0, 6.0, 2.0, 11.0, 7.0, 3.0, 12.0, 8.0, 4.0], mat);
106    }
107}