Add ui test for unnecessary_get_then_check

This commit is contained in:
Guillaume Gomez 2024-02-23 16:33:08 +01:00
parent ad319484f1
commit 40cff2d99f
3 changed files with 127 additions and 0 deletions

View File

@ -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()`
}

View File

@ -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()`
}

View File

@ -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