1
1
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} ;
3
4
use if_chain:: if_chain;
4
5
use rustc:: hir:: intravisit:: { walk_expr, NestedVisitorMap , Visitor } ;
5
6
use rustc:: hir:: * ;
@@ -64,6 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
64
65
} else {
65
66
true
66
67
}
68
+ // XXXManishearth we can also check for if/else blocks containing `None`.
67
69
} ;
68
70
69
71
let mut visitor = InsertVisitor {
@@ -145,10 +147,11 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
145
147
span_lint_and_then( self . cx, MAP_ENTRY , self . span,
146
148
& format!( "usage of `contains_key` followed by `insert` on a `{}`" , self . ty) , |db| {
147
149
if self . sole_expr {
148
- 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, ".." ) ) ;
150
+ let mut app = Applicability :: MachineApplicable ;
151
+ let help = format!( "{}.entry({}).or_insert({});" ,
152
+ snippet_with_applicability( self . cx, self . map. span, "map" , & mut app) ,
153
+ snippet_with_applicability( self . cx, params[ 1 ] . span, ".." , & mut app) ,
154
+ snippet_with_applicability( self . cx, params[ 2 ] . span, ".." , & mut app) ) ;
152
155
153
156
db. span_suggestion(
154
157
self . span,
@@ -158,15 +161,13 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
158
161
) ;
159
162
}
160
163
else {
161
- let help = format!( "{}.entry({})" ,
164
+ let help = format!( "consider using ` {}.entry({})` " ,
162
165
snippet( self . cx, self . map. span, "map" ) ,
163
166
snippet( self . cx, params[ 1 ] . span, ".." ) ) ;
164
167
165
- db. span_suggestion (
168
+ db. span_label (
166
169
self . span,
167
- "consider using" ,
168
- help,
169
- Applicability :: MachineApplicable , // snippet
170
+ & help,
170
171
) ;
171
172
}
172
173
} ) ;
0 commit comments