diff --git a/clippy_lints/src/casts/as_pointer_underscore.rs b/clippy_lints/src/casts/as_pointer_underscore.rs index 3ab6693756f5..5d5ccceecee1 100644 --- a/clippy_lints/src/casts/as_pointer_underscore.rs +++ b/clippy_lints/src/casts/as_pointer_underscore.rs @@ -1,10 +1,13 @@ use rustc_errors::Applicability; use rustc_lint::LateContext; -use rustc_middle::ty::Ty; +use rustc_middle::ty::{self, 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(())) + && let ty::RawPtr(pointee, _) = ty_into.kind() + // skip fn item type, since it can not be explicitly expressed with syntax + && !matches!(pointee.kind(), ty::FnDef(..)) { clippy_utils::diagnostics::span_lint_and_sugg( cx, diff --git a/tests/ui/as_pointer_underscore.fixed b/tests/ui/as_pointer_underscore.fixed index 93daae6bc668..5e840232ac79 100644 --- a/tests/ui/as_pointer_underscore.fixed +++ b/tests/ui/as_pointer_underscore.fixed @@ -13,3 +13,11 @@ fn g(s: &mut S) -> usize { s as *mut S as usize //~^ as_pointer_underscore } + +fn issue_15281() { + fn bar(_: usize) {} + // pointer to fn item, lint should not trigger + let _ = &bar as *const _; + let _ = &(bar as fn(usize)) as *const fn(usize); + //~^ as_pointer_underscore +} diff --git a/tests/ui/as_pointer_underscore.rs b/tests/ui/as_pointer_underscore.rs index 56f1a8935255..ddac92ac0ccb 100644 --- a/tests/ui/as_pointer_underscore.rs +++ b/tests/ui/as_pointer_underscore.rs @@ -13,3 +13,11 @@ fn g(s: &mut S) -> usize { s as *mut _ as usize //~^ as_pointer_underscore } + +fn issue_15281() { + fn bar(_: usize) {} + // pointer to fn item, lint should not trigger + let _ = &bar as *const _; + let _ = &(bar as fn(usize)) as *const _; + //~^ as_pointer_underscore +} diff --git a/tests/ui/as_pointer_underscore.stderr b/tests/ui/as_pointer_underscore.stderr index 270056f36454..c80cffe4943d 100644 --- a/tests/ui/as_pointer_underscore.stderr +++ b/tests/ui/as_pointer_underscore.stderr @@ -13,5 +13,11 @@ error: using inferred pointer cast LL | s as *mut _ as usize | ^^^^^^ help: use explicit type: `*mut S` -error: aborting due to 2 previous errors +error: using inferred pointer cast + --> tests/ui/as_pointer_underscore.rs:21:36 + | +LL | let _ = &(bar as fn(usize)) as *const _; + | ^^^^^^^^ help: use explicit type: `*const fn(usize)` + +error: aborting due to 3 previous errors