Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 181 additions & 148 deletions clippy_lints/src/entry.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/ui/entry.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@needs-asm-support

#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
#![allow(clippy::needless_pass_by_value, clippy::collapsible_if)]
#![warn(clippy::map_entry)]

use std::arch::asm;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@needs-asm-support

#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
#![allow(clippy::needless_pass_by_value, clippy::collapsible_if)]
#![warn(clippy::map_entry)]

use std::arch::asm;
Expand Down
1 change: 0 additions & 1 deletion tests/ui/entry_btree.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(clippy::map_entry)]
#![allow(dead_code)]

use std::collections::BTreeMap;

Expand Down
1 change: 0 additions & 1 deletion tests/ui/entry_btree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(clippy::map_entry)]
#![allow(dead_code)]

use std::collections::BTreeMap;

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/entry_btree.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
--> tests/ui/entry_btree.rs:10:5
--> tests/ui/entry_btree.rs:9:5
|
LL | / if !m.contains_key(&k) {
LL | |
Expand Down
75 changes: 41 additions & 34 deletions tests/ui/entry_unfixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,6 @@ macro_rules! insert {
};
}

mod issue13306 {
use std::collections::HashMap;

struct Env {
enclosing: Option<Box<Env>>,
values: HashMap<String, usize>,
}

impl Env {
fn assign(&mut self, name: String, value: usize) -> bool {
if !self.values.contains_key(&name) {
//~^ map_entry
self.values.insert(name, value);
true
} else if let Some(enclosing) = &mut self.enclosing {
enclosing.assign(name, value)
} else {
false
}
}
}
}

fn issue9925(mut hm: HashMap<String, bool>) {
let key = "hello".to_string();
if hm.contains_key(&key) {
//~^ map_entry
let bval = hm.get_mut(&key).unwrap();
*bval = false;
} else {
hm.insert(key, true);
}
}

mod issue9470 {
use std::collections::HashMap;
use std::sync::Mutex;
Expand Down Expand Up @@ -90,4 +56,45 @@ mod issue9470 {
}
}

fn issue9925(mut hm: HashMap<String, bool>) {
let key = "hello".to_string();
if hm.contains_key(&key) {
//~^ map_entry
let bval = hm.get_mut(&key).unwrap();
*bval = false;
} else {
hm.insert(key, true);
}
}

mod issue13306 {
use std::collections::HashMap;

struct Env {
enclosing: Option<Box<Env>>,
values: HashMap<String, usize>,
}

impl Env {
fn assign(&mut self, name: String, value: usize) -> bool {
if !self.values.contains_key(&name) {
//~^ map_entry
self.values.insert(name, value);
true
} else if let Some(enclosing) = &mut self.enclosing {
enclosing.assign(name, value)
} else {
false
}
}
}
}

fn issue15307<K: Eq + Hash, V>(mut m: HashMap<K, V>, k: K, v: V) {
if !m.contains_key(&k) {
//~^ map_entry
assert!(m.insert(k, v).is_none());
}
}

fn main() {}
37 changes: 24 additions & 13 deletions tests/ui/entry_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_unfixable.rs:27:13
--> tests/ui/entry_unfixable.rs:46:13
|
LL | / if !self.values.contains_key(&name) {
LL | / if self.globals.contains_key(&name) {
LL | |
LL | | self.values.insert(name, value);
LL | | true
... |
LL | | false
LL | | self.globals.insert(name, value);
LL | | } else {
LL | | let interner = INTERNER.lock().unwrap();
LL | | return Err(interner.resolve(name).unwrap().to_owned());
LL | | }
| |_____________^
|
Expand All @@ -15,7 +15,7 @@ LL | | }
= help: to override `-D warnings` add `#[allow(clippy::map_entry)]`

