From 40cff2d99f9723f4bfcf4bd9c7291bbb35fc323e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 23 Feb 2024 16:33:08 +0100 Subject: [PATCH] Add ui test for `unnecessary_get_then_check` --- tests/ui/unnecessary_get_then_check.fixed | 26 ++++++++ tests/ui/unnecessary_get_then_check.rs | 26 ++++++++ tests/ui/unnecessary_get_then_check.stderr | 75 ++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 tests/ui/unnecessary_get_then_check.fixed create mode 100644 tests/ui/unnecessary_get_then_check.rs create mode 100644 tests/ui/unnecessary_get_then_check.stderr 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 = 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 = 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 = 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 = 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 = 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()` +} 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 = 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 = 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 = 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 = 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 = 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()` +} 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::("a").is_some()` + --> tests/ui/unnecessary_get_then_check.rs:24: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:25:15 + | +LL | let _ = s.get::("a").is_none(); + | --^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | help: replace it with: `!s.contains::("a")` + +error: aborting due to 10 previous errors +