@@ -95,14 +95,13 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
95
95
return ;
96
96
} ;
97
97
98
- if then_search. is_key_used_and_no_copy || else_search. is_key_used_and_no_copy {
99
- span_lint ( cx, MAP_ENTRY , expr. span , lint_msg) ;
100
- return ;
101
- }
102
-
103
98
if then_search. edits . is_empty ( ) && else_search. edits . is_empty ( ) {
104
99
// No insertions
105
100
return ;
101
+ } else if then_search. is_key_used_and_no_copy || else_search. is_key_used_and_no_copy {
102
+ // If there are other uses of the key, and the key is not copy,
103
+ // we cannot perform a fix automatically, but continue to emit a lint.
104
+ None
106
105
} else if then_search. edits . is_empty ( ) || else_search. edits . is_empty ( ) {
107
106
// if .. { insert } else { .. } or if .. { .. } else { insert }
108
107
let ( ( then_str, entry_kind) , else_str) = match ( else_search. edits . is_empty ( ) , contains_expr. negated ) {
@@ -123,10 +122,10 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
123
122
snippet_with_applicability ( cx, then_expr. span , "{ .. }" , & mut app) ,
124
123
) ,
125
124
} ;
126
- format ! (
125
+ Some ( format ! (
127
126
"if let {}::{entry_kind} = {map_str}.entry({key_str}) {then_str} else {else_str}" ,
128
127
map_ty. entry_path( ) ,
129
- )
128
+ ) )
130
129
} else {
131
130
// if .. { insert } else { insert }
132
131
let ( ( then_str, then_entry) , ( else_str, else_entry) ) = if contains_expr. negated {
@@ -142,13 +141,13 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
142
141
} ;
143
142
let indent_str = snippet_indent ( cx, expr. span ) ;
144
143
let indent_str = indent_str. as_deref ( ) . unwrap_or ( "" ) ;
145
- format ! (
144
+ Some ( format ! (
146
145
"match {map_str}.entry({key_str}) {{\n {indent_str} {entry}::{then_entry} => {}\n \
147
146
{indent_str} {entry}::{else_entry} => {}\n {indent_str}}}",
148
147
reindent_multiline( & then_str, true , Some ( 4 + indent_str. len( ) ) ) ,
149
148
reindent_multiline( & else_str, true , Some ( 4 + indent_str. len( ) ) ) ,
150
149
entry = map_ty. entry_path( ) ,
151
- )
150
+ ) )
152
151
}
153
152
} else {
154
153
if then_search. edits . is_empty ( ) {
@@ -163,17 +162,17 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
163
162
} else {
164
163
then_search. snippet_occupied ( cx, then_expr. span , & mut app)
165
164
} ;
166
- format ! (
165
+ Some ( format ! (
167
166
"if let {}::{entry_kind} = {map_str}.entry({key_str}) {body_str}" ,
168
167
map_ty. entry_path( ) ,
169
- )
168
+ ) )
170
169
} else if let Some ( insertion) = then_search. as_single_insertion ( ) {
171
170
let value_str = snippet_with_context ( cx, insertion. value . span , then_expr. span . ctxt ( ) , ".." , & mut app) . 0 ;
172
171
if contains_expr. negated {
173
172
if insertion. value . can_have_side_effects ( ) {
174
- format ! ( "{map_str}.entry({key_str}).or_insert_with(|| {value_str});" )
173
+ Some ( format ! ( "{map_str}.entry({key_str}).or_insert_with(|| {value_str});" ) )
175
174
} else {
176
- format ! ( "{map_str}.entry({key_str}).or_insert({value_str});" )
175
+ Some ( format ! ( "{map_str}.entry({key_str}).or_insert({value_str});" ) )
177
176
}
178
177
} else {
179
178
// TODO: suggest using `if let Some(v) = map.get_mut(k) { .. }` here.
@@ -183,7 +182,7 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
183
182
} else {
184
183
let block_str = then_search. snippet_closure ( cx, then_expr. span , & mut app) ;
185
184
if contains_expr. negated {
186
- format ! ( "{map_str}.entry({key_str}).or_insert_with(|| {block_str});" )
185
+ Some ( format ! ( "{map_str}.entry({key_str}).or_insert_with(|| {block_str});" ) )
187
186
} else {
188
187
// TODO: suggest using `if let Some(v) = map.get_mut(k) { .. }` here.
189
188
// This would need to be a different lint.
@@ -192,7 +191,11 @@ impl<'tcx> LateLintPass<'tcx> for HashMapPass {
192
191
}
193
192
} ;
194
193
195
- span_lint_and_sugg ( cx, MAP_ENTRY , expr. span , lint_msg, "try" , sugg, app) ;
194
+ if let Some ( sugg) = sugg {
195
+ span_lint_and_sugg ( cx, MAP_ENTRY , expr. span , lint_msg, "try" , sugg, app) ;
196
+ } else {
197
+ span_lint ( cx, MAP_ENTRY , expr. span , lint_msg) ;
198
+ }
196
199
}
197
200
}
198
201
0 commit comments