]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/entry.fixed
Auto merge of #102536 - scottmcm:lookup_line-tweak, r=jackh726
[rust.git] / src / tools / clippy / tests / ui / entry.fixed
1 // needs-asm-support
2 // run-rustfix
3
4 #![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
5 #![warn(clippy::map_entry)]
6
7 use std::arch::asm;
8 use std::collections::HashMap;
9 use std::hash::Hash;
10
11 macro_rules! m {
12     ($e:expr) => {{ $e }};
13 }
14
15 macro_rules! insert {
16     ($map:expr, $key:expr, $val:expr) => {
17         $map.insert($key, $val)
18     };
19 }
20
21 fn foo() {}
22
23 fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, m2: &mut HashMap<K, V>, k: K, k2: K, v: V, v2: V) {
24     // or_insert(v)
25     m.entry(k).or_insert(v);
26
27     // semicolon on insert, use or_insert_with(..)
28     m.entry(k).or_insert_with(|| {
29         if true {
30             v
31         } else {
32             v2
33         }
34     });
35
36     // semicolon on if, use or_insert_with(..)
37     m.entry(k).or_insert_with(|| {
38         if true {
39             v
40         } else {
41             v2
42         }
43     });
44
45     // early return, use if let
46     if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
47         if true {
48             e.insert(v);
49         } else {
50             e.insert(v2);
51             return;
52         }
53     }
54
55     // use or_insert_with(..)
56     m.entry(k).or_insert_with(|| {
57         foo();
58         v
59     });
60
61     // semicolon on insert and match, use or_insert_with(..)
62     m.entry(k).or_insert_with(|| {
63         match 0 {
64             1 if true => {
65                 v
66             },
67             _ => {
68                 v2
69             },
70         }
71     });
72
73     // one branch doesn't insert, use if let
74     if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
75         match 0 {
76             0 => foo(),
77             _ => {
78                 e.insert(v2);
79             },
80         };
81     }
82
83     // use or_insert_with
84     m.entry(k).or_insert_with(|| {
85         foo();
86         match 0 {
87             0 if false => {
88                 v
89             },
90             1 => {
91                 foo();
92                 v
93             },
94             2 | 3 => {
95                 for _ in 0..2 {
96                     foo();
97                 }
98                 if true {
99                     v
100                 } else {
101                     v2
102                 }
103             },
104             _ => {
105                 v2
106             },
107         }
108     });
109
110     // ok, insert in loop
111     if !m.contains_key(&k) {
112         for _ in 0..2 {
113             m.insert(k, v);
114         }
115     }
116
117     // macro_expansion test, use or_insert(..)
118     m.entry(m!(k)).or_insert_with(|| m!(v));
119
120     // ok, map used before insertion
121     if !m.contains_key(&k) {
122         let _ = m.len();
123         m.insert(k, v);
124     }
125
126     // ok, inline asm
127     if !m.contains_key(&k) {
128         unsafe { asm!("nop") }
129         m.insert(k, v);
130     }
131
132     // ok, different keys.
133     if !m.contains_key(&k) {
134         m.insert(k2, v);
135     }
136
137     // ok, different maps
138     if !m.contains_key(&k) {
139         m2.insert(k, v);
140     }
141
142     // ok, insert in macro
143     if !m.contains_key(&k) {
144         insert!(m, k, v);
145     }
146
147     // or_insert_with. Partial move of a local declared in the closure is ok.
148     m.entry(k).or_insert_with(|| {
149         let x = (String::new(), String::new());
150         let _ = x.0;
151         v
152     });
153 }
154
155 fn main() {}