Skip to content

Commit 2a1a2b1

Browse files
committed
new lint for as *const _ and as *mut _ casting
1 parent 86d348d commit 2a1a2b1

File tree

7 files changed

+107
-0
lines changed

7 files changed

+107
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,6 +5193,7 @@ Released 2018-09-13
51935193
<!-- begin autogenerated links to lint list -->
51945194
[`absolute_paths`]: https://rust-lang.github.io/rust-clippy/master/index.html#absolute_paths
51955195
[`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons
5196+
[`accidental_double_ref_into_flat_addr_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#accidental_double_ref_into_flat_addr_cast
51965197
[`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core
51975198
[`allow_attributes`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes
51985199
[`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use rustc_errors::Applicability;
2+
use rustc_lint::LateContext;
3+
use rustc_middle::ty::Ty;
4+
5+
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
6+
if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind
7+
&& matches!(ty.kind, rustc_hir::TyKind::Infer)
8+
{
9+
clippy_utils::diagnostics::span_lint_and_then(
10+
cx,
11+
super::AS_POINTER_UNDERSCORE,
12+
cast_to_hir.span,
13+
"using inferred pointer cast",
14+
|diag| {
15+
diag.span_suggestion(
16+
cast_to_hir.span,
17+
"use explicit type",
18+
ty_into,
19+
Applicability::MachineApplicable,
20+
);
21+
},
22+
);
23+
}
24+
}

clippy_lints/src/casts/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod as_pointer_underscore;
12
mod as_ptr_cast_mut;
23
mod as_underscore;
34
mod borrow_as_ptr;
@@ -717,6 +718,33 @@ declare_clippy_lint! {
717718
"using `as` to cast a reference to pointer"
718719
}
719720

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+
720748
pub struct Casts {
721749
msrv: Msrv,
722750
}
@@ -753,6 +781,7 @@ impl_lint_pass!(Casts => [
753781
CAST_NAN_TO_INT,
754782
ZERO_PTR,
755783
REF_AS_PTR,
784+
AS_POINTER_UNDERSCORE,
756785
]);
757786

758787
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -795,6 +824,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
795824
}
796825

797826
as_underscore::check(cx, expr, cast_to_hir);
827+
as_pointer_underscore::check(cx, cast_to, cast_to_hir);
798828

799829
if self.msrv.meets(msrvs::PTR_FROM_REF) {
800830
ref_as_ptr::check(cx, expr, cast_expr, cast_to_hir);

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
7878
crate::cargo::NEGATIVE_FEATURE_NAMES_INFO,
7979
crate::cargo::REDUNDANT_FEATURE_NAMES_INFO,
8080
crate::cargo::WILDCARD_DEPENDENCIES_INFO,
81+
crate::casts::AS_POINTER_UNDERSCORE_INFO,
8182
crate::casts::AS_PTR_CAST_MUT_INFO,
8283
crate::casts::AS_UNDERSCORE_INFO,
8384
crate::casts::BORROW_AS_PTR_INFO,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(clippy::as_pointer_underscore)]
2+
#![crate_type = "lib"]
3+
#![no_std]
4+
5+
struct S;
6+
7+
fn f(s: &S) -> usize {
8+
&s as *const &S as usize
9+
//~^ ERROR: using inferred pointer cast
10+
//FIXME: idk how to get this help working ~| HELP: use explicit type: `*const &S`
11+
}
12+
13+
fn g(s: &mut S) -> usize {
14+
s as *mut S as usize
15+
//~^ ERROR: using inferred pointer cast
16+
//FIXME: idk how to get this help working ~| HELP: use explicit type: `*mut S`
17+
}

tests/ui/as_pointer_underscore.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(clippy::as_pointer_underscore)]
2+
#![crate_type = "lib"]
3+
#![no_std]
4+
5+
struct S;
6+
7+
fn f(s: &S) -> usize {
8+
&s as *const _ as usize
9+
//~^ ERROR: using inferred pointer cast
10+
//FIXME: idk how to get this help working ~| HELP: use explicit type: `*const &S`
11+
}
12+
13+
fn g(s: &mut S) -> usize {
14+
s as *mut _ as usize
15+
//~^ ERROR: using inferred pointer cast
16+
//FIXME: idk how to get this help working ~| HELP: use explicit type: `*mut S`
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: using inferred pointer cast
2+
--> tests/ui/as_pointer_underscore.rs:8:11
3+
|
4+
LL | &s as *const _ as usize
5+
| ^^^^^^^^ help: use explicit type: `*const &S`
6+
|
7+
= note: `-D clippy::as-pointer-underscore` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::as_pointer_underscore)]`
9+
10+
error: using inferred pointer cast
11+
--> tests/ui/as_pointer_underscore.rs:14:10
12+
|
13+
LL | s as *mut _ as usize
14+
| ^^^^^^ help: use explicit type: `*mut S`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)