|
| 1 | +mod as_pointer_underscore; |
1 | 2 | mod as_ptr_cast_mut;
|
2 | 3 | mod as_underscore;
|
3 | 4 | mod borrow_as_ptr;
|
@@ -726,6 +727,33 @@ declare_clippy_lint! {
|
726 | 727 | "using `as` to cast a reference to pointer"
|
727 | 728 | }
|
728 | 729 |
|
| 730 | +declare_clippy_lint! { |
| 731 | + /// ### What it does |
| 732 | + /// Checks for the usage of `as *const _` or `as *mut _` conversion using inferred type. |
| 733 | + /// |
| 734 | + /// ### Why restrict this? |
| 735 | + /// The conversion might include a dangerous cast that might go undetected due to the type being inferred. |
| 736 | + /// |
| 737 | + /// ### Example |
| 738 | + /// ```no_run |
| 739 | + /// fn as_usize<T>(t: &T) -> usize { |
| 740 | + /// // BUG: `t` is already a reference, so we will here |
| 741 | + /// // return a dangling pointer to a temporary value instead |
| 742 | + /// &t as *const _ as usize |
| 743 | + /// } |
| 744 | + /// ``` |
| 745 | + /// Use instead: |
| 746 | + /// ```no_run |
| 747 | + /// fn as_usize<T>(t: &T) -> usize { |
| 748 | + /// t as *const T as usize |
| 749 | + /// } |
| 750 | + /// ``` |
| 751 | + #[clippy::version = "1.81.0"] |
| 752 | + pub AS_POINTER_UNDERSCORE, |
| 753 | + restriction, |
| 754 | + "detects `as *mut _` and `as *const _` conversion" |
| 755 | +} |
| 756 | + |
729 | 757 | pub struct Casts {
|
730 | 758 | msrv: Msrv,
|
731 | 759 | }
|
@@ -763,6 +791,7 @@ impl_lint_pass!(Casts => [
|
763 | 791 | CAST_NAN_TO_INT,
|
764 | 792 | ZERO_PTR,
|
765 | 793 | REF_AS_PTR,
|
| 794 | + AS_POINTER_UNDERSCORE, |
766 | 795 | ]);
|
767 | 796 |
|
768 | 797 | impl<'tcx> LateLintPass<'tcx> for Casts {
|
@@ -805,6 +834,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
|
805 | 834 | }
|
806 | 835 |
|
807 | 836 | as_underscore::check(cx, expr, cast_to_hir);
|
| 837 | + as_pointer_underscore::check(cx, cast_to, cast_to_hir); |
808 | 838 |
|
809 | 839 | let was_borrow_as_ptr_emitted = if self.msrv.meets(msrvs::BORROW_AS_PTR) {
|
810 | 840 | borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv)
|
|
0 commit comments