Skip to content

Commit ed176b7

Browse files
authored
Fix ptr_as_ptr suggests wrongly with turbo fish (#15289)
Closes #15283 ---- changelog: [`ptr_as_ptr`]: fix wrong suggestions with turbo fish
2 parents c0dc3b6 + df529eb commit ed176b7

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::sugg::Sugg;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
7-
use rustc_hir_pretty::qpath_to_string;
87
use rustc_lint::LateContext;
98
use rustc_middle::ty;
10-
use rustc_span::sym;
9+
use rustc_span::{Span, sym};
1110

1211
use super::PTR_AS_PTR;
1312

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

7574
let (help, final_suggestion) = if let Some(method) = omit_cast.corresponding_item() {
7675
// don't force absolute path
77-
let method = qpath_to_string(&cx.tcx, method);
76+
let method = snippet_with_applicability(cx, qpath_span_without_turbofish(method), "..", &mut app);
7877
("try call directly", format!("{method}{turbofish}()"))
7978
} else {
8079
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
@@ -96,3 +95,14 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
9695
);
9796
}
9897
}
98+
99+
fn qpath_span_without_turbofish(qpath: &QPath<'_>) -> Span {
100+
if let QPath::Resolved(_, path) = qpath
101+
&& let [.., last_ident] = path.segments
102+
&& last_ident.args.is_some()
103+
{
104+
return qpath.span().shrink_to_lo().to(last_ident.ident.span);
105+
}
106+
107+
qpath.span()
108+
}

tests/ui/ptr_as_ptr.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,11 @@ mod null_entire_infer {
219219
//~^ ptr_as_ptr
220220
}
221221
}
222+
223+
#[allow(clippy::transmute_null_to_fn)]
224+
fn issue15283() {
225+
unsafe {
226+
let _: fn() = std::mem::transmute(std::ptr::null::<u8>());
227+
//~^ ptr_as_ptr
228+
}
229+
}

tests/ui/ptr_as_ptr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,11 @@ mod null_entire_infer {
219219
//~^ ptr_as_ptr
220220
}
221221
}
222+
223+
#[allow(clippy::transmute_null_to_fn)]
224+
fn issue15283() {
225+
unsafe {
226+
let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
227+
//~^ ptr_as_ptr
228+
}
229+
}

tests/ui/ptr_as_ptr.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,11 @@ error: `as` casting between raw pointers without changing their constness
201201
LL | core::ptr::null() as _
202202
| ^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null()`
203203

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

0 commit comments

Comments
 (0)