Skip to content

Commit ddc4996

Browse files
committed
Fix suggestion to point to the whole method
1 parent 9891af3 commit ddc4996

File tree

2 files changed

+72
-40
lines changed

2 files changed

+72
-40
lines changed

clippy_lints/src/functions/missnamed_getters.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use rustc_errors::Applicability;
44
use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind};
@@ -13,7 +13,7 @@ pub fn check_fn(
1313
kind: FnKind<'_>,
1414
decl: &FnDecl<'_>,
1515
body: &Body<'_>,
16-
_span: Span,
16+
span: Span,
1717
_hir_id: HirId,
1818
) {
1919
let FnKind::Method(ref ident, sig) = kind else {
@@ -55,14 +55,15 @@ pub fn check_fn(
5555
let expr_span = block_expr.span;
5656

5757
let mut expr = block_expr;
58+
// Accept &<expr>, &mut <expr> and <expr>
5859
if let ExprKind::AddrOf(_, _, tmp) = expr.kind {
5960
expr = tmp;
6061
}
6162
let (self_data, used_ident) = if_chain! {
6263
if let ExprKind::Field(self_data, ident) = expr.kind;
6364
if ident.name.as_str() != name;
6465
then {
65-
(self_data,ident)
66+
(self_data, ident)
6667
} else {
6768
return;
6869
}
@@ -121,16 +122,17 @@ pub fn check_fn(
121122
};
122123

123124
if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) {
124-
let snippet = snippet(cx, expr_span, "..");
125-
let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap());
126-
span_lint_and_sugg(
125+
let left_span = block_expr.span.until(used_ident.span);
126+
let snippet = snippet(cx, left_span, "..");
127+
let sugg = format!("{snippet}{name}");
128+
span_lint_and_then(
127129
cx,
128130
MISSNAMED_GETTERS,
129-
expr_span,
131+
span,
130132
"getter function appears to return the wrong field",
131-
"consider using",
132-
sugg,
133-
Applicability::MaybeIncorrect,
133+
|diag| {
134+
diag.span_suggestion(expr_span, "consider using", sugg, Applicability::MaybeIncorrect);
135+
},
134136
);
135137
}
136138
}

tests/ui/missnamed_getters.stderr

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,94 @@
11
error: getter function appears to return the wrong field
2-
--> $DIR/missnamed_getters.rs:12:9
2+
--> $DIR/missnamed_getters.rs:11:5
33
|
4-
LL | &self.b
5-
| ^^^^^^^ help: consider using: `&self.a`
4+
LL | / fn a(&self) -> &u8 {
5+
LL | | &self.b
6+
| | ------- help: consider using: `&self.a`
7+
LL | | }
8+
| |_____^
69
|
710
= note: `-D clippy::missnamed-getters` implied by `-D warnings`
811

912
error: getter function appears to return the wrong field
10-
--> $DIR/missnamed_getters.rs:15:9
13+
--> $DIR/missnamed_getters.rs:14:5
1114
|
12-
LL | &mut self.b
13-
| ^^^^^^^^^^^ help: consider using: `&mut self.a`
15+
LL | / fn a_mut(&mut self) -> &mut u8 {
16+
LL | | &mut self.b
17+
| | ----------- help: consider using: `&mut self.a`
18+
LL | | }
19+
| |_____^
1420

1521
error: getter function appears to return the wrong field
16-
--> $DIR/missnamed_getters.rs:19:9
22+
--> $DIR/missnamed_getters.rs:18:5
1723
|
18-
LL | self.a
19-
| ^^^^^^ help: consider using: `self.b`
24+
LL | / fn b(self) -> u8 {
25+
LL | | self.a
26+
| | ------ help: consider using: `self.b`
27+
LL | | }
28+
| |_____^
2029

2130
error: getter function appears to return the wrong field
22-
--> $DIR/missnamed_getters.rs:23:9
31+
--> $DIR/missnamed_getters.rs:22:5
2332
|
24-
LL | &mut self.a
25-
| ^^^^^^^^^^^ help: consider using: `&mut self.b`
33+
LL | / fn b_mut(&mut self) -> &mut u8 {
34+
LL | | &mut self.a
35+
| | ----------- help: consider using: `&mut self.b`
36+
LL | | }
37+
| |_____^
2638

2739
error: getter function appears to return the wrong field
28-
--> $DIR/missnamed_getters.rs:27:9
40+
--> $DIR/missnamed_getters.rs:26:5
2941
|
30-
LL | &self.b
31-
| ^^^^^^^ help: consider using: `&self.c`
42+
LL | / fn c(&self) -> &u8 {
43+
LL | | &self.b
44+
| | ------- help: consider using: `&self.c`
45+
LL | | }
46+
| |_____^
3247

3348
error: getter function appears to return the wrong field
34-
--> $DIR/missnamed_getters.rs:31:9
49+
--> $DIR/missnamed_getters.rs:30:5
3550
|
36-
LL | &mut self.a
37-
| ^^^^^^^^^^^ help: consider using: `&mut self.c`
51+
LL | / fn c_mut(&mut self) -> &mut u8 {
52+
LL | | &mut self.a
53+
| | ----------- help: consider using: `&mut self.c`
54+
LL | | }
55+
| |_____^
3856

3957
error: getter function appears to return the wrong field
40-
--> $DIR/missnamed_getters.rs:42:9
58+
--> $DIR/missnamed_getters.rs:41:5
4159
|
42-
LL | &self.b
43-
| ^^^^^^^ help: consider using: `&self.a`
60+
LL | / unsafe fn a(&self) -> &u8 {
61+
LL | | &self.b
62+
| | ------- help: consider using: `&self.a`
63+
LL | | }
64+
| |_____^
4465

4566
error: getter function appears to return the wrong field
46-
--> $DIR/missnamed_getters.rs:45:9
67+
--> $DIR/missnamed_getters.rs:44:5
4768
|
48-
LL | &mut self.b
49-
| ^^^^^^^^^^^ help: consider using: `&mut self.a`
69+
LL | / unsafe fn a_mut(&mut self) -> &mut u8 {
70+
LL | | &mut self.b
71+
| | ----------- help: consider using: `&mut self.a`
72+
LL | | }
73+
| |_____^
5074

5175
error: getter function appears to return the wrong field
52-
--> $DIR/missnamed_getters.rs:49:9
76+
--> $DIR/missnamed_getters.rs:48:5
5377
|
54-
LL | self.a
55-
| ^^^^^^ help: consider using: `self.b`
78+
LL | / unsafe fn b(self) -> u8 {
79+
LL | | self.a
80+
| | ------ help: consider using: `self.b`
81+
LL | | }
82+
| |_____^
5683

5784
error: getter function appears to return the wrong field
58-
--> $DIR/missnamed_getters.rs:53:9
85+
--> $DIR/missnamed_getters.rs:52:5
5986
|
60-
LL | &mut self.a
61-
| ^^^^^^^^^^^ help: consider using: `&mut self.b`
87+
LL | / unsafe fn b_mut(&mut self) -> &mut u8 {
88+
LL | | &mut self.a
89+
| | ----------- help: consider using: `&mut self.b`
90+
LL | | }
91+
| |_____^
6292

6393
error: aborting due to 10 previous errors
6494

0 commit comments

Comments
 (0)