Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5380,6 +5380,7 @@ Released 2018-09-13
[`arc_with_non_send_sync`]: https://rust-lang.github.io/rust-clippy/master/index.html#arc_with_non_send_sync
[`arithmetic_side_effects`]: https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
[`as_conversions`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_conversions
[`as_pointer_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_pointer_underscore
[`as_ptr_cast_mut`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_ptr_cast_mut
[`as_underscore`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_underscore
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
Expand Down
19 changes: 19 additions & 0 deletions clippy_lints/src/casts/as_pointer_underscore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rustc_errors::Applicability;
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;

pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind
&& matches!(ty.kind, rustc_hir::TyKind::Infer)
{
clippy_utils::diagnostics::span_lint_and_sugg(
cx,
super::AS_POINTER_UNDERSCORE,
cast_to_hir.span,
"using inferred pointer cast",
"use explicit type",
ty_into.to_string(),
Applicability::MachineApplicable,
);
}
}
30 changes: 30 additions & 0 deletions clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod as_pointer_underscore;
mod as_ptr_cast_mut;
mod as_underscore;
mod borrow_as_ptr;
Expand Down Expand Up @@ -726,6 +727,33 @@ declare_clippy_lint! {
"using `as` to cast a reference to pointer"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for the usage of `as *const _` or `as *mut _` conversion using inferred type.
///
/// ### Why restrict this?
/// The conversion might include a dangerous cast that might go undetected due to the type being inferred.
///
/// ### Example
/// ```no_run
/// fn as_usize<T>(t: &T) -> usize {
/// // BUG: `t` is already a reference, so we will here
/// // return a dangling pointer to a temporary value instead
/// &t as *const _ as usize
/// }
/// ```
/// Use instead:
/// ```no_run
/// fn as_usize<T>(t: &T) -> usize {
/// t as *const T as usize
/// }
/// ```
#[clippy::version = "1.81.0"]
pub AS_POINTER_UNDERSCORE,
restriction,
"detects `as *mut _` and `as *const _` conversion"
}

pub struct Casts {
msrv: Msrv,
}
Expand Down Expand Up @@ -763,6 +791,7 @@ impl_lint_pass!(Casts => [
CAST_NAN_TO_INT,
ZERO_PTR,
REF_AS_PTR,
AS_POINTER_UNDERSCORE,
]);

impl<'tcx> LateLintPass<'tcx> for Casts {
Expand Down Expand Up @@ -805,6 +834,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
}

as_underscore::check(cx, expr, cast_to_hir);
as_pointer_underscore::check(cx, cast_to, cast_to_hir);

let was_borrow_as_ptr_emitted = if self.msrv.meets(msrvs::BORROW_AS_PTR) {
borrow_as_ptr::check(cx, expr, cast_from_expr, cast_to_hir, &self.msrv)
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::cargo::NEGATIVE_FEATURE_NAMES_INFO,
crate::cargo::REDUNDANT_FEATURE_NAMES_INFO,
crate::cargo::WILDCARD_DEPENDENCIES_INFO,
crate::casts::AS_POINTER_UNDERSCORE_INFO,
crate::casts::AS_PTR_CAST_MUT_INFO,
crate::casts::AS_UNDERSCORE_INFO,
crate::casts::BORROW_AS_PTR_INFO,
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/as_pointer_underscore.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![warn(clippy::as_pointer_underscore)]
#![crate_type = "lib"]
#![no_std]

struct S;

fn f(s: &S) -> usize {
&s as *const &S as usize
//~^ ERROR: using inferred pointer cast
}

fn g(s: &mut S) -> usize {
s as *mut S as usize
//~^ ERROR: using inferred pointer cast
}
15 changes: 15 additions & 0 deletions tests/ui/as_pointer_underscore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![warn(clippy::as_pointer_underscore)]
#![crate_type = "lib"]
#![no_std]

struct S;

fn f(s: &S) -> usize {
&s as *const _ as usize
//~^ ERROR: using inferred pointer cast
}

fn g(s: &mut S) -> usize {
s as *mut _ as usize
//~^ ERROR: using inferred pointer cast
}
17 changes: 17 additions & 0 deletions tests/ui/as_pointer_underscore.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: using inferred pointer cast
--> tests/ui/as_pointer_underscore.rs:8:11
|
LL | &s as *const _ as usize
| ^^^^^^^^ help: use explicit type: `*const &S`
|
= note: `-D clippy::as-pointer-underscore` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::as_pointer_underscore)]`

error: using inferred pointer cast
--> tests/ui/as_pointer_underscore.rs:13:10
|
LL | s as *mut _ as usize
| ^^^^^^ help: use explicit type: `*mut S`

error: aborting due to 2 previous errors

Loading