Skip to content

Commit 9891af3

Browse files
committed
missnamed_getters: Match owned methods
1 parent 31b83d0 commit 9891af3

File tree

3 files changed

+110
-10
lines changed

3 files changed

+110
-10
lines changed

clippy_lints/src/functions/missnamed_getters.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,34 @@ pub fn check_fn(
3535
};
3636
name
3737
},
38+
ImplicitSelfKind::Imm | ImplicitSelfKind::Mut => name,
3839
_ => return,
3940
};
4041

4142
// Body must be &(mut) <self_data>.name
42-
// self_data is not neccessarilly self
43-
let (self_data, used_ident, span) = if_chain! {
43+
// self_data is not neccessarilly self, to also lint sub-getters, etc…
44+
45+
let block_expr = if_chain! {
4446
if let ExprKind::Block(block,_) = body.value.kind;
4547
if block.stmts.is_empty();
4648
if let Some(block_expr) = block.expr;
47-
// replace with while for as many addrof needed
48-
if let ExprKind::AddrOf(_,_, expr) = block_expr.kind;
49+
then {
50+
block_expr
51+
} else {
52+
return;
53+
}
54+
};
55+
let expr_span = block_expr.span;
56+
57+
let mut expr = block_expr;
58+
if let ExprKind::AddrOf(_, _, tmp) = expr.kind {
59+
expr = tmp;
60+
}
61+
let (self_data, used_ident) = if_chain! {
4962
if let ExprKind::Field(self_data, ident) = expr.kind;
5063
if ident.name.as_str() != name;
5164
then {
52-
(self_data,ident,block_expr.span)
65+
(self_data,ident)
5366
} else {
5467
return;
5568
}
@@ -108,12 +121,12 @@ pub fn check_fn(
108121
};
109122

110123
if cx.tcx.type_of(used_field.did) == cx.tcx.type_of(correct_field.did) {
111-
let snippet = snippet(cx, span, "..");
124+
let snippet = snippet(cx, expr_span, "..");
112125
let sugg = format!("{}{name}", snippet.strip_suffix(used_field.name.as_str()).unwrap());
113126
span_lint_and_sugg(
114127
cx,
115128
MISSNAMED_GETTERS,
116-
span,
129+
expr_span,
117130
"getter function appears to return the wrong field",
118131
"consider using",
119132
sugg,

tests/ui/missnamed_getters.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@
44
struct A {
55
a: u8,
66
b: u8,
7+
c: u8,
78
}
89

910
impl A {
1011
fn a(&self) -> &u8 {
1112
&self.b
1213
}
14+
fn a_mut(&mut self) -> &mut u8 {
15+
&mut self.b
16+
}
17+
18+
fn b(self) -> u8 {
19+
self.a
20+
}
21+
22+
fn b_mut(&mut self) -> &mut u8 {
23+
&mut self.a
24+
}
25+
26+
fn c(&self) -> &u8 {
27+
&self.b
28+
}
29+
30+
fn c_mut(&mut self) -> &mut u8 {
31+
&mut self.a
32+
}
1333
}
1434

1535
union B {
@@ -21,6 +41,25 @@ impl B {
2141
unsafe fn a(&self) -> &u8 {
2242
&self.b
2343
}
44+
unsafe fn a_mut(&mut self) -> &mut u8 {
45+
&mut self.b
46+
}
47+
48+
unsafe fn b(self) -> u8 {
49+
self.a
50+
}
51+
52+
unsafe fn b_mut(&mut self) -> &mut u8 {
53+
&mut self.a
54+
}
55+
56+
unsafe fn c(&self) -> &u8 {
57+
&self.b
58+
}
59+
60+
unsafe fn c_mut(&mut self) -> &mut u8 {
61+
&mut self.a
62+
}
2463
}
2564

2665
fn main() {

tests/ui/missnamed_getters.stderr

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
11
error: getter function appears to return the wrong field
2-
--> $DIR/missnamed_getters.rs:11:9
2+
--> $DIR/missnamed_getters.rs:12:9
33
|
44
LL | &self.b
55
| ^^^^^^^ help: consider using: `&self.a`
66
|
77
= note: `-D clippy::missnamed-getters` implied by `-D warnings`
88

99
error: getter function appears to return the wrong field
10-
--> $DIR/missnamed_getters.rs:22:9
10+
--> $DIR/missnamed_getters.rs:15:9
11+
|
12+
LL | &mut self.b
13+
| ^^^^^^^^^^^ help: consider using: `&mut self.a`
14+
15+
error: getter function appears to return the wrong field
16+
--> $DIR/missnamed_getters.rs:19:9
17+
|
18+
LL | self.a
19+
| ^^^^^^ help: consider using: `self.b`
20+
21+
error: getter function appears to return the wrong field
22+
--> $DIR/missnamed_getters.rs:23:9
23+
|
24+
LL | &mut self.a
25+
| ^^^^^^^^^^^ help: consider using: `&mut self.b`
26+
27+
error: getter function appears to return the wrong field
28+
--> $DIR/missnamed_getters.rs:27:9
29+
|
30+
LL | &self.b
31+
| ^^^^^^^ help: consider using: `&self.c`
32+
33+
error: getter function appears to return the wrong field
34+
--> $DIR/missnamed_getters.rs:31:9
35+
|
36+
LL | &mut self.a
37+
| ^^^^^^^^^^^ help: consider using: `&mut self.c`
38+
39+
error: getter function appears to return the wrong field
40+
--> $DIR/missnamed_getters.rs:42:9
1141
|
1242
LL | &self.b
1343
| ^^^^^^^ help: consider using: `&self.a`
1444

15-
error: aborting due to 2 previous errors
45+
error: getter function appears to return the wrong field
46+
--> $DIR/missnamed_getters.rs:45:9
47+
|
48+
LL | &mut self.b
49+
| ^^^^^^^^^^^ help: consider using: `&mut self.a`
50+
51+
error: getter function appears to return the wrong field
52+
--> $DIR/missnamed_getters.rs:49:9
53+
|
54+
LL | self.a
55+
| ^^^^^^ help: consider using: `self.b`
56+
57+
error: getter function appears to return the wrong field
58+
--> $DIR/missnamed_getters.rs:53:9
59+
|
60+
LL | &mut self.a
61+
| ^^^^^^^^^^^ help: consider using: `&mut self.b`
62+
63+
error: aborting due to 10 previous errors
1664

0 commit comments

Comments
 (0)