mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
Split map_entry tests into fixable and unfixable
This commit is contained in:
parent
d445bf2e89
commit
e2f4b60661
7 changed files with 53 additions and 66 deletions
21
tests/ui/entry_fixable.rs
Normal file
21
tests/ui/entry_fixable.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
#![allow(unused, clippy::needless_pass_by_value)]
|
||||
#![warn(clippy::map_entry)]
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::hash::Hash;
|
||||
|
||||
fn foo() {}
|
||||
|
||||
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
m.insert(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
m.insert(o, v);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
12
tests/ui/entry_fixable.stderr
Normal file
12
tests/ui/entry_fixable.stderr
Normal file
|
@ -0,0 +1,12 @@
|
|||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry_fixable.rs:10:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | m.insert(k, v);
|
||||
LL | | }
|
||||
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
|
||||
|
|
||||
= note: `-D clippy::map-entry` implied by `-D warnings`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
|
@ -6,19 +6,6 @@ use std::hash::Hash;
|
|||
|
||||
fn foo() {}
|
||||
|
||||
fn insert_if_absent0<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
m.insert(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_if_absent1<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
foo();
|
||||
m.insert(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_if_absent2<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
m.insert(k, v)
|
||||
|
@ -62,12 +49,6 @@ fn insert_in_btreemap<K: Ord, V>(m: &mut BTreeMap<K, V>, k: K, v: V) {
|
|||
};
|
||||
}
|
||||
|
||||
fn insert_other_if_absent<K: Eq + Hash, V>(m: &mut HashMap<K, V>, k: K, o: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
||||
m.insert(o, v);
|
||||
}
|
||||
}
|
||||
|
||||
// should not trigger, because the one uses different HashMap from another one
|
||||
fn insert_from_different_map<K: Eq + Hash, V>(m: HashMap<K, V>, n: &mut HashMap<K, V>, k: K, v: V) {
|
||||
if !m.contains_key(&k) {
|
|
@ -1,43 +1,16 @@
|
|||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:10:5
|
||||
--> $DIR/entry_unfixable.rs:10:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | m.insert(k, v);
|
||||
LL | | }
|
||||
| |_____^ help: consider using: `m.entry(k).or_insert(v)`
|
||||
LL | | m.insert(k, v)
|
||||
LL | | } else {
|
||||
LL | | None
|
||||
LL | | };
|
||||
| |_____^
|
||||
|
|
||||
= note: `-D clippy::map-entry` implied by `-D warnings`
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:16:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
LL | | m.insert(k, v);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:16:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
LL | | m.insert(k, v);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:23:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | m.insert(k, v)
|
||||
LL | | } else {
|
||||
LL | | None
|
||||
LL | | };
|
||||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:23:5
|
||||
--> $DIR/entry_unfixable.rs:10:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | m.insert(k, v)
|
||||
|
@ -47,7 +20,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:31:5
|
||||
--> $DIR/entry_unfixable.rs:18:5
|
||||
|
|
||||
LL | / if m.contains_key(&k) {
|
||||
LL | | None
|
||||
|
@ -57,7 +30,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:31:5
|
||||
--> $DIR/entry_unfixable.rs:18:5
|
||||
|
|
||||
LL | / if m.contains_key(&k) {
|
||||
LL | | None
|
||||
|
@ -67,7 +40,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:39:5
|
||||
--> $DIR/entry_unfixable.rs:26:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
|
@ -78,7 +51,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:39:5
|
||||
--> $DIR/entry_unfixable.rs:26:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
|
@ -89,7 +62,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `HashMap`
|
||||
--> $DIR/entry.rs:48:5
|
||||
--> $DIR/entry_unfixable.rs:35:5
|
||||
|
|
||||
LL | / if m.contains_key(&k) {
|
||||
LL | | None
|
||||
|
@ -100,7 +73,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:48:5
|
||||
--> $DIR/entry_unfixable.rs:35:5
|
||||
|
|
||||
LL | / if m.contains_key(&k) {
|
||||
LL | | None
|
||||
|
@ -111,7 +84,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
||||
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
|
||||
--> $DIR/entry.rs:57:5
|
||||
--> $DIR/entry_unfixable.rs:44:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
|
@ -122,7 +95,7 @@ LL | | };
|
|||
| |_____^
|
||||
|
|
||||
help: consider using `m.entry(k)`
|
||||
--> $DIR/entry.rs:57:5
|
||||
--> $DIR/entry_unfixable.rs:44:5
|
||||
|
|
||||
LL | / if !m.contains_key(&k) {
|
||||
LL | | foo();
|
||||
|
@ -132,5 +105,5 @@ LL | | None
|
|||
LL | | };
|
||||
| |_____^
|
||||
|
||||
error: aborting due to 7 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
|
@ -14,7 +14,7 @@ fn str_lit_as_bytes() {
|
|||
|
||||
let strify = stringify!(foobar).as_bytes();
|
||||
|
||||
let includestr = include_bytes!("entry.rs");
|
||||
let includestr = include_bytes!("entry_unfixable.rs");
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -14,7 +14,7 @@ fn str_lit_as_bytes() {
|
|||
|
||||
let strify = stringify!(foobar).as_bytes();
|
||||
|
||||
let includestr = include_str!("entry.rs").as_bytes();
|
||||
let includestr = include_str!("entry_unfixable.rs").as_bytes();
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
@ -15,8 +15,8 @@ LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
|
|||
error: calling `as_bytes()` on `include_str!(..)`
|
||||
--> $DIR/string_lit_as_bytes.rs:17:22
|
||||
|
|
||||
LL | let includestr = include_str!("entry.rs").as_bytes();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`
|
||||
LL | let includestr = include_str!("entry_unfixable.rs").as_bytes();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry_unfixable.rs")`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
|
|
Loading…
Reference in a new issue