Skip to content

as_pointer_underscore: don't lint when casting fn item pointers #15418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion clippy_lints/src/casts/as_pointer_underscore.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/as_pointer_underscore.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions tests/ui/as_pointer_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion tests/ui/as_pointer_underscore.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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