@@ -1637,12 +1637,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
1637
1637
return ;
1638
1638
}
1639
1639
if let ExprKind :: Cast ( ref ex, cast_to) = expr. kind {
1640
- if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = cast_to. kind {
1641
- if let Res :: Def ( _, def_id) = path. res {
1642
- if cx. tcx . has_attr ( def_id, sym:: cfg) || cx. tcx . has_attr ( def_id, sym:: cfg_attr) {
1643
- return ;
1644
- }
1645
- }
1640
+ if is_hir_ty_cfg_dependant ( cx, cast_to) {
1641
+ return ;
1646
1642
}
1647
1643
let ( cast_from, cast_to) = ( cx. typeck_results ( ) . expr_ty ( ex) , cx. typeck_results ( ) . expr_ty ( expr) ) ;
1648
1644
lint_fn_to_numeric_cast ( cx, expr, ex, cast_from, cast_to) ;
@@ -1691,6 +1687,21 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
1691
1687
lint_numeric_casts ( cx, expr, ex, cast_from, cast_to) ;
1692
1688
}
1693
1689
1690
+ lint_cast_ptr_alignment ( cx, expr, cast_from, cast_to) ;
1691
+ } else if let ExprKind :: MethodCall ( method_path, _, args, _) = expr. kind {
1692
+ if method_path. ident . name != sym ! ( cast) {
1693
+ return ;
1694
+ }
1695
+ if_chain ! {
1696
+ if let Some ( generic_args) = method_path. args;
1697
+ if let [ GenericArg :: Type ( cast_to) ] = generic_args. args;
1698
+ // There probably is no obvious reason to do this, just to be consistent with `as` cases.
1699
+ if is_hir_ty_cfg_dependant( cx, cast_to) ;
1700
+ then {
1701
+ return ;
1702
+ }
1703
+ }
1704
+ let ( cast_from, cast_to) = ( cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) , cx. typeck_results ( ) . expr_ty ( expr) ) ;
1694
1705
lint_cast_ptr_alignment ( cx, expr, cast_from, cast_to) ;
1695
1706
}
1696
1707
}
@@ -1714,6 +1725,18 @@ fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
1714
1725
}
1715
1726
}
1716
1727
1728
+ fn is_hir_ty_cfg_dependant ( cx : & LateContext < ' _ > , ty : & hir:: Ty < ' _ > ) -> bool {
1729
+ if_chain ! {
1730
+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = ty. kind;
1731
+ if let Res :: Def ( _, def_id) = path. res;
1732
+ then {
1733
+ cx. tcx. has_attr( def_id, sym:: cfg) || cx. tcx. has_attr( def_id, sym:: cfg_attr)
1734
+ } else {
1735
+ false
1736
+ }
1737
+ }
1738
+ }
1739
+
1717
1740
fn show_unnecessary_cast ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , literal_str : & str , cast_from : Ty < ' _ > , cast_to : Ty < ' _ > ) {
1718
1741
let literal_kind_name = if cast_from. is_integral ( ) { "integer" } else { "float" } ;
1719
1742
span_lint_and_sugg (
0 commit comments