Skip to content

Commit 40cff2d

Browse files
Add ui test for unnecessary_get_then_check
1 parent ad31948 commit 40cff2d

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![warn(clippy::unnecessary_get_then_check)]
2+
3+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
4+
5+
fn main() {
6+
let s: HashSet<String> = HashSet::new();
7+
let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
8+
let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
9+
10+
let s: HashMap<String, ()> = HashMap::new();
11+
let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
12+
let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
13+
14+
let s: BTreeSet<String> = BTreeSet::new();
15+
let _ = s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
16+
let _ = !s.contains("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
17+
18+
let s: BTreeMap<String, ()> = BTreeMap::new();
19+
let _ = s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_some()`
20+
let _ = !s.contains_key("a"); //~ ERROR: unnecessary use of `get("a").is_none()`
21+
22+
// Import to check that the generic annotations are kept!
23+
let s: HashSet<String> = HashSet::new();
24+
let _ = s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_some()`
25+
let _ = !s.contains::<str>("a"); //~ ERROR: unnecessary use of `get::<str>("a").is_none()`
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![warn(clippy::unnecessary_get_then_check)]
2+
3+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
4+
5+
fn main() {
6+
let s: HashSet<String> = HashSet::new();
7+
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
8+
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
9+
10+
let s: HashMap<String, ()> = HashMap::new();
11+
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
12+
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
13+
14+
let s: BTreeSet<String> = BTreeSet::new();
15+
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
16+
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
17+
18+
let s: BTreeMap<String, ()> = BTreeMap::new();
19+
let _ = s.get("a").is_some(); //~ ERROR: unnecessary use of `get("a").is_some()`
20+
let _ = s.get("a").is_none(); //~ ERROR: unnecessary use of `get("a").is_none()`
21+
22+
// Import to check that the generic annotations are kept!
23+
let s: HashSet<String> = HashSet::new();
24+
let _ = s.get::<str>("a").is_some(); //~ ERROR: unnecessary use of `get::<str>("a").is_some()`
25+
let _ = s.get::<str>("a").is_none(); //~ ERROR: unnecessary use of `get::<str>("a").is_none()`
26+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error: unnecessary use of `get("a").is_some()`
2+
--> tests/ui/unnecessary_get_then_check.rs:7:15
3+
|
4+
LL | let _ = s.get("a").is_some();
5+
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains("a")`
6+
|
7+
= note: `-D clippy::unnecessary-get-then-check` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_get_then_check)]`
9+
10+
error: unnecessary use of `get("a").is_none()`
11+
--> tests/ui/unnecessary_get_then_check.rs:8:15
12+
|
13+
LL | let _ = s.get("a").is_none();
14+
| --^^^^^^^^^^^^^^^^^^
15+
| |
16+
| help: replace it with: `!s.contains("a")`
17+
18+
error: unnecessary use of `get("a").is_some()`
19+
--> tests/ui/unnecessary_get_then_check.rs:11:15
20+
|
21+
LL | let _ = s.get("a").is_some();
22+
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("a")`
23+
24+
error: unnecessary use of `get("a").is_none()`
25+
--> tests/ui/unnecessary_get_then_check.rs:12:15
26+
|
27+
LL | let _ = s.get("a").is_none();
28+
| --^^^^^^^^^^^^^^^^^^
29+
| |
30+
| help: replace it with: `!s.contains_key("a")`
31+
32+
error: unnecessary use of `get("a").is_some()`
33+
--> tests/ui/unnecessary_get_then_check.rs:15:15
34+
|
35+
LL | let _ = s.get("a").is_some();
36+
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains("a")`
37+
38+
error: unnecessary use of `get("a").is_none()`
39+
--> tests/ui/unnecessary_get_then_check.rs:16:15
40+
|
41+
LL | let _ = s.get("a").is_none();
42+
| --^^^^^^^^^^^^^^^^^^
43+
| |
44+
| help: replace it with: `!s.contains("a")`
45+
46+
error: unnecessary use of `get("a").is_some()`
47+
--> tests/ui/unnecessary_get_then_check.rs:19:15
48+
|
49+
LL | let _ = s.get("a").is_some();
50+
| ^^^^^^^^^^^^^^^^^^ help: replace it with: `contains_key("a")`
51+
52+
error: unnecessary use of `get("a").is_none()`
53+
--> tests/ui/unnecessary_get_then_check.rs:20:15
54+
|
55+
LL | let _ = s.get("a").is_none();
56+
| --^^^^^^^^^^^^^^^^^^
57+
| |
58+
| help: replace it with: `!s.contains_key("a")`
59+
60+
error: unnecessary use of `get::<str>("a").is_some()`
61+
--> tests/ui/unnecessary_get_then_check.rs:24:15
62+
|
63+
LL | let _ = s.get::<str>("a").is_some();
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `contains::<str>("a")`
65+
66+
error: unnecessary use of `get::<str>("a").is_none()`
67+
--> tests/ui/unnecessary_get_then_check.rs:25:15
68+
|
69+
LL | let _ = s.get::<str>("a").is_none();
70+
| --^^^^^^^^^^^^^^^^^^^^^^^^^
71+
| |
72+
| help: replace it with: `!s.contains::<str>("a")`
73+
74+
error: aborting due to 10 previous errors
75+

0 commit comments

Comments
 (0)