Skip to content
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
33 changes: 22 additions & 11 deletions clippy_lints/src/casts/as_pointer_underscore.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
use rustc_errors::Applicability;
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;
use rustc_middle::ty::{IsSuggestable, Ty};

pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'_>, cast_to_hir: &'tcx rustc_hir::Ty<'tcx>) {
pub fn check<'tcx>(cx: &LateContext<'tcx>, ty_into: Ty<'tcx>, 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(()))
{
clippy_utils::diagnostics::span_lint_and_sugg(
cx,
super::AS_POINTER_UNDERSCORE,
cast_to_hir.span,
"using inferred pointer cast",
"use explicit type",
ty_into.to_string(),
Applicability::MachineApplicable,
);
if ty_into.is_suggestable(cx.tcx, true) {
clippy_utils::diagnostics::span_lint_and_sugg(
cx,
super::AS_POINTER_UNDERSCORE,
cast_to_hir.span,
"using inferred pointer cast",
"use explicit type",
ty_into.to_string(),
Applicability::MachineApplicable,
);
} else {
clippy_utils::diagnostics::span_lint_and_help(
cx,
super::AS_POINTER_UNDERSCORE,
cast_to_hir.span,
format!("using inferred pointer cast to unsuggestable type: `{ty_into}`"),
None,
"use explicit type",
);
}
}
}
14 changes: 14 additions & 0 deletions tests/ui/as_pointer_underscore_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![warn(clippy::as_pointer_underscore)]
#![crate_type = "lib"]
#![no_std]
Comment on lines +2 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two attrs should be redundant, or are they needed for something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could the single test case in this file just be part of tests/ui/as_pointer_underscore.rs? Usually _unfixable.rs test files are for lints that emit a broken suggestion and need //@no-rustfix, but in this case there's no suggestion at all so it should be fine to live in the same file as the other lint's test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are actually still emitting some helper message w/o the full type name: "use explicit type" for unsuggestable types. Should we make it silent?


fn issue_15281() {
fn bar(_: usize) {}
// pointer to fn item, lint should not trigger
let _ = &bar as *const _;
//~^ as_pointer_underscore

let closure = &|| {};
let _ = &closure as *const _;
//~^ as_pointer_underscore
}
20 changes: 20 additions & 0 deletions tests/ui/as_pointer_underscore_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: using inferred pointer cast to unsuggestable type: `*const fn(usize) {issue_15281::bar}`
--> tests/ui/as_pointer_underscore_unfixable.rs:8:21
|
LL | let _ = &bar as *const _;
| ^^^^^^^^
|
= help: use explicit type
= note: `-D clippy::as-pointer-underscore` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::as_pointer_underscore)]`

error: using inferred pointer cast to unsuggestable type: `*const &{closure@tests/ui/as_pointer_underscore_unfixable.rs:11:20: 11:22}`
--> tests/ui/as_pointer_underscore_unfixable.rs:12:25
|
LL | let _ = &closure as *const _;
| ^^^^^^^^
|
= help: use explicit type

error: aborting due to 2 previous errors