|
| 1 | +mod as_pointer_underscore; |
1 | 2 | mod as_ptr_cast_mut; |
2 | 3 | mod as_underscore; |
3 | 4 | mod borrow_as_ptr; |
@@ -717,6 +718,33 @@ declare_clippy_lint! { |
717 | 718 | "using `as` to cast a reference to pointer" |
718 | 719 | } |
719 | 720 |
|
| 721 | +declare_clippy_lint! { |
| 722 | + /// ### What it does |
| 723 | + /// Checks for the usage of `as *const _` or `as *mut _` conversion using inferred type. |
| 724 | + /// |
| 725 | + /// ### Why restrict this? |
| 726 | + /// The conversion might include a dangerous cast that might go undetected due to the type being inferred. |
| 727 | + /// |
| 728 | + /// ### Example |
| 729 | + /// ```no_run |
| 730 | + /// fn as_usize<T>(t: &T) -> usize { |
| 731 | + /// // BUG: `t` is already a reference, so we will here |
| 732 | + /// // return a dangling pointer to a temporary value instead |
| 733 | + /// &t as *const _ as usize |
| 734 | + /// } |
| 735 | + /// ``` |
| 736 | + /// Use instead: |
| 737 | + /// ```no_run |
| 738 | + /// fn as_usize<T>(t: &T) -> usize { |
| 739 | + /// t as *const T as usize |
| 740 | + /// } |
| 741 | + /// ``` |
| 742 | + #[clippy::version = "1.81.0"] |
| 743 | + pub AS_POINTER_UNDERSCORE, |
| 744 | + restriction, |
| 745 | + "detects `as *mut _` and `as *const _` conversion" |
| 746 | +} |
| 747 | + |
720 | 748 | pub struct Casts { |
721 | 749 | msrv: Msrv, |
722 | 750 | } |
@@ -753,6 +781,7 @@ impl_lint_pass!(Casts => [ |
753 | 781 | CAST_NAN_TO_INT, |
754 | 782 | ZERO_PTR, |
755 | 783 | REF_AS_PTR, |
| 784 | + AS_POINTER_UNDERSCORE, |
756 | 785 | ]); |
757 | 786 |
|
758 | 787 | impl<'tcx> LateLintPass<'tcx> for Casts { |
@@ -795,6 +824,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts { |
795 | 824 | } |
796 | 825 |
|
797 | 826 | as_underscore::check(cx, expr, cast_to_hir); |
| 827 | + as_pointer_underscore::check(cx, cast_to, cast_to_hir); |
798 | 828 |
|
799 | 829 | if self.msrv.meets(msrvs::PTR_FROM_REF) { |
800 | 830 | ref_as_ptr::check(cx, expr, cast_expr, cast_to_hir); |
|
0 commit comments