Skip to content

Commit 20afee5

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

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
use rustc_errors::Applicability;
22
use rustc_lint::LateContext;
3-
use rustc_middle::ty::Ty;
3+
use rustc_middle::ty::{IsSuggestable, Ty};
44

5-
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
5+
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'tcx>, 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(()))
88
{
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-
);
9+
if !ty_into.is_suggestable(cx.tcx, true) {
10+
clippy_utils::diagnostics::span_lint_and_help(
11+
cx,
12+
super::AS_POINTER_UNDERSCORE,
13+
cast_to_hir.span,
14+
format!("using inferred pointer cast to unsuggestable type: `{ty_into}`"),
15+
None,
16+
"use explicit type",
17+
);
18+
} else {
19+
clippy_utils::diagnostics::span_lint_and_sugg(
20+
cx,
21+
super::AS_POINTER_UNDERSCORE,
22+
cast_to_hir.span,
23+
"using inferred pointer cast",
24+
"use explicit type",
25+
ty_into.to_string(),
26+
Applicability::MachineApplicable,
27+
);
28+
}
1829
}
1930
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
11+
let closure = &|| {};
12+
let _ = &closure as *const _;
13+
//~^ as_pointer_underscore
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: using inferred pointer cast to unsuggestable 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: using inferred pointer cast to unsuggestable type: `*const &{closure@tests/ui/as_pointer_underscore_unfixable.rs:11:20: 11:22}`
12+
--> tests/ui/as_pointer_underscore_unfixable.rs:12:25
13+
|
14+
LL | let _ = &closure as *const _;
15+
| ^^^^^^^^
16+
|
17+
= help: use explicit type
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)