Skip to content

Commit e2f4b60

Browse files
committed
Split map_entry tests into fixable and unfixable
1 parent d445bf2 commit e2f4b60

7 files changed

+49
-62
lines changed

tests/ui/entry_fixable.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![allow(unused, clippy::needless_pass_by_value)]
2+
#![warn(clippy::map_entry)]
3+
4+
use std::collections::{BTreeMap, HashMap};
5+
use std::hash::Hash;
6+
7+
fn foo() {}
8+
9+
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
10+
if !m.contains_key(&k) {
11+
m.insert(k, v);
12+
}
13+
}
14+
15+
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
16+
if !m.contains_key(&k) {
17+
m.insert(o, v);
18+
}
19+
}
20+
21+
fn main() {}

tests/ui/entry_fixable.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: usage of `contains_key` followed by `insert` on a `HashMap`
2+
--> $DIR/entry_fixable.rs:10:5
3+
|
4+
LL | / if !m.contains_key(&k) {
5+
LL | | m.insert(k, v);
6+
LL | | }
7+
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
8+
|
9+
= note: `-D clippy::map-entry` implied by `-D warnings`
10+
11+
error: aborting due to previous error
12+

tests/ui/entry.rs renamed to tests/ui/entry_unfixable.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,6 @@ use std::hash::Hash;
66

77
fn foo() {}
88

9-
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
10-
if !m.contains_key(&k) {
11-
m.insert(k, v);
12-
}
13-
}
14-
15-
fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
16-
if !m.contains_key(&k) {
17-
foo();
18-
m.insert(k, v);
19-
}
20-
}
21-
229
fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
2310
if !m.contains_key(&k) {
2411
m.insert(k, v)
@@ -62,12 +49,6 @@ fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
6249
};
6350
}
6451

