Skip to content

Commit 6e3f3e5

Browse files
committed
as_pointer_underscore: don't lint when casting fn item pointers
The pointer to fn item can not be explicitly expressed with syntax. We should avoid trying to recover from `_` in that case. changelog: [`as_pointer_underscore`]: don't lint when casting fn item pointers Signed-off-by: Zihan <[email protected]>
1 parent 264bc97 commit 6e3f3e5

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

clippy_lints/src/casts/as_pointer_underscore.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use rustc_errors::Applicability;
22
use rustc_lint::LateContext;
3-
use rustc_middle::ty::Ty;
3+
use rustc_middle::ty::{self, Ty};
44

55
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
66
if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind
77
&& matches!(ty.kind, rustc_hir::TyKind::Infer(()))
8+
&& let ty::RawPtr(pointee, _) = ty_into.kind()
9+
// skip fn item type, since it can not be explicitly expressed with syntax
10+
&& !matches!(pointee.kind(), ty::FnDef(..))
811
{
912
clippy_utils::diagnostics::span_lint_and_sugg(
1013
cx,

tests/ui/as_pointer_underscore.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ fn g(s: &mut S) -> usize {
1313
s as *mut S as usize
1414
//~^ as_pointer_underscore
1515
}
16+
17+
fn issue_15281() {
18+
fn bar(_: usize) {}
19+
// pointer to fn item, lint should not trigger
20+
let _ = &bar as *const _;
21+
let _ = &(bar as fn(usize)) as *const fn(usize);
22+
//~^ as_pointer_underscore
23+
}

tests/ui/as_pointer_underscore.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ fn g(s: &mut S) -> usize {
1313
s as *mut _ as usize
1414
//~^ as_pointer_underscore
1515
}
16+
17+
fn issue_15281() {
18+
fn bar(_: usize) {}
19+
// pointer to fn item, lint should not trigger
20+
let _ = &bar as *const _;
21+
let _ = &(bar as fn(usize)) as *const _;
22+
//~^ as_pointer_underscore
23+
}

tests/ui/as_pointer_underscore.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ error: using inferred pointer cast
1313
LL | s as *mut _ as usize
1414
| ^^^^^^ help: use explicit type: `*mut S`
1515

16-
error: aborting due to 2 previous errors
16+
error: using inferred pointer cast
17+
--> tests/ui/as_pointer_underscore.rs:21:36
18+
|
19+
LL | let _ = &(bar as fn(usize)) as *const _;
20+
| ^^^^^^^^ help: use explicit type: `*const fn(usize)`
21+
22+
error: aborting due to 3 previous errors
1723

0 commit comments

Comments
 (0)