about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorEthiraric <ethiraric@gmail.com>2024-02-27 11:41:38 +0100
committerEthiraric <ethiraric@gmail.com>2024-02-27 15:20:49 +0100
commit03bb7908b964fd461b2ba277dcf4c04608b083ef (patch)
tree79606aa8d934f871bc77ed968c8bb12d2189d366 /tests
parent10136170fe9ed01e46aeb4f4479175b79eb0e3c7 (diff)
downloadrust-03bb7908b964fd461b2ba277dcf4c04608b083ef.tar.gz
rust-03bb7908b964fd461b2ba277dcf4c04608b083ef.zip
[`map_entry`]: Check insert expression for map use
The lint makes sure that the map is not used (borrowed) before the call
to `insert`. Since the lint creates a mutable borrow on the map with the
`Entry`, it wouldn't be possible to replace such code with `Entry`.
However, expressions up to the `insert` call are checked, but not
expressions for the arguments of the `insert` call itself. This commit
fixes that.

Fixes #11935
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/entry.fixed11
-rw-r--r--tests/ui/entry.rs11
2 files changed, 22 insertions, 0 deletions
diff --git a/tests/ui/entry.fixed b/tests/ui/entry.fixed
index 4099fe7e139..71ec13f4610 100644
--- a/tests/ui/entry.fixed
+++ b/tests/ui/entry.fixed
@@ -165,4 +165,15 @@ pub fn issue_10331() {
     }
 }
 
+/// Issue 11935
+/// Do not suggest using entries if the map is used inside the `insert` expression.
+pub fn issue_11935() {
+    let mut counts: HashMap<u64, u64> = HashMap::new();
+    if !counts.contains_key(&1) {
+        counts.insert(1, 1);
+    } else {
+        counts.insert(1, counts.get(&1).unwrap() + 1);
+    }
+}
+
 fn main() {}
diff --git a/tests/ui/entry.rs b/tests/ui/entry.rs
index 409be0aa060..86092b7c055 100644
--- a/tests/ui/entry.rs
+++ b/tests/ui/entry.rs
@@ -169,4 +169,15 @@ pub fn issue_10331() {
     }
 }
 
+/// Issue 11935
+/// Do not suggest using entries if the map is used inside the `insert` expression.
+pub fn issue_11935() {
+    let mut counts: HashMap<u64, u64> = HashMap::new();
+    if !counts.contains_key(&1) {
+        counts.insert(1, 1);
+    } else {
+        counts.insert(1, counts.get(&1).unwrap() + 1);
+    }
+}
+
 fn main() {}