@@ -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_no_std_crate , is_res_lang_ctor, path_res, peel_ref_operators} ;
6+ use clippy_utils:: { is_default_equivalent, is_res_lang_ctor, path_res, peel_ref_operators, std_or_core } ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: LangItem :: OptionNone ;
99use rustc_hir:: { Expr , ExprKind } ;
@@ -123,15 +123,12 @@ 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-
130126fn check_replace_with_uninit ( cx : & LateContext < ' _ > , src : & Expr < ' _ > , dest : & Expr < ' _ > , expr_span : Span ) {
131127 if let Some ( method_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( src. hir_id )
132128 // check if replacement is mem::MaybeUninit::uninit().assume_init()
133129 && cx. tcx . is_diagnostic_item ( sym:: assume_init, method_def_id)
134130 {
131+ let Some ( top_crate) = std_or_core ( cx) else { return } ;
135132 let mut applicability = Applicability :: MachineApplicable ;
136133 span_lint_and_sugg (
137134 cx,
@@ -140,8 +137,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
140137 "replacing with `mem::MaybeUninit::uninit().assume_init()`" ,
141138 "consider using" ,
142139 format ! (
143- "{}::ptr::read({})" ,
144- get_top_crate( cx) ,
140+ "{top_crate}::ptr::read({})" ,
145141 snippet_with_applicability( cx, dest. span, "" , & mut applicability)
146142 ) ,
147143 applicability,
@@ -154,6 +150,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
154150 && let Some ( repl_def_id) = cx. qpath_res ( repl_func_qpath, repl_func. hir_id ) . opt_def_id ( )
155151 {
156152 if cx. tcx . is_diagnostic_item ( sym:: mem_uninitialized, repl_def_id) {
153+ let Some ( top_crate) = std_or_core ( cx) else { return } ;
157154 let mut applicability = Applicability :: MachineApplicable ;
158155 span_lint_and_sugg (
159156 cx,
@@ -162,8 +159,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
162159 "replacing with `mem::uninitialized()`" ,
163160 "consider using" ,
164161 format ! (
165- "{}::ptr::read({})" ,
166- get_top_crate( cx) ,
162+ "{top_crate}::ptr::read({})" ,
167163 snippet_with_applicability( cx, dest. span, "" , & mut applicability)
168164 ) ,
169165 applicability,
@@ -190,7 +186,7 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
190186 return ;
191187 }
192188 if is_default_equivalent ( cx, src) && !in_external_macro ( cx. tcx . sess , expr_span) {
193- let top_crate = get_top_crate ( cx) ;
189+ let Some ( top_crate) = std_or_core ( cx) else { return } ;
194190 span_lint_and_then (
195191 cx,
196192 MEM_REPLACE_WITH_DEFAULT ,
0 commit comments