From e2f4b6066132f48b026cc9b7947c1eb45a3d825d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 25 Sep 2019 04:31:46 -0700 Subject: [PATCH] Split map_entry tests into fixable and unfixable --- tests/ui/entry_fixable.rs | 21 +++++++ tests/ui/entry_fixable.stderr | 12 ++++ tests/ui/{entry.rs => entry_unfixable.rs} | 19 ------ .../{entry.stderr => entry_unfixable.stderr} | 59 +++++-------------- tests/ui/string_lit_as_bytes.fixed | 2 +- tests/ui/string_lit_as_bytes.rs | 2 +- tests/ui/string_lit_as_bytes.stderr | 4 +- 7 files changed, 53 insertions(+), 66 deletions(-) create mode 100644 tests/ui/entry_fixable.rs create mode 100644 tests/ui/entry_fixable.stderr rename tests/ui/{entry.rs => entry_unfixable.rs} (77%) rename tests/ui/{entry.stderr => entry_unfixable.stderr} (69%) diff --git a/tests/ui/entry_fixable.rs b/tests/ui/entry_fixable.rs new file mode 100644 index 000000000..82267cf34 --- /dev/null +++ b/tests/ui/entry_fixable.rs @@ -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(m: &mut HashMap, k: K, v: V) { + if !m.contains_key(&k) { + m.insert(k, v); + } +} + +fn insert_other_if_absent(m: &mut HashMap, k: K, o: K, v: V) { + if !m.contains_key(&k) { + m.insert(o, v); + } +} + +fn main() {} diff --git a/tests/ui/entry_fixable.stderr b/tests/ui/entry_fixable.stderr new file mode 100644 index 000000000..59a20e5c0 --- /dev/null +++ b/tests/ui/entry_fixable.stderr @@ -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 + diff --git a/tests/ui/entry.rs b/tests/ui/entry_unfixable.rs similarity index 77% rename from tests/ui/entry.rs rename to tests/ui/entry_unfixable.rs index 0c84cd325..8859a3a2f 100644 --- a/tests/ui/entry.rs +++ b/tests/ui/entry_unfixable.rs @@ -6,19 +6,6 @@ use std::hash::Hash; fn foo() {} -fn insert_if_absent0(m: &mut HashMap, k: K, v: V) { - if !m.contains_key(&k) { - m.insert(k, v); - } -} - -fn insert_if_absent1(m: &mut HashMap, k: K, v: V) { - if !m.contains_key(&k) { - foo(); - m.insert(k, v); - } -} - fn insert_if_absent2(m: &mut HashMap, k: K, v: V) { if !m.contains_key(&k) { m.insert(k, v) @@ -62,12 +49,6 @@ fn insert_in_btreemap(m: &mut BTreeMap, k: K, v: V) { }; } -fn insert_other_if_absent(m: &mut HashMap, 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(m: HashMap, n: &mut HashMap, k: K, v: V) { if !m.contains_key(&k) { diff --git a/tests/ui/entry.stderr b/tests/ui/entry_unfixable.stderr similarity index 69% rename from tests/ui/entry.stderr rename to tests/ui/entry_unfixable.stderr index d17456ef8..ab655fcfe 100644 --- a/tests/ui/entry.stderr +++ b/tests/ui/entry_unfixable.stderr @@ -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 diff --git a/tests/ui/string_lit_as_bytes.fixed b/tests/ui/string_lit_as_bytes.fixed index 192247816..c8d1479f1 100644 --- a/tests/ui/string_lit_as_bytes.fixed +++ b/tests/ui/string_lit_as_bytes.fixed @@ -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() {} diff --git a/tests/ui/string_lit_as_bytes.rs b/tests/ui/string_lit_as_bytes.rs index 560cbcb65..f0066d5d1 100644 --- a/tests/ui/string_lit_as_bytes.rs +++ b/tests/ui/string_lit_as_bytes.rs @@ -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() {} diff --git a/tests/ui/string_lit_as_bytes.stderr b/tests/ui/string_lit_as_bytes.stderr index 59aaec75b..d6c6c5270 100644 --- a/tests/ui/string_lit_as_bytes.stderr +++ b/tests/ui/string_lit_as_bytes.stderr @@ -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