Skip to content

Commit

Permalink
fn_to_numeric_cast_any: Do not warn cast to raw pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 30, 2025
1 parent ad05bc0 commit 362e166
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 22 deletions.
6 changes: 0 additions & 6 deletions clippy_lints/src/casts/fn_to_numeric_cast_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ use rustc_middle::ty::{self, Ty};
use super::FN_TO_NUMERIC_CAST_ANY;

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
// We allow casts from any function type to any function type.
match cast_to.kind() {
ty::FnDef(..) | ty::FnPtr(..) => return,
_ => { /* continue to checks */ },
}

if let ty::FnDef(..) | ty::FnPtr(..) = cast_from.kind() {
let mut applicability = Applicability::MaybeIncorrect;
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
cast_slice_from_raw_parts::check(cx, expr, cast_from_expr, cast_to, &self.msrv);
ptr_cast_constness::check(cx, expr, cast_from_expr, cast_from, cast_to, &self.msrv);
as_ptr_cast_mut::check(cx, expr, cast_from_expr, cast_to);
fn_to_numeric_cast_any::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_from_expr, cast_from, cast_to);
zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir);

if cast_to.is_numeric() {
Expand All @@ -831,6 +828,9 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
}
cast_lossless::check(cx, expr, cast_from_expr, cast_from, cast_to, cast_to_hir, &self.msrv);
cast_enum_constructor::check(cx, expr, cast_from_expr, cast_from);
fn_to_numeric_cast_any::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_from_expr, cast_from, cast_to);
}

as_underscore::check(cx, expr, cast_to_hir);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fn_to_numeric_cast_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn closure_to_fn_to_integer() {

fn fn_to_raw_ptr() {
let _ = foo as *const ();
//~^ ERROR: casting function pointer `foo` to `*const ()`
let _ = foo as *mut ();
}

fn cast_fn_to_self() {
Expand Down
13 changes: 1 addition & 12 deletions tests/ui/fn_to_numeric_cast_any.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,5 @@ help: did you mean to invoke the function?
LL | let _ = (clos as fn(u32) -> u32)() as usize;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: casting function pointer `foo` to `*const ()`
--> tests/ui/fn_to_numeric_cast_any.rs:74:13
|
LL | let _ = foo as *const ();
| ^^^^^^^^^^^^^^^^
|
help: did you mean to invoke the function?
|
LL | let _ = foo() as *const ();
| ~~~~~~~~~~~~~~~~~~

error: aborting due to 17 previous errors
error: aborting due to 16 previous errors

0 comments on commit 362e166

Please sign in to comment.