@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lin
33use clippy_utils:: source:: { snippet, snippet_with_applicability} ;
44use clippy_utils:: sugg:: Sugg ;
55use clippy_utils:: ty:: is_non_aggregate_primitive_type;
6- use clippy_utils:: { is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators} ;
6+ use clippy_utils:: { is_default_equivalent, is_no_std_crate , is_res_lang_ctor, path_res, peel_ref_operators} ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: LangItem :: OptionNone ;
99use rustc_hir:: { Expr , ExprKind } ;
@@ -123,6 +123,10 @@ fn check_replace_option_with_none(cx: &LateContext<'_>, dest: &Expr<'_>, expr_sp
123123 ) ;
124124}
125125
126+ fn get_top_crate ( cx : & LateContext < ' _ > ) -> & ' static str {
127+ if is_no_std_crate ( cx) { "core" } else { "std" }
128+ }
129+
126130fn check_replace_with_uninit ( cx : & LateContext < ' _ > , src : & Expr < ' _ > , dest : & Expr < ' _ > , expr_span : Span ) {
127131 if let Some ( method_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( src. hir_id )
128132 // check if replacement is mem::MaybeUninit::uninit().assume_init()
@@ -136,7 +140,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
136140 "replacing with `mem::MaybeUninit::uninit().assume_init()`" ,
137141 "consider using" ,
138142 format ! (
139- "std::ptr::read({})" ,
143+ "{}::ptr::read({})" ,
144+ get_top_crate( cx) ,
140145 snippet_with_applicability( cx, dest. span, "" , & mut applicability)
141146 ) ,
142147 applicability,
@@ -157,7 +162,8 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
157162 "replacing with `mem::uninitialized()`" ,
158163 "consider using" ,
159164 format ! (
160- "std::ptr::read({})" ,
165+ "{}::ptr::read({})" ,
166+ get_top_crate( cx) ,
161167 snippet_with_applicability( cx, dest. span, "" , & mut applicability)
162168 ) ,
163169 applicability,
@@ -184,14 +190,17 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
184190 return ;
185191 }
186192 if is_default_equivalent ( cx, src) && !in_external_macro ( cx. tcx . sess , expr_span) {
193+ let top_crate = get_top_crate ( cx) ;
187194 span_lint_and_then (
188195 cx,
189196 MEM_REPLACE_WITH_DEFAULT ,
190197 expr_span,
191- "replacing a value of type `T` with `T::default()` is better expressed using `std::mem::take`" ,
198+ & format ! (
199+ "replacing a value of type `T` with `T::default()` is better expressed using `{top_crate}::mem::take`"
200+ ) ,
192201 |diag| {
193202 if !expr_span. from_expansion ( ) {
194- let suggestion = format ! ( "std ::mem::take({})" , snippet( cx, dest. span, "" ) ) ;
203+ let suggestion = format ! ( "{top_crate} ::mem::take({})" , snippet( cx, dest. span, "" ) ) ;
195204
196205 diag. span_suggestion (
197206 expr_span,
0 commit comments