@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22use clippy_utils:: source:: snippet_with_applicability;
33use clippy_utils:: sugg:: Sugg ;
44use 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 } ;
66use if_chain:: if_chain;
77use rustc_errors:: Applicability ;
88use 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
113113
114114 let first = Sugg :: hir_with_applicability ( cx, e1, ".." , & mut applicability) ;
115115 let second = Sugg :: hir_with_applicability ( cx, e2, ".." , & mut applicability) ;
116+ let Some ( sugg) = std_or_core ( cx) else { return } ;
117+
116118 span_lint_and_then (
117119 cx,
118120 MANUAL_SWAP ,
@@ -122,11 +124,11 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
122124 diag. span_suggestion (
123125 span,
124126 "try" ,
125- format ! ( "std ::mem::swap({}, {})" , first. mut_addr( ) , second. mut_addr( ) ) ,
127+ format ! ( "{} ::mem::swap({}, {})" , sugg , first. mut_addr( ) , second. mut_addr( ) ) ,
126128 applicability,
127129 ) ;
128130 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 ) ) ;
130132 }
131133 } ,
132134 ) ;
@@ -187,26 +189,30 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
187189 } ;
188190
189191 let span = first. span. to( second. span) ;
192+ let Some ( sugg) = std_or_core( cx) else { return } ;
190193
191194 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+ } ) ;
210216 }
211217 }
212218 }
0 commit comments