@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2
2
use clippy_utils:: source:: snippet_with_applicability;
3
3
use clippy_utils:: sugg:: Sugg ;
4
4
use clippy_utils:: ty:: is_type_diagnostic_item;
5
- use clippy_utils:: { can_mut_borrow_both, differing_macro_contexts, eq_expr_value} ;
5
+ use clippy_utils:: { can_mut_borrow_both, differing_macro_contexts, eq_expr_value, std_or_core } ;
6
6
use if_chain:: if_chain;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , PatKind , QPath , Stmt , StmtKind } ;
@@ -113,6 +113,8 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
113
113
114
114
let first = Sugg :: hir_with_applicability ( cx, e1, ".." , & mut applicability) ;
115
115
let second = Sugg :: hir_with_applicability ( cx, e2, ".." , & mut applicability) ;
116
+ let Some ( sugg) = std_or_core ( cx) else { return } ;
117
+
116
118
span_lint_and_then (
117
119
cx,
118
120
MANUAL_SWAP ,
@@ -122,11 +124,11 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
122
124
diag. span_suggestion (
123
125
span,
124
126
"try" ,
125
- format ! ( "std ::mem::swap({}, {})" , first. mut_addr( ) , second. mut_addr( ) ) ,
127
+ format ! ( "{} ::mem::swap({}, {})" , sugg , first. mut_addr( ) , second. mut_addr( ) ) ,
126
128
applicability,
127
129
) ;
128
130
if !is_xor_based {
129
- diag. note ( "or maybe you should use `std ::mem::replace`?" ) ;
131
+ diag. note ( & format ! ( "or maybe you should use `{} ::mem::replace`?" , sugg ) ) ;
130
132
}
131
133
} ,
132
134
) ;
@@ -187,26 +189,30 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
187
189
} ;
188
190
189
191
let span = first. span. to( second. span) ;
192
+ let Some ( sugg) = std_or_core( cx) else { return } ;
190
193
191
194
span_lint_and_then( cx,
192
- ALMOST_SWAPPED ,
193
- span,
194
- & format!( "this looks like you are trying to swap{}" , what) ,
195
- |diag| {
196
- if !what. is_empty( ) {
197
- diag. span_suggestion(
198
- span,
199
- "try" ,
200
- format!(
201
- "std::mem::swap({}, {})" ,
202
- lhs,
203
- rhs,
204
- ) ,
205
- Applicability :: MaybeIncorrect ,
206
- ) ;
207
- diag. note( "or maybe you should use `std::mem::replace`?" ) ;
208
- }
209
- } ) ;
195
+ ALMOST_SWAPPED ,
196
+ span,
197
+ & format!( "this looks like you are trying to swap{}" , what) ,
198
+ |diag| {
199
+ if !what. is_empty( ) {
200
+ diag. span_suggestion(
201
+ span,
202
+ "try" ,
203
+ format!(
204
+ "{}::mem::swap({}, {})" ,
205
+ sugg,
206
+ lhs,
207
+ rhs,
208
+ ) ,
209
+ Applicability :: MaybeIncorrect ,
210
+ ) ;
211
+ diag. note(
212
+ & format!( "or maybe you should use `{}::mem::replace`?" , sugg)
213
+ ) ;
214
+ }
215
+ } ) ;
210
216
}
211
217
}
212
218
}
0 commit comments