@@ -13,8 +13,8 @@ use crate::consts::{constant, Constant};
13
13
use crate :: utils:: sugg:: Sugg ;
14
14
use crate :: utils:: {
15
15
get_item_name, get_parent_expr, implements_trait, in_constant, is_integer_const, iter_input_pats,
16
- last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then ,
17
- span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq ,
16
+ last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt , span_lint, span_lint_and_sugg ,
17
+ span_lint_and_then , span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq ,
18
18
} ;
19
19
20
20
declare_clippy_lint ! {
@@ -621,17 +621,25 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
621
621
622
622
fn check_cast ( cx : & LateContext < ' _ , ' _ > , span : Span , e : & Expr , ty : & Ty ) {
623
623
if_chain ! {
624
- if let TyKind :: Ptr ( MutTy { mutbl , .. } ) = ty. kind;
624
+ if let TyKind :: Ptr ( ref mut_ty ) = ty. kind;
625
625
if let ExprKind :: Lit ( ref lit) = e. kind;
626
- if let LitKind :: Int ( value, ..) = lit. node;
627
- if value == 0 ;
626
+ if let LitKind :: Int ( 0 , _) = lit. node;
628
627
if !in_constant( cx, e. hir_id) ;
629
628
then {
630
- let msg = match mutbl {
631
- Mutability :: MutMutable => "`0 as *mut _` detected. Consider using ` ptr::null_mut()`" ,
632
- Mutability :: MutImmutable => "`0 as *const _` detected. Consider using ` ptr::null()`" ,
629
+ let ( msg, sugg_fn ) = match mut_ty . mutbl {
630
+ Mutability :: MutMutable => ( "`0 as *mut _` detected" , "std:: ptr::null_mut" ) ,
631
+ Mutability :: MutImmutable => ( "`0 as *const _` detected" , "std:: ptr::null" ) ,
633
632
} ;
634
- span_lint( cx, ZERO_PTR , span, msg) ;
633
+
634
+ let ( sugg, appl) = if let TyKind :: Infer = mut_ty. ty. kind {
635
+ ( format!( "{}()" , sugg_fn) , Applicability :: MachineApplicable )
636
+ } else if let Some ( mut_ty_snip) = snippet_opt( cx, mut_ty. ty. span) {
637
+ ( format!( "{}::<{}>()" , sugg_fn, mut_ty_snip) , Applicability :: MachineApplicable )
638
+ } else {
639
+ // `MaybeIncorrect` as type inference may not work with the suggested code
640
+ ( format!( "{}()" , sugg_fn) , Applicability :: MaybeIncorrect )
641
+ } ;
642
+ span_lint_and_sugg( cx, ZERO_PTR , span, msg, "try" , sugg, appl) ;
635
643
}
636
644
}
637
645
}
0 commit comments