about summary refs log tree commit diff
path: root/clippy_lints/src
diff options
context:
space:
mode:
authorllogiq <bogusandre@gmail.com>2025-07-16 17:05:35 +0000
committerGitHub <noreply@github.com>2025-07-16 17:05:35 +0000
commited176b7b8889d6ae77c5be54dc48bcb1bbbf9195 (patch)
tree5df2dc8fc79377da9238fd56850fe1e58d1785b7 /clippy_lints/src
parentc0dc3b616da6d4f2f53ee1da4139dc258cf9b27f (diff)
parentdf529eb8cc6182a1e2bf0d80be7426d68c6d071e (diff)
Fix `ptr_as_ptr` suggests wrongly with turbo fish (#15289)
Closes rust-lang/rust-clippy#15283

----

changelog: [`ptr_as_ptr`]: fix wrong suggestions with turbo fish
Diffstat (limited to 'clippy_lints/src')
-rw-r--r--clippy_lints/src/casts/ptr_as_ptr.rs16
1 files changed, 13 insertions, 3 deletions
diff --git a/clippy_lints/src/casts/ptr_as_ptr.rs b/clippy_lints/src/casts/ptr_as_ptr.rs
index 6f944914b8f..ee0f3fa81c6 100644
--- a/clippy_lints/src/casts/ptr_as_ptr.rs
+++ b/clippy_lints/src/casts/ptr_as_ptr.rs
@@ -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;
 
@@ -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);
@@ -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()
+}