]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/tests/ui/entry.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / entry.rs
index ccbc7038f13600aa88b9b6ccd719cab2081c6c1c..d972a201ad76460e401c6a0ff613eab5e4f53af9 100644 (file)
+// run-rustfix
 
+#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
+#![warn(clippy::map_entry)]
+#![feature(asm)]
 
-#![allow(unused, needless_pass_by_value)]
+use std::collections::HashMap;
+use std::hash::Hash;
 
-#![warn(map_entry)]
+macro_rules! m {
+    ($e:expr) => {{ $e }};
+}
 
-use std::collections::{BTreeMap, HashMap};
-use std::hash::Hash;
+macro_rules! insert {
+    ($map:expr, $key:expr, $val:expr) => {
+        $map.insert($key, $val)
+    };
+}
 
 fn foo() {}
 
-fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if !m.contains_key(&k) { m.insert(k, v); }
-}
+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) {
+    // or_insert(v)
+    if !m.contains_key(&k) {
+        m.insert(k, v);
+    }
 
-fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if !m.contains_key(&k) { foo(); m.insert(k, v); }
-}
+    // semicolon on insert, use or_insert_with(..)
+    if !m.contains_key(&k) {
+        if true {
+            m.insert(k, v);
+        } else {
+            m.insert(k, v2);
+        }
+    }
 
-fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if !m.contains_key(&k) { m.insert(k, v) } else { None };
-}
+    // semicolon on if, use or_insert_with(..)
+    if !m.contains_key(&k) {
+        if true {
+            m.insert(k, v)
+        } else {
+            m.insert(k, v2)
+        };
+    }
 
-fn insert_if_present2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if m.contains_key(&k) { None } else { m.insert(k, v) };
-}
+    // early return, use if let
+    if !m.contains_key(&k) {
+        if true {
+            m.insert(k, v);
+        } else {
+            m.insert(k, v2);
+            return;
+        }
+    }
 
-fn insert_if_absent3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None };
-}
+    // use or_insert_with(..)
+    if !m.contains_key(&k) {
+        foo();
+        m.insert(k, v);
+    }
 
-fn insert_if_present3<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
-    if m.contains_key(&k) { None } else { foo(); m.insert(k, v) };
-}
+    // semicolon on insert and match, use or_insert_with(..)
+    if !m.contains_key(&k) {
+        match 0 {
+            1 if true => {
+                m.insert(k, v);
+            },
+            _ => {
+                m.insert(k, v2);
+            },
+        };
+    }
 
-fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
-    if !m.contains_key(&k) { foo(); m.insert(k, v) } else { None };
-}
+    // one branch doesn't insert, use if let
+    if !m.contains_key(&k) {
+        match 0 {
+            0 => foo(),
+            _ => {
+                m.insert(k, v2);
+            },
+        };
+    }
 
-fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
-    if !m.contains_key(&k) { m.insert(o, v); }
-}
+    // use or_insert_with
+    if !m.contains_key(&k) {
+        foo();
+        match 0 {
+            0 if false => {
+                m.insert(k, v);
+            },
+            1 => {
+                foo();
+                m.insert(k, v);
+            },
+            2 | 3 => {
+                for _ in 0..2 {
+                    foo();
+                }
+                if true {
+                    m.insert(k, v);
+                } else {
+                    m.insert(k, v2);
+                };
+            },
+            _ => {
+                m.insert(k, v2);
+            },
+        }
+    }
+
+    // ok, insert in loop
+    if !m.contains_key(&k) {
+        for _ in 0..2 {
+            m.insert(k, v);
+        }
+    }
+
+    // macro_expansion test, use or_insert(..)
+    if !m.contains_key(&m!(k)) {
+        m.insert(m!(k), m!(v));
+    }
+
+    // ok, map used before insertion
+    if !m.contains_key(&k) {
+        let _ = m.len();
+        m.insert(k, v);
+    }
 
-fn main() {
+    // ok, inline asm
+    if !m.contains_key(&k) {
+        unsafe { asm!("nop") }
+        m.insert(k, v);
+    }
+
+    // ok, different keys.
+    if !m.contains_key(&k) {
+        m.insert(k2, v);
+    }
+
+    // ok, different maps
+    if !m.contains_key(&k) {
+        m2.insert(k, v);
+    }
+
+    // ok, insert in macro
+    if !m.contains_key(&k) {
+        insert!(m, k, v);
+    }
+
+    // or_insert_with. Partial move of a local declared in the closure is ok.
+    if !m.contains_key(&k) {
+        let x = (String::new(), String::new());
+        let _ = x.0;
+        m.insert(k, v);
+    }
 }
+
+fn main() {}