about summary refs log tree commit diff
path: root/clippy_lints
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-07-16 22:56:49 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-07-16 22:56:49 +0800
commitdf529eb8cc6182a1e2bf0d80be7426d68c6d071e (patch)
tree5992cb5ee6d5776b0a2e61425d5bf80f190290cf /clippy_lints
parente1be06240d2be922d7cd20226ebb9b6d0051d311 (diff)
fix: `ptr_as_ptr` suggests wrongly with turbo fish
Diffstat (limited to 'clippy_lints')
-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()
+}