Skip to content

Commit 2f1aa60

Browse files
committed
as_pointer_underscore: reduce applicability for unnameable inferred type
don't emit machine applicable suggestion when the inferred type is unnameable, for example fn item pointer changelog: [`as_pointer_underscore`]: reduce applicability for unnameable inferred type Signed-off-by: Zihan <[email protected]>
1 parent e29ec90 commit 2f1aa60

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1+
use clippy_utils::ty::ty_cannot_be_named;
12
use rustc_errors::Applicability;
23
use rustc_lint::LateContext;
3-
use rustc_middle::ty::Ty;
4+
use rustc_middle::ty::{self, Ty};
45

56
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
67
if let rustc_hir::TyKind::Ptr(rustc_hir::MutTy { ty, .. }) = cast_to_hir.kind
78
&& matches!(ty.kind, rustc_hir::TyKind::Infer(()))
9+
&& let ty::RawPtr(pointee, _) = ty_into.kind()
810
{
9-
clippy_utils::diagnostics::span_lint_and_sugg(
10-
cx,
11-
super::AS_POINTER_UNDERSCORE,
12-
cast_to_hir.span,
13-
"using inferred pointer cast",
14-
"use explicit type",
15-
ty_into.to_string(),
16-
Applicability::MachineApplicable,
17-
);
11+
if ty_cannot_be_named(*pointee) {
12+
clippy_utils::diagnostics::span_lint_and_help(
13+
cx,
14+
super::AS_POINTER_UNDERSCORE,
15+
cast_to_hir.span,
16+
format!("using inferred pointer cast to unnameable type: `{ty_into}`"),
17+
None,
18+
"use explicit type",
19+
);
20+
} else {
21+
clippy_utils::diagnostics::span_lint_and_sugg(
22+
cx,
23+
super::AS_POINTER_UNDERSCORE,
24+
cast_to_hir.span,
25+
"using inferred pointer cast",
26+
"use explicit type",
27+
ty_into.to_string(),
28+
Applicability::MachineApplicable,
29+
);
30+
};
1831
}
1932
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![warn(clippy::as_pointer_underscore)]
2+
#![crate_type = "lib"]
3+
#![no_std]
4+
5+
fn issue_15281() {
6+
fn bar(_: usize) {}
7+
// pointer to fn item, lint should not trigger
8+
let _ = &bar as *const _;
9+
//~^ as_pointer_underscore
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: using inferred pointer cast to unnameable type: `*const fn(usize) {issue_15281::bar}`
2+
--> tests/ui/as_pointer_underscore_unfixable.rs:8:21
3+
|
4+
LL | let _ = &bar as *const _;
5+
| ^^^^^^^^
6+
|
7+
= help: use explicit type
8+
= note: `-D clippy::as-pointer-underscore` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::as_pointer_underscore)]`
10+
11+
error: aborting due to 1 previous error
12+

0 commit comments

Comments
 (0)