@@ -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
+ if let Some ( ty_snip) = snippet_opt( cx, ty. span) {
634
+ let sugg = match mut_ty. ty. kind {
635
+ TyKind :: Infer => String :: from( sugg_fn) + "()" ,
636
+ _ => format!( "{}::<{}>()" , sugg_fn, ty_snip) ,
637
+ } ;
638
+ span_lint_and_sugg( cx, ZERO_PTR , span, msg, "try" , sugg, Applicability :: MachineApplicable )
639
+ } else {
640
+ // `MaybeIncorrect` as type inference may not work with the suggested code
641
+ span_lint_and_sugg( cx, ZERO_PTR , span, msg, "try" , String :: from( sugg_fn) + "()" , Applicability :: MaybeIncorrect )
642
+ }
635
643
}
636
644
}
637
645
}
0 commit comments