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