@@ -9,12 +9,14 @@ use rustc_hir::{
9
9
} ;
10
10
use rustc_lint:: { LateContext , LateLintPass } ;
11
11
use rustc_middle:: lint:: in_external_macro;
12
- use rustc_session:: { declare_lint_pass , declare_tool_lint } ;
12
+ use rustc_session:: { declare_tool_lint , impl_lint_pass } ;
13
13
use rustc_span:: hygiene:: DesugaringKind ;
14
14
use rustc_span:: source_map:: { ExpnKind , Span } ;
15
15
16
16
use clippy_utils:: sugg:: Sugg ;
17
- use clippy_utils:: { get_parent_expr, in_constant, is_integer_literal, iter_input_pats, last_path_segment, SpanlessEq } ;
17
+ use clippy_utils:: {
18
+ get_parent_expr, in_constant, is_integer_literal, is_no_std_crate, iter_input_pats, last_path_segment, SpanlessEq ,
19
+ } ;
18
20
19
21
declare_clippy_lint ! {
20
22
/// ### What it does
@@ -120,14 +122,28 @@ declare_clippy_lint! {
120
122
"using `0 as *{const, mut} T`"
121
123
}
122
124
123
- declare_lint_pass ! ( MiscLints => [
125
+ pub struct LintPass {
126
+ std_or_core : & ' static str ,
127
+ }
128
+ impl Default for LintPass {
129
+ fn default ( ) -> Self {
130
+ Self { std_or_core : "std" }
131
+ }
132
+ }
133
+ impl_lint_pass ! ( LintPass => [
124
134
TOPLEVEL_REF_ARG ,
125
135
USED_UNDERSCORE_BINDING ,
126
136
SHORT_CIRCUIT_STATEMENT ,
127
137
ZERO_PTR ,
128
138
] ) ;
129
139
130
- impl < ' tcx > LateLintPass < ' tcx > for MiscLints {
140
+ impl < ' tcx > LateLintPass < ' tcx > for LintPass {
141
+ fn check_crate ( & mut self , cx : & LateContext < ' _ > ) {
142
+ if is_no_std_crate ( cx) {
143
+ self . std_or_core = "core" ;
144
+ }
145
+ }
146
+
131
147
fn check_fn (
132
148
& mut self ,
133
149
cx : & LateContext < ' tcx > ,
@@ -231,7 +247,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
231
247
232
248
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
233
249
if let ExprKind :: Cast ( e, ty) = expr. kind {
234
- check_cast ( cx, expr. span , e, ty) ;
250
+ self . check_cast ( cx, expr. span , e, ty) ;
235
251
return ;
236
252
}
237
253
if in_attributes_expansion ( expr) || expr. span . is_desugaring ( DesugaringKind :: Await ) {
@@ -310,26 +326,28 @@ fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {
310
326
}
311
327
}
312
328
313
- fn check_cast ( cx : & LateContext < ' _ > , span : Span , e : & Expr < ' _ > , ty : & hir:: Ty < ' _ > ) {
314
- if_chain ! {
315
- if let TyKind :: Ptr ( ref mut_ty) = ty. kind;
316
- if is_integer_literal( e, 0 ) ;
317
- if !in_constant( cx, e. hir_id) ;
318
- then {
319
- let ( msg, sugg_fn) = match mut_ty. mutbl {
320
- Mutability :: Mut => ( "`0 as *mut _` detected" , "std::ptr::null_mut" ) ,
321
- Mutability :: Not => ( "`0 as *const _` detected" , "std::ptr::null" ) ,
322
- } ;
329
+ impl LintPass {
330
+ fn check_cast ( & self , cx : & LateContext < ' _ > , span : Span , e : & Expr < ' _ > , ty : & hir:: Ty < ' _ > ) {
331
+ if_chain ! {
332
+ if let TyKind :: Ptr ( ref mut_ty) = ty. kind;
333
+ if is_integer_literal( e, 0 ) ;
334
+ if !in_constant( cx, e. hir_id) ;
335
+ then {
336
+ let ( msg, sugg_fn) = match mut_ty. mutbl {
337
+ Mutability :: Mut => ( "`0 as *mut _` detected" , "ptr::null_mut" ) ,
338
+ Mutability :: Not => ( "`0 as *const _` detected" , "ptr::null" ) ,
339
+ } ;
323
340
324
- let ( sugg, appl) = if let TyKind :: Infer = mut_ty. ty. kind {
325
- ( format!( "{sugg_fn}()" ) , Applicability :: MachineApplicable )
326
- } else if let Some ( mut_ty_snip) = snippet_opt( cx, mut_ty. ty. span) {
327
- ( format!( "{sugg_fn}::<{mut_ty_snip}>()" ) , Applicability :: MachineApplicable )
328
- } else {
329
- // `MaybeIncorrect` as type inference may not work with the suggested code
330
- ( format!( "{sugg_fn}()" ) , Applicability :: MaybeIncorrect )
331
- } ;
332
- span_lint_and_sugg( cx, ZERO_PTR , span, msg, "try" , sugg, appl) ;
341
+ let ( sugg, appl) = if let TyKind :: Infer = mut_ty. ty. kind {
342
+ ( format!( "{}::{sugg_fn}()" , self . std_or_core) , Applicability :: MachineApplicable )
343
+ } else if let Some ( mut_ty_snip) = snippet_opt( cx, mut_ty. ty. span) {
344
+ ( format!( "{}::{sugg_fn}::<{mut_ty_snip}>()" , self . std_or_core) , Applicability :: MachineApplicable )
345
+ } else {
346
+ // `MaybeIncorrect` as type inference may not work with the suggested code
347
+ ( format!( "{}::{sugg_fn}()" , self . std_or_core) , Applicability :: MaybeIncorrect )
348
+ } ;
349
+ span_lint_and_sugg( cx, ZERO_PTR , span, msg, "try" , sugg, appl) ;
350
+ }
333
351
}
334
352
}
335
353
}
0 commit comments