11use 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} ;
34use if_chain:: if_chain;
45use rustc:: hir:: intravisit:: { walk_expr, NestedVisitorMap , Visitor } ;
56use rustc:: hir:: * ;
@@ -64,6 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
6465 } else {
6566 true
6667 }
68+ // XXXManishearth we can also check for if/else blocks containing `None`.
6769 } ;
6870
6971 let mut visitor = InsertVisitor {
@@ -145,10 +147,11 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
145147 span_lint_and_then( self . cx, MAP_ENTRY , self . span,
146148 & format!( "usage of `contains_key` followed by `insert` on a `{}`" , self . ty) , |db| {
147149 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) ) ;
152155
153156 db. span_suggestion(
154157 self . span,
@@ -158,15 +161,13 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for InsertVisitor<'a, 'tcx, 'b> {
158161 ) ;
159162 }
160163 else {
161- let help = format!( "{}.entry({})" ,
164+ let help = format!( "consider using ` {}.entry({})` " ,
162165 snippet( self . cx, self . map. span, "map" ) ,
163166 snippet( self . cx, params[ 1 ] . span, ".." ) ) ;
164167
165- db. span_suggestion (
168+ db. span_label (
166169 self . span,
167- "consider using" ,
168- help,
169- Applicability :: MachineApplicable , // snippet
170+ & help,
170171 ) ;
171172 }
172173 } ) ;
0 commit comments