]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/entry.fixed
Minor cleanup of `map_entry` and a few additional tests.
[rust.git] / tests / ui / entry.fixed
index 524f2132797e6a76bcc858eefdd76dc70459e744..cfad3090ba38d2bc7a0a5482c0c97084757f4fee 100644 (file)
@@ -2,6 +2,7 @@
 
 #![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
 #![warn(clippy::map_entry)]
+#![feature(asm)]
 
 use std::collections::{BTreeMap, HashMap};
 use std::hash::Hash;
@@ -10,11 +11,19 @@ macro_rules! m {
     ($e:expr) => {{ $e }};
 }
 
+macro_rules! insert {
+    ($map:expr, $key:expr, $val:expr) => {
+        $map.insert($key, $val)
+    };
+}
+
 fn foo() {}
 
-fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2: 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)
     m.entry(k).or_insert(v);
 
+    // semicolon on insert, use or_insert_with(..)
     m.entry(k).or_insert_with(|| {
         if true {
             v
@@ -23,6 +32,7 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2:
         }
     });
 
+    // semicolon on if, use or_insert_with(..)
     m.entry(k).or_insert_with(|| {
         if true {
             v
@@ -31,6 +41,7 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2:
         }
     });
 
+    // early return, use if let
     if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
         if true {
             e.insert(v);
@@ -40,11 +51,13 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2:
         }
     }
 
+    // use or_insert_with(..)
     m.entry(k).or_insert_with(|| {
         foo();
         v
     });
 
+    // semicolon on insert and match, use or_insert_with(..)
     m.entry(k).or_insert_with(|| {
         match 0 {
             1 if true => {
@@ -56,18 +69,17 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2:
         }
     });
 
+    // one branch doesn't insert, use if let
     if let std::collections::hash_map::Entry::Vacant(e) = m.entry(k) {
         match 0 {
-            0 => {},
-            1 => {
-                e.insert(v);
-            },
+            0 => foo(),
             _ => {
                 e.insert(v2);
             },
         };
     }
 
+    // use or_insert_with
     m.entry(k).or_insert_with(|| {
         foo();
         match 0 {
@@ -94,10 +106,46 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2:
         }
     });
 
+    // ok, insert in loop
+    if !m.contains_key(&k) {
+        for _ in 0..2 {
+            m.insert(k, v);
+        }
+    }
+
+    // macro_expansion test, use or_insert(..)
     m.entry(m!(k)).or_insert_with(|| m!(v));
+
+    // ok, map used before insertion
+    if !m.contains_key(&k) {
+        let _ = m.len();
+        m.insert(k, v);
+    }
+
+    // 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);
+    }
 }
 
 fn btree_map<K: Eq + Ord + Copy, V: Copy>(m: &mut BTreeMap<K, V>, k: K, v: V, v2: V) {
+    // insert then do something, use if let
     if let std::collections::btree_map::Entry::Vacant(e) = m.entry(k) {
         e.insert(v);
         foo();