Skip to content

Commit d445bf2

Browse files
committed
Remove suggestion for complex map_entry cases
1 parent a756b9b commit d445bf2

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

clippy_lints/src/entry.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::SpanlessEq;
2-
use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty};
2+
use crate::utils::{get_item_name, higher, match_type, paths, snippet, snippet_opt};
3+
use crate::utils::{snippet_with_applicability, span_lint_and_then, walk_ptrs_ty};
34
use if_chain::if_chain;
45
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
56
use rustc::hir::*;
@@ -145,10 +146,11 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
145146
span_lint_and_then(self.cx, MAP_ENTRY, self.span,
146147
&format!("usage of `contains_key` followed by `insert` on a `{}`", self.ty), |db| {
147148
if self.sole_expr {
149+
let mut app = Applicability::MachineApplicable;
148150
let help = format!("{}.entry({}).or_insert({})",
149-
snippet(self.cx, self.map.span, "map"),
150-
snippet(self.cx, params[1].span, ".."),
151-
snippet(self.cx, params[2].span, ".."));
151+
snippet_with_applicability(self.cx, self.map.span, "map", &mut app),
152+
snippet_with_applicability(self.cx, params[1].span, "..", &mut app),
153+
snippet_with_applicability(self.cx, params[2].span, "..", &mut app));
152154

153155
db.span_suggestion(
154156
self.span,
@@ -158,15 +160,13 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
158160
);
159161
}
160162
else {
161-
let help = format!("{}.entry({})",
163+
let help = format!("consider using `{}.entry({})`",
162164
snippet(self.cx, self.map.span, "map"),
163165
snippet(self.cx, params[1].span, ".."));
164166

165-
db.span_suggestion(
167+
db.span_help(
166168
self.span,
167-
"consider using",
168-
help,
169-
Applicability::MachineApplicable, // snippet
169+
&help,
170170
);
171171
}
172172
});

tests/ui/entry.stderr

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ LL | / if !m.contains_key(&k) {
1515
LL | | foo();
1616
LL | | m.insert(k, v);
1717
LL | | }
18-
| |_____^ help: consider using: `m.entry(k)`
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+
| |_____^
1928

2029
error: usage of `contains_key` followed by `insert` on a `HashMap`
2130
--> $DIR/entry.rs:23:5
@@ -25,7 +34,17 @@ LL | | m.insert(k, v)
2534
LL | | } else {
2635
LL | | None
2736
LL | | };
28-
| |_____^ help: consider using: `m.entry(k)`
37+
| |_____^
38+
|
39+
help: consider using `m.entry(k)`
40+
--> $DIR/entry.rs:23:5
41+
|
42+
LL | / if !m.contains_key(&k) {
43+
LL | | m.insert(k, v)
44+
LL | | } else {
45+
LL | | None
46+
LL | | };
47+
| |_____^
2948

3049
error: usage of `contains_key` followed by `insert` on a `HashMap`
3150
--> $DIR/entry.rs:31:5
@@ -35,7 +54,17 @@ LL | | None
3554
LL | | } else {
3655
LL | | m.insert(k, v)
3756
LL | | };
38-
| |_____^ help: consider using: `m.entry(k)`
57+
| |_____^
58+
|
59+
help: consider using `m.entry(k)`
60+
--> $DIR/entry.rs:31:5
61+
|
62+
LL | / if m.contains_key(&k) {
63+
LL | | None
64+
LL | | } else {
65+
LL | | m.insert(k, v)
66+
LL | | };
67+
| |_____^
3968

4069
error: usage of `contains_key` followed by `insert` on a `HashMap`
4170
--> $DIR/entry.rs:39:5
@@ -46,7 +75,18 @@ LL | | m.insert(k, v)
4675
LL | | } else {
4776
LL | | None
4877
LL | | };
49-
| |_____^ help: consider using: `m.entry(k)`
78+
| |_____^
79+
|
80+
help: consider using `m.entry(k)`
81+
--> $DIR/entry.rs:39:5
82+
|
83+
LL | / if !m.contains_key(&k) {
84+
LL | | foo();
85+
LL | | m.insert(k, v)
86+
LL | | } else {
87+
LL | | None
88+
LL | | };
89+
| |_____^
5090

5191
error: usage of `contains_key` followed by `insert` on a `HashMap`
5292
--> $DIR/entry.rs:48:5
@@ -57,7 +97,18 @@ LL | | } else {
5797
LL | | foo();
5898
LL | | m.insert(k, v)
5999
LL | | };
60-
| |_____^ help: consider using: `m.entry(k)`
100+
| |_____^
101+
|
102+
help: consider using `m.entry(k)`
103+
--> $DIR/entry.rs:48:5
104+
|
105+
LL | / if m.contains_key(&k) {
106+
LL | | None
107+
LL | | } else {
108+
LL | | foo();
109+
LL | | m.insert(k, v)
110+
LL | | };
111+
| |_____^
61112

62113
error: usage of `contains_key` followed by `insert` on a `BTreeMap`
63114
--> $DIR/entry.rs:57:5
@@ -68,7 +119,18 @@ LL | | m.insert(k, v)
68119
LL | | } else {
69120
LL | | None
70121
LL | | };
71-
| |_____^ help: consider using: `m.entry(k)`
122+
| |_____^
123+
|
124+
help: consider using `m.entry(k)`
125+
--> $DIR/entry.rs:57:5
126+
|
127+
LL | / if !m.contains_key(&k) {
128+
LL | | foo();
129+
LL | | m.insert(k, v)
130+
LL | | } else {
131+
LL | | None
132+
LL | | };
133+
| |_____^
72134

73135
error: aborting due to 7 previous errors
74136

0 commit comments

Comments
 (0)