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