@@ -13,8 +13,8 @@ use crate::consts::{constant, Constant};
1313use crate :: utils:: sugg:: Sugg ;
1414use crate :: utils:: {
1515 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 ,
1818} ;
1919
2020declare_clippy_lint ! {
@@ -621,17 +621,25 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
621621
622622fn check_cast ( cx : & LateContext < ' _ , ' _ > , span : Span , e : & Expr , ty : & Ty ) {
623623 if_chain ! {
624- if let TyKind :: Ptr ( MutTy { mutbl , .. } ) = ty. kind;
624+ if let TyKind :: Ptr ( ref mut_ty ) = ty. kind;
625625 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;
628627 if !in_constant( cx, e. hir_id) ;
629628 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" ) ,
633632 } ;
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) ;
635643 }
636644 }
637645}
0 commit comments