65-
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
66-
if !m.contains_key(&k) {
67-
m.insert(o, v);
68-
}
69-
}
70-
7152
// should not trigger, because the one uses different HashMap from another one
7253
fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
7354
if !m.contains_key(&k) {
Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,5 @@
11
error: usage of `contains_key` followed by `insert` on a `HashMap`
2-
--> $DIR/entry.rs:10:5
3-
|
4-
LL | / if !m.contains_key(&k) {
5-
LL | | m.insert(k, v);
6-
LL | | }
7-
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
8-
|
9-
= note: `-D clippy::map-entry` implied by `-D warnings`
10-
11-
error: usage of `contains_key` followed by `insert` on a `HashMap`
12-
--> $DIR/entry.rs:16:5
13-
|
14-
LL | / if !m.contains_key(&k) {
15-
LL | | foo();
16-
LL | | m.insert(k, v);
17-
LL | | }
18-
| |_____^
19-
|
20-
help: consider using `m.entry(k)`
21-
--> $DIR/entry.rs:16:5
22-
|
23-
LL | / if !m.contains_key(&k) {
24-
LL | | foo();
25-
LL | | m.insert(k, v);
26-
LL | | }
27-
| |_____^
28-
29-
error: usage of `contains_key` followed by `insert` on a `HashMap`
30-
--> $DIR/entry.rs:23:5
2+
--> $DIR/entry_unfixable.rs:10:5
313
|
324
LL | / if !m.contains_key(&k) {
335
LL | | m.insert(k, v)
@@ -36,8 +8,9 @@ LL | | None
368
LL | | };
379
| |_____^
3810
|
11+
= note: `-D clippy::map-entry` implied by `-D warnings`
3912
help: consider using `m.entry(k)`
40-
--> $DIR/entry.rs:23:5
13+
--> $DIR/entry_unfixable.rs:10:5
4114
|
4215
LL | / if !m.contains_key(&k) {
4316
LL | | m.insert(k, v)
@@ -47,7 +20,7 @@ LL | | };
4720
| |_____^
4821

4922
error: usage of `contains_key` followed by `insert` on a `HashMap`
50-
--> $DIR/entry.rs:31:5
23+
--> $DIR/entry_unfixable.rs:18:5
5124
|
5225
LL | / if m.contains_key(&k) {
5326
LL | | None
@@ -57,7 +30,7 @@ LL | | };
5730
| |_____^
5831
|
5932
help: consider using `m.entry(k)`
60-
--> $DIR/entry.rs:31:5
33+
--> $DIR/entry_unfixable.rs:18:5
6134
|
6235
LL | / if m.contains_key(&k) {
6336
LL | | None
@@ -67,7 +40,7 @@ LL | | };
6740
| |_____^
6841

6942
error: usage of `contains_key` followed by `insert` on a `HashMap`
70-
--> $DIR/entry.rs:39:5
43+
--> $DIR/entry_unfixable.rs:26:5
7144
|
7245
LL | / if !m.contains_key(&k) {
7346
LL | | foo();
@@ -78,7 +51,7 @@ LL | | };
7851
| |_____^
7952
|
8053
help: consider using `m.entry(k)`
81-
--> $DIR/entry.rs:39:5
54+
--> $DIR/entry_unfixable.rs:26:5
8255
|
8356
LL | / if !m.contains_key(&k) {
8457
LL | | foo();
@@ -89,7 +62,7 @@ LL | | };
8962
| |_____^
9063

9164
error: usage of `contains_key` followed by `insert` on a `HashMap`
92-
--> $DIR/entry.rs:48:5
65+
--> $DIR/entry_unfixable.rs:35:5
9366
|
9467
LL | / if m.contains_key(&k) {
9568
LL | | None
@@ -100,7 +73,7 @@ LL | | };
10073
| |_____^
10174
|
10275
help: consider using `m.entry(k)`
103-
--> $DIR/entry.rs:48:5
76+
--> $DIR/entry_unfixable.rs:35:5
10477
|
10578
LL | / if m.contains_key(&k) {
10679
LL | | None
@@ -111,7 +84,7 @@ LL | | };
11184
| |_____^
11285

11386
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
114-
--> $DIR/entry.rs:57:5
87+
--> $DIR/entry_unfixable.rs:44:5
11588
|
11689
LL | / if !m.contains_key(&k) {
11790
LL | | foo();
@@ -122,7 +95,7 @@ LL | | };
12295
| |_____^
12396
|
12497
help: consider using `m.entry(k)`
125-
--> $DIR/entry.rs:57:5
98+
--> $DIR/entry_unfixable.rs:44:5
12699
|
127100
LL | / if !m.contains_key(&k) {
128101
LL | | foo();
@@ -132,5 +105,5 @@ LL | | None
132105
LL | | };
133106
| |_____^
134107

135-
error: aborting due to 7 previous errors
108+
error: aborting due to 5 previous errors
136109

tests/ui/string_lit_as_bytes.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn str_lit_as_bytes() {
1414

1515
let strify = stringify!(foobar).as_bytes();
1616

17-
let includestr = include_bytes!("entry.rs");
17+
let includestr = include_bytes!("entry_unfixable.rs");
1818
}
1919

2020
fn main() {}

tests/ui/string_lit_as_bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn str_lit_as_bytes() {
1414

1515
let strify = stringify!(foobar).as_bytes();
1616

17-
let includestr = include_str!("entry.rs").as_bytes();
17+
let includestr = include_str!("entry_unfixable.rs").as_bytes();
1818
}
1919

2020
fn main() {}

tests/ui/string_lit_as_bytes.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
1515
error: calling `as_bytes()` on `include_str!(..)`
1616
--> $DIR/string_lit_as_bytes.rs:17:22
1717
|
18-
LL | let includestr = include_str!("entry.rs").as_bytes();
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`
18+
LL | let includestr = include_str!("entry_unfixable.rs").as_bytes();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry_unfixable.rs")`
2020

2121
error: aborting due to 3 previous errors
2222

0 commit comments

Comments
 (0)