Skip to content
Merged
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
16 changes: 13 additions & 3 deletions clippy_lints/src/casts/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
use rustc_hir_pretty::qpath_to_string;
use rustc_lint::LateContext;
use rustc_middle::ty;
use rustc_span::sym;
use rustc_span::{Span, sym};

use super::PTR_AS_PTR;

Expand Down Expand Up @@ -74,7 +73,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {

let (help, final_suggestion) = if let Some(method) = omit_cast.corresponding_item() {
// don't force absolute path
let method = qpath_to_string(&cx.tcx, method);
let method = snippet_with_applicability(cx, qpath_span_without_turbofish(method), "..", &mut app);
("try call directly", format!("{method}{turbofish}()"))
} else {
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
Expand All @@ -96,3 +95,14 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
);
}
}

fn qpath_span_without_turbofish(qpath: &QPath<'_>) -> Span {
if let QPath::Resolved(_, path) = qpath
&& let [.., last_ident] = path.segments
&& last_ident.args.is_some()
{
return qpath.span().shrink_to_lo().to(last_ident.ident.span);
}

qpath.span()
}
8 changes: 8 additions & 0 deletions tests/ui/ptr_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,11 @@ mod null_entire_infer {
//~^ ptr_as_ptr
}
}

#[allow(clippy::transmute_null_to_fn)]
fn issue15283() {
unsafe {
let _: fn() = std::mem::transmute(std::ptr::null::<u8>());
//~^ ptr_as_ptr
}
}
8 changes: 8 additions & 0 deletions tests/ui/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,11 @@ mod null_entire_infer {
//~^ ptr_as_ptr
}
}

#[allow(clippy::transmute_null_to_fn)]
fn issue15283() {
unsafe {
let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
//~^ ptr_as_ptr
}
}
8 changes: 7 additions & 1 deletion tests/ui/ptr_as_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,11 @@ error: `as` casting between raw pointers without changing their constness
LL | core::ptr::null() as _
| ^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null()`

error: aborting due to 33 previous errors
error: `as` casting between raw pointers without changing their constness
--> tests/ui/ptr_as_ptr.rs:226:43
|
LL | let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u8>()`

error: aborting due to 34 previous errors