about summary refs log tree commit diff
path: root/compiler/rustc_lint/src/autorefs.rs
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2025-05-14 23:44:24 +0200
committerUrgau <urgau@numericable.fr>2025-05-14 23:53:40 +0200
commit9b3abe79d73d476d317a7a06fea5a11e5dd34950 (patch)
treeabb12839ddedc53b94429b66952b53d2a52136dd /compiler/rustc_lint/src/autorefs.rs
parentac1df15f86a911cd0f2fd54dd5df6fc58f6d4eb5 (diff)
downloadrust-9b3abe79d73d476d317a7a06fea5a11e5dd34950.tar.gz
rust-9b3abe79d73d476d317a7a06fea5a11e5dd34950.zip
Use more subdiagnostics and reword the overloaded deref note
Diffstat (limited to 'compiler/rustc_lint/src/autorefs.rs')
-rw-r--r--compiler/rustc_lint/src/autorefs.rs29
1 files changed, 20 insertions, 9 deletions
diff --git a/compiler/rustc_lint/src/autorefs.rs b/compiler/rustc_lint/src/autorefs.rs
index 9bb23526362..5de2cbf9939 100644
--- a/compiler/rustc_lint/src/autorefs.rs
+++ b/compiler/rustc_lint/src/autorefs.rs
@@ -2,9 +2,12 @@ use rustc_ast::{BorrowKind, UnOp};
 use rustc_hir::{Expr, ExprKind, Mutability};
 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, OverloadedDeref};
 use rustc_session::{declare_lint, declare_lint_pass};
-use rustc_span::{kw, sym};
+use rustc_span::sym;
 
-use crate::lints::{ImplicitUnsafeAutorefsDiag, ImplicitUnsafeAutorefsSuggestion};
+use crate::lints::{
+    ImplicitUnsafeAutorefsDiag, ImplicitUnsafeAutorefsMethodNote, ImplicitUnsafeAutorefsOrigin,
+    ImplicitUnsafeAutorefsSuggestion,
+};
 use crate::{LateContext, LateLintPass, LintContext};
 
 declare_lint! {
@@ -98,8 +101,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
                 peel_place_mappers(inner).kind
             // 1. Deref of a raw pointer.
             && typeck.expr_ty(dereferenced).is_raw_ptr()
-            // PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
             && let method_did = match expr.kind {
+                // PERF: 5. b. A method call annotated with `#[rustc_no_implicit_refs]`
                 ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
                 _ => None,
             }
@@ -111,11 +114,18 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitAutorefs {
                 ImplicitUnsafeAutorefsDiag {
                     raw_ptr_span: dereferenced.span,
                     raw_ptr_ty: typeck.expr_ty(dereferenced),
-                    autoref_span: inner.span,
-                    autoref_ty: typeck.expr_ty_adjusted(inner),
-                    method_def_span: method_did.map(|did| cx.tcx.def_span(did)),
-                    method_name: method_did.map(|did| cx.tcx.item_name(did)).unwrap_or(kw::Empty),
-                    through_overloaded_deref,
+                    origin: if through_overloaded_deref {
+                        ImplicitUnsafeAutorefsOrigin::OverloadedDeref
+                    } else {
+                        ImplicitUnsafeAutorefsOrigin::Autoref {
+                            autoref_span: inner.span,
+                            autoref_ty: typeck.expr_ty_adjusted(inner),
+                        }
+                    },
+                    method: method_did.map(|did| ImplicitUnsafeAutorefsMethodNote {
+                        def_span: cx.tcx.def_span(did),
+                        method_name: cx.tcx.item_name(did),
+                    }),
                     suggestion: ImplicitUnsafeAutorefsSuggestion {
                         mutbl: borrow_mutbl.ref_prefix_str(),
                         deref: if is_coming_from_deref { "*" } else { "" },
@@ -151,7 +161,8 @@ fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustmen
 
 /// Test if some adjustment has some implicit borrow.
 ///
-/// Returns `Some(mutability)` if the argument adjustment has implicit borrow in it.
+/// Returns `Some((mutability, was_an_overloaded_deref))` if the argument adjustment is
+/// an implicit borrow (or has an implicit borrow via an overloaded deref).
 fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<(Mutability, bool)> {
     match kind {
         &Adjust::Deref(Some(OverloadedDeref { mutbl, .. })) => Some((mutbl, true)),