about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSosthène Guédon <sosthene@guedon.gdn>2022-11-01 19:28:06 +0100
committerSosthène Guédon <sosthene@guedon.gdn>2022-11-20 13:45:12 +0100
commitddc49966dc4552b77582cf7e5aace8ac97d736fc (patch)
treeb6e690037d3e1a3e921c8ca099cca1f81c279d8e
parent9891af348c01f8cf14ab1e10754faccad04fcaa5 (diff)
downloadrust-ddc49966dc4552b77582cf7e5aace8ac97d736fc.tar.gz
rust-ddc49966dc4552b77582cf7e5aace8ac97d736fc.zip
Fix suggestion to point to the whole method
-rw-r--r--clippy_lints/src/functions/missnamed_getters.rs22
-rw-r--r--tests/ui/missnamed_getters.stderr90
2 files changed, 72 insertions, 40 deletions
diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs
index 1f4eefc620b..60922fb4ea4 100644
--- a/clippy_lints/src/functions/missnamed_getters.rs
+++ b/clippy_lints/src/functions/missnamed_getters.rs
@@ -1,4 +1,4 @@
-use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::source::snippet;
 use rustc_errors::Applicability;
 use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind};
@@ -13,7 +13,7 @@ pub fn check_fn(
     kind: FnKind<'_>,
     decl: &FnDecl<'_>,
     body: &Body<'_>,
-    _span: Span,
+    span: Span,
     _hir_id: HirId,
 ) {
     let FnKind::Method(ref ident, sig) = kind else {
@@ -55,6 +55,7 @@ pub fn check_fn(
     let expr_span = block_expr.span;
 
     let mut expr = block_expr;
+    // Accept &<expr>, &mut <expr> and <expr>
     if let ExprKind::AddrOf(_, _, tmp) = expr.kind {
         expr = tmp;
     }
@@ -62,7 +63,7 @@ pub fn check_fn(
         if let ExprKind::Field(self_data, ident) = expr.kind;
         if ident.name.as_str() != name;
         then {
-            (self_data,ident)
+            (self_data, ident)
         } else {
             return;
         }
@@ -121,16 +122,17 @@ pub fn check_fn(
         };
 
     if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) {
-        let snippet = snippet(cx, expr_span, "..");
-        let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap());
-        span_lint_and_sugg(
+        let left_span = block_expr.span.until(used_ident.span);
+        let snippet = snippet(cx, left_span, "..");
+        let sugg = format!("{snippet}{name}");
+        span_lint_and_then(
             cx,
             MISSNAMED_GETTERS,
-            expr_span,
+            span,
             "getter function appears to return the wrong field",
-            "consider using",
-            sugg,
-            Applicability::MaybeIncorrect,
+            |diag| {
+                diag.span_suggestion(expr_span, "consider using", sugg, Applicability::MaybeIncorrect);
+            },
         );
     }
 }
diff --git a/tests/ui/missnamed_getters.stderr b/tests/ui/missnamed_getters.stderr
index 276096ade87..2e3d9df34f4 100644
--- a/tests/ui/missnamed_getters.stderr
+++ b/tests/ui/missnamed_getters.stderr
@@ -1,64 +1,94 @@
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:12:9
+  --> $DIR/missnamed_getters.rs:11:5
    |
-LL |         &self.b
-   |         ^^^^^^^ help: consider using: `&self.a`
+LL | /     fn a(&self) -> &u8 {
+LL | |         &self.b
+   | |         ------- help: consider using: `&self.a`
+LL | |     }
+   | |_____^
    |
    = note: `-D clippy::missnamed-getters` implied by `-D warnings`
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:15:9
+  --> $DIR/missnamed_getters.rs:14:5
    |
-LL |         &mut self.b
-   |         ^^^^^^^^^^^ help: consider using: `&mut self.a`
+LL | /     fn a_mut(&mut self) -> &mut u8 {
+LL | |         &mut self.b
+   | |         ----------- help: consider using: `&mut self.a`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:19:9
+  --> $DIR/missnamed_getters.rs:18:5
    |
-LL |         self.a
-   |         ^^^^^^ help: consider using: `self.b`
+LL | /     fn b(self) -> u8 {
+LL | |         self.a
+   | |         ------ help: consider using: `self.b`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:23:9
+  --> $DIR/missnamed_getters.rs:22:5
    |
-LL |         &mut self.a
-   |         ^^^^^^^^^^^ help: consider using: `&mut self.b`
+LL | /     fn b_mut(&mut self) -> &mut u8 {
+LL | |         &mut self.a
+   | |         ----------- help: consider using: `&mut self.b`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:27:9
+  --> $DIR/missnamed_getters.rs:26:5
    |
-LL |         &self.b
-   |         ^^^^^^^ help: consider using: `&self.c`
+LL | /     fn c(&self) -> &u8 {
+LL | |         &self.b
+   | |         ------- help: consider using: `&self.c`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:31:9
+  --> $DIR/missnamed_getters.rs:30:5
    |
-LL |         &mut self.a
-   |         ^^^^^^^^^^^ help: consider using: `&mut self.c`
+LL | /     fn c_mut(&mut self) -> &mut u8 {
+LL | |         &mut self.a
+   | |         ----------- help: consider using: `&mut self.c`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:42:9
+  --> $DIR/missnamed_getters.rs:41:5
    |
-LL |         &self.b
-   |         ^^^^^^^ help: consider using: `&self.a`
+LL | /     unsafe fn a(&self) -> &u8 {
+LL | |         &self.b
+   | |         ------- help: consider using: `&self.a`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:45:9
+  --> $DIR/missnamed_getters.rs:44:5
    |
-LL |         &mut self.b
-   |         ^^^^^^^^^^^ help: consider using: `&mut self.a`
+LL | /     unsafe fn a_mut(&mut self) -> &mut u8 {
+LL | |         &mut self.b
+   | |         ----------- help: consider using: `&mut self.a`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:49:9
+  --> $DIR/missnamed_getters.rs:48:5
    |
-LL |         self.a
-   |         ^^^^^^ help: consider using: `self.b`
+LL | /     unsafe fn b(self) -> u8 {
+LL | |         self.a
+   | |         ------ help: consider using: `self.b`
+LL | |     }
+   | |_____^
 
 error: getter function appears to return the wrong field
-  --> $DIR/missnamed_getters.rs:53:9
+  --> $DIR/missnamed_getters.rs:52:5
    |
-LL |         &mut self.a
-   |         ^^^^^^^^^^^ help: consider using: `&mut self.b`
+LL | /     unsafe fn b_mut(&mut self) -> &mut u8 {
+LL | |         &mut self.a
+   | |         ----------- help: consider using: `&mut self.b`
+LL | |     }
+   | |_____^
 
 error: aborting due to 10 previous errors