about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tests/ui/unnecessary_get_then_check.fixed26
-rw-r--r--tests/ui/unnecessary_get_then_check.rs26
-rw-r--r--tests/ui/unnecessary_get_then_check.stderr75
3 files changed, 127 insertions, 0 deletions
diff --git a/tests/ui/unnecessary_get_then_check.fixed b/tests/ui/unnecessary_get_then_check.fixed
new file mode 100644
index 00000000000..178a3300c34
--- /dev/null
+++ b/tests/ui/unnecessary_get_then_check.fixed
@@ -0,0 +1,26 @@
+#![warn(clippy::unnecessary_get_then_check)]
+
+use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
+
+fn main() {
+    let s: HashSet<String> = HashSet::new();
+    let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: HashMap<String, ()> = HashMap::new();
+    let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: BTreeSet<String> = BTreeSet::new();
+    let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: BTreeMap<String, ()> = BTreeMap::new();
+    let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    // Import to check that the generic annotations are kept!
+    let s: HashSet<String> = HashSet::new();
+    let _ = s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_some()`
+    let _ = !s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_none()`
+}
diff --git a/tests/ui/unnecessary_get_then_check.rs b/tests/ui/unnecessary_get_then_check.rs
new file mode 100644
index 00000000000..c197bdef47e
--- /dev/null
+++ b/tests/ui/unnecessary_get_then_check.rs
@@ -0,0 +1,26 @@
+#![warn(clippy::unnecessary_get_then_check)]
+
+use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
+
+fn main() {
+    let s: HashSet<String> = HashSet::new();
+    let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: HashMap<String, ()> = HashMap::new();
+    let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: BTreeSet<String> = BTreeSet::new();
+    let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    let s: BTreeMap<String, ()> = BTreeMap::new();
+    let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
+    let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
+
+    // Import to check that the generic annotations are kept!
+    let s: HashSet<String> = HashSet::new();
+    let _ = s.get::<str>("a").is_some(); //~ ERROR: unnecessary use of `get::<str>("a").is_some()`
+    let _ = s.get::<str>("a").is_none(); //~ ERROR: unnecessary use of `get::<str>("a").is_none()`
+}
diff --git a/tests/ui/unnecessary_get_then_check.stderr b/tests/ui/unnecessary_get_then_check.stderr
new file mode 100644
index 00000000000..0477c03d16d
--- /dev/null
+++ b/tests/ui/unnecessary_get_then_check.stderr
@@ -0,0 +1,75 @@
+error: unnecessary use of `get("a").is_some()`
+  --> tests/ui/unnecessary_get_then_check.rs:7:15
+   |
+LL |     let _ = s.get("a").is_some();
+   |               ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains("a")`
+   |
+   = note: `-D clippy::unnecessary-get-then-check` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::unnecessary_get_then_check)]`
+
+error: unnecessary use of `get("a").is_none()`
+  --> tests/ui/unnecessary_get_then_check.rs:8:15
+   |
+LL |     let _ = s.get("a").is_none();
+   |             --^^^^^^^^^^^^^^^^^^
+   |             |
+   |             help: replace it with: `!s.contains("a")`
+
+error: unnecessary use of `get("a").is_some()`
+  --> tests/ui/unnecessary_get_then_check.rs:11:15
+   |
+LL |     let _ = s.get("a").is_some();
+   |               ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("a")`
+
+error: unnecessary use of `get("a").is_none()`
+  --> tests/ui/unnecessary_get_then_check.rs:12:15
+   |
+LL |     let _ = s.get("a").is_none();
+   |             --^^^^^^^^^^^^^^^^^^
+   |             |
+   |             help: replace it with: `!s.contains_key("a")`
+
+error: unnecessary use of `get("a").is_some()`
+  --> tests/ui/unnecessary_get_then_check.rs:15:15
+   |
+LL |     let _ = s.get("a").is_some();
+   |               ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains("a")`
+
+error: unnecessary use of `get("a").is_none()`
+  --> tests/ui/unnecessary_get_then_check.rs:16:15
+   |
+LL |     let _ = s.get("a").is_none();
+   |             --^^^^^^^^^^^^^^^^^^
+   |             |
+   |             help: replace it with: `!s.contains("a")`
+
+error: unnecessary use of `get("a").is_some()`
+  --> tests/ui/unnecessary_get_then_check.rs:19:15
+   |
+LL |     let _ = s.get("a").is_some();
+   |               ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("a")`
+
+error: unnecessary use of `get("a").is_none()`
+  --> tests/ui/unnecessary_get_then_check.rs:20:15
+   |
+LL |     let _ = s.get("a").is_none();
+   |             --^^^^^^^^^^^^^^^^^^
+   |             |
+   |             help: replace it with: `!s.contains_key("a")`
+
+error: unnecessary use of `get::<str>("a").is_some()`
+  --> tests/ui/unnecessary_get_then_check.rs:24:15
+   |
+LL |     let _ = s.get::<str>("a").is_some();
+   |               ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains::<str>("a")`
+
+error: unnecessary use of `get::<str>("a").is_none()`
+  --> tests/ui/unnecessary_get_then_check.rs:25:15
+   |
+LL |     let _ = s.get::<str>("a").is_none();
+   |             --^^^^^^^^^^^^^^^^^^^^^^^^^
+   |             |
+   |             help: replace it with: `!s.contains::<str>("a")`
+
+error: aborting due to 10 previous errors
+