error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_unfixable.rs:42:5
--> tests/ui/entry_unfixable.rs:61:5
|
LL | / if hm.contains_key(&key) {
LL | |
Expand All @@ -31,16 +31,27 @@ LL | | }
error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_unfixable.rs:80:13
|
LL | / if self.globals.contains_key(&name) {
LL | / if !self.values.contains_key(&name) {
LL | |
LL | | self.globals.insert(name, value);
LL | | } else {
LL | | let interner = INTERNER.lock().unwrap();
LL | | return Err(interner.resolve(name).unwrap().to_owned());
LL | | self.values.insert(name, value);
LL | | true
... |
LL | | false
LL | | }
| |_____________^
|
= help: consider using the `Entry` API: https://doc.rust-lang.org/std/collections/struct.HashMap.html#entry-api

error: aborting due to 3 previous errors
error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_unfixable.rs:94:5
|
LL | / if !m.contains_key(&k) {
LL | |
LL | | assert!(m.insert(k, v).is_none());
LL | | }
| |_____^
|
= help: consider using the `Entry` API: https://doc.rust-lang.org/std/collections/struct.HashMap.html#entry-api

error: aborting due to 4 previous errors

13 changes: 1 addition & 12 deletions tests/ui/entry_with_else.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
#![allow(clippy::needless_pass_by_value, clippy::collapsible_if)]
#![warn(clippy::map_entry)]

use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -55,17 +55,6 @@ fn insert_if_absent0<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K,
}
}

match m.entry(k) {
std::collections::hash_map::Entry::Occupied(mut e) => {
//~^ map_entry
if true { Some(e.insert(v)) } else { Some(e.insert(v2)) }
}
std::collections::hash_map::Entry::Vacant(e) => {
e.insert(v);
None
}
};

if let std::collections::hash_map::Entry::Occupied(mut e) = m.entry(k) {
//~^ map_entry
foo();
Expand Down
9 changes: 1 addition & 8 deletions tests/ui/entry_with_else.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(unused, clippy::needless_pass_by_value, clippy::collapsible_if)]
#![allow(clippy::needless_pass_by_value, clippy::collapsible_if)]
#![warn(clippy::map_entry)]

use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -46,13 +46,6 @@ fn insert_if_absent0<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K,
m.insert(k, v2);
}

if m.contains_key(&k) {
//~^ map_entry
if true { m.insert(k, v) } else { m.insert(k, v2) }
} else {
m.insert(k, v)
};

if m.contains_key(&k) {
//~^ map_entry
foo();
Expand Down
27 changes: 1 addition & 26 deletions tests/ui/entry_with_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -119,31 +119,6 @@ error: usage of `contains_key` followed by `insert` on a `HashMap`
|
LL | / if m.contains_key(&k) {
LL | |
LL | | if true { m.insert(k, v) } else { m.insert(k, v2) }
LL | | } else {
LL | | m.insert(k, v)
LL | | };
| |_____^
|
help: try
|
LL ~ match m.entry(k) {
LL + std::collections::hash_map::Entry::Occupied(mut e) => {
LL +
LL + if true { Some(e.insert(v)) } else { Some(e.insert(v2)) }
LL + }
LL + std::collections::hash_map::Entry::Vacant(e) => {
LL + e.insert(v);
LL + None
LL + }
LL ~ };
|

error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_with_else.rs:56:5
|
LL | / if m.contains_key(&k) {
LL | |
LL | | foo();
LL | | m.insert(k, v)
LL | | } else {
Expand All @@ -162,5 +137,5 @@ LL + None
LL ~ };
|

error: aborting due to 7 previous errors
error: aborting due to 6 previous errors

16 changes: 16 additions & 0 deletions tests/ui/entry_with_else_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@no-rustfix
#![warn(clippy::map_entry)]

use std::collections::HashMap;
use std::hash::Hash;

fn foo() {}

fn insert_if_absent0<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, k: K, v: V, v2: V) {
if m.contains_key(&k) {
//~^ map_entry
if true { m.insert(k, v) } else { m.insert(k, v2) }
} else {
m.insert(k, v)
};
}
17 changes: 17 additions & 0 deletions tests/ui/entry_with_else_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: usage of `contains_key` followed by `insert` on a `HashMap`
--> tests/ui/entry_with_else_unfixable.rs:10:5
|
LL | / if m.contains_key(&k) {
LL | |
LL | | if true { m.insert(k, v) } else { m.insert(k, v2) }
LL | | } else {
LL | | m.insert(k, v)
LL | | };
| |_____^
|
= help: consider using the `Entry` API: https://doc.rust-lang.org/std/collections/struct.HashMap.html#entry-api
= note: `-D clippy::map-entry` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::map_entry)]`

error: aborting due to 1 previous error