@@ -11,10 +11,11 @@ use std::fmt;
11
11
use syntax:: codemap:: Span ;
12
12
use syntax:: ptr:: P ;
13
13
use utils:: { get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
14
- match_type, method_chain_args, return_ty, same_tys, snippet, snippet_opt , span_lint,
14
+ match_type, method_chain_args, return_ty, same_tys, snippet, span_lint,
15
15
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth} ;
16
16
use utils:: MethodArgs ;
17
17
use utils:: paths;
18
+ use utils:: sugg;
18
19
19
20
#[ derive( Clone ) ]
20
21
pub struct Pass ;
@@ -628,8 +629,8 @@ fn lint_clone_double_ref(cx: &LateContext, expr: &hir::Expr, arg: &hir::Expr, ty
628
629
expr. span ,
629
630
"using `clone` on a double-reference; \
630
631
this will copy the reference instead of cloning the inner type",
631
- |db| if let Some ( snip) = snippet_opt ( cx, arg. span ) {
632
- db. span_suggestion ( expr. span , "try dereferencing it" , format ! ( "(* {}).clone()" , snip) ) ;
632
+ |db| if let Some ( snip) = sugg :: Sugg :: hir_opt ( cx, arg) {
633
+ db. span_suggestion ( expr. span , "try dereferencing it" , format ! ( "({}).clone()" , snip. deref ( ) ) ) ;
633
634
} ) ;
634
635
}
635
636
}
@@ -641,14 +642,13 @@ fn lint_extend(cx: &LateContext, expr: &hir::Expr, args: &MethodArgs) {
641
642
return ;
642
643
}
643
644
let arg_ty = cx. tcx . expr_ty ( & args[ 1 ] ) ;
644
- if let Some ( ( span , r ) ) = derefs_to_slice ( cx, & args[ 1 ] , & arg_ty) {
645
+ if let Some ( slice ) = derefs_to_slice ( cx, & args[ 1 ] , & arg_ty) {
645
646
span_lint_and_then ( cx, EXTEND_FROM_SLICE , expr. span , "use of `extend` to extend a Vec by a slice" , |db| {
646
647
db. span_suggestion ( expr. span ,
647
648
"try this" ,
648
- format ! ( "{}.extend_from_slice({}{} )" ,
649
+ format ! ( "{}.extend_from_slice({})" ,
649
650
snippet( cx, args[ 0 ] . span, "_" ) ,
650
- r,
651
- snippet( cx, span, "_" ) ) ) ;
651
+ slice) ) ;
652
652
} ) ;
653
653
}
654
654
}
@@ -695,7 +695,7 @@ fn lint_iter_nth(cx: &LateContext, expr: &hir::Expr, iter_args: &MethodArgs, is_
695
695
) ;
696
696
}
697
697
698
- fn derefs_to_slice ( cx : & LateContext , expr : & hir:: Expr , ty : & ty:: Ty ) -> Option < ( Span , & ' static str ) > {
698
+ fn derefs_to_slice ( cx : & LateContext , expr : & hir:: Expr , ty : & ty:: Ty ) -> Option < sugg :: Sugg < ' static > > {
699
699
fn may_slice ( cx : & LateContext , ty : & ty:: Ty ) -> bool {
700
700
match ty. sty {
701
701
ty:: TySlice ( _) => true ,
@@ -706,19 +706,22 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: &ty::Ty) -> Option<(S
706
706
_ => false ,
707
707
}
708
708
}
709
+
709
710
if let hir:: ExprMethodCall ( name, _, ref args) = expr. node {
710
711
if & name. node . as_str ( ) == & "iter" && may_slice ( cx, & cx. tcx . expr_ty ( & args[ 0 ] ) ) {
711
- Some ( ( args[ 0 ] . span , "&" ) )
712
+ sugg:: Sugg :: hir_opt ( cx, & * args[ 0 ] ) . map ( |sugg| {
713
+ sugg. addr ( )
714
+ } )
712
715
} else {
713
716
None
714
717
}
715
718
} else {
716
719
match ty. sty {
717
- ty:: TySlice ( _) => Some ( ( expr . span , "" ) ) ,
720
+ ty:: TySlice ( _) => sugg :: Sugg :: hir_opt ( cx , expr ) ,
718
721
ty:: TyRef ( _, ty:: TypeAndMut { ty : ref inner, .. } ) |
719
722
ty:: TyBox ( ref inner) => {
720
723
if may_slice ( cx, inner) {
721
- Some ( ( expr . span , "" ) )
724
+ sugg :: Sugg :: hir_opt ( cx , expr )
722
725
} else {
723
726
None
724
727
}
0 commit comments