diff --git a/clippy_lints/src/functions/missnamed_getters.rs b/clippy_lints/src/functions/missnamed_getters.rs index 1f4eefc620bd..60922fb4ea49 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 &, &mut and 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 276096ade87f..2e3d9df34f45 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