about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorEzra Shaw <ezrasure@outlook.com>2023-01-18 13:14:56 +1300
committerEzra Shaw <ezrasure@outlook.com>2023-01-18 13:14:56 +1300
commitb73cdf1b29d25d3c6d0cc4f8a7744b08930e86ee (patch)
treea3bdc4dc6325dff8a8b5b26cb2a70fca101ac6ed /compiler
parentca1178f02237fd84649a30b74052da4ef265371f (diff)
special case removing `&` suggestion
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir/src/hir.rs8
-rw-r--r--compiler/rustc_hir_typeck/src/cast.rs24
2 files changed, 26 insertions, 6 deletions
diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs
index 60f5b79de10..5e59475a7bd 100644
--- a/compiler/rustc_hir/src/hir.rs
+++ b/compiler/rustc_hir/src/hir.rs
@@ -1787,6 +1787,14 @@ impl Expr<'_> {
         expr
     }
 
+    pub fn peel_borrows(&self) -> &Self {
+        let mut expr = self;
+        while let ExprKind::AddrOf(.., inner) = &expr.kind {
+            expr = inner;
+        }
+        expr
+    }
+
     pub fn can_have_side_effects(&self) -> bool {
         match self.peel_drop_temps().kind {
             ExprKind::Path(_) | ExprKind::Lit(_) => false,
diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs
index b312a3d30af..712f9b87aed 100644
--- a/compiler/rustc_hir_typeck/src/cast.rs
+++ b/compiler/rustc_hir_typeck/src/cast.rs
@@ -31,6 +31,7 @@
 use super::FnCtxt;
 
 use crate::type_error_struct;
+use hir::ExprKind;
 use rustc_errors::{
     struct_span_err, Applicability, DelayDm, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
 };
@@ -237,12 +238,23 @@ impl<'a, 'tcx> CastCheck<'tcx> {
                     fcx,
                 );
 
-                err.span_suggestion_verbose(
-                    self.expr_span.shrink_to_lo(),
-                    "dereference the expression",
-                    "*",
-                    Applicability::MachineApplicable,
-                );
+                if matches!(self.expr.kind, ExprKind::AddrOf(..)) {
+                    // get just the borrow part of the expression
+                    let span = self.expr_span.with_hi(self.expr.peel_borrows().span.lo());
+                    err.span_suggestion_verbose(
+                        span,
+                        "remove the unneeded borrow",
+                        "",
+                        Applicability::MachineApplicable,
+                    );
+                } else {
+                    err.span_suggestion_verbose(
+                        self.expr_span.shrink_to_lo(),
+                        "dereference the expression",
+                        "*",
+                        Applicability::MachineApplicable,
+                    );
+                }
 
                 err.emit();
             }