@@ -2,7 +2,8 @@ use rustc::hir::*;
2
2
use rustc:: lint:: * ;
3
3
use rustc:: ty;
4
4
use syntax:: codemap:: mk_sp;
5
- use utils:: { differing_macro_contexts, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty, SpanlessEq } ;
5
+ use utils:: { differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq } ;
6
+ use utils:: sugg:: Sugg ;
6
7
7
8
/// **What it does:** This lints manual swapping.
8
9
///
@@ -100,17 +101,17 @@ fn check_manual_swap(cx: &LateContext, block: &Block) {
100
101
}
101
102
102
103
let ( replace, what, sugg) = if let Some ( ( slice, idx1, idx2) ) = check_for_slice( cx, lhs1, lhs2) {
103
- if let Some ( slice) = snippet_opt ( cx, slice. span ) {
104
+ if let Some ( slice) = Sugg :: hir_opt ( cx, slice) {
104
105
( false ,
105
106
format!( " elements of `{}`" , slice) ,
106
- format!( "{}.swap({}, {})" , slice, snippet( cx, idx1. span, ".." ) , snippet( cx, idx2. span, ".." ) ) )
107
+ format!( "{}.swap({}, {})" , slice. maybe_par ( ) , snippet( cx, idx1. span, ".." ) , snippet( cx, idx2. span, ".." ) ) )
107
108
} else {
108
109
( false , "" . to_owned( ) , "" . to_owned( ) )
109
110
}
110
111
} else {
111
- if let ( Some ( first) , Some ( second) ) = ( snippet_opt ( cx, lhs1. span ) , snippet_opt ( cx, rhs1. span ) ) {
112
+ if let ( Some ( first) , Some ( second) ) = ( Sugg :: hir_opt ( cx, lhs1) , Sugg :: hir_opt ( cx, rhs1) ) {
112
113
( true , format!( " `{}` and `{}`" , first, second) ,
113
- format!( "std::mem::swap(&mut {}, &mut {})" , first, second) )
114
+ format!( "std::mem::swap({}, {})" , first. mut_addr ( ) , second. mut_addr ( ) ) )
114
115
} else {
115
116
( true , "" . to_owned( ) , "" . to_owned( ) )
116
117
}
@@ -147,8 +148,8 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) {
147
148
SpanlessEq :: new( cx) . ignore_fn( ) . eq_expr( lhs0, rhs1) ,
148
149
SpanlessEq :: new( cx) . ignore_fn( ) . eq_expr( lhs1, rhs0)
149
150
] , {
150
- let ( what, lhs, rhs) = if let ( Some ( first) , Some ( second) ) = ( snippet_opt ( cx, lhs0. span ) , snippet_opt ( cx, rhs0. span ) ) {
151
- ( format!( " `{}` and `{}`" , first, second) , first, second)
151
+ let ( what, lhs, rhs) = if let ( Some ( first) , Some ( second) ) = ( Sugg :: hir_opt ( cx, lhs0) , Sugg :: hir_opt ( cx, rhs0) ) {
152
+ ( format!( " `{}` and `{}`" , first, second) , first. mut_addr ( ) . to_string ( ) , second. mut_addr ( ) . to_string ( ) )
152
153
} else {
153
154
( "" . to_owned( ) , "" . to_owned( ) , "" . to_owned( ) )
154
155
} ;
@@ -162,7 +163,7 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) {
162
163
|db| {
163
164
if !what. is_empty( ) {
164
165
db. span_suggestion( span, "try" ,
165
- format!( "std::mem::swap(&mut {}, &mut {})" , lhs, rhs) ) ;
166
+ format!( "std::mem::swap({}, {})" , lhs, rhs) ) ;
166
167
db. note( "or maybe you should use `std::mem::replace`?" ) ;
167
168
}
168
169
} ) ;
0 commit comments