Skip to content

Commit cd26de6

Browse files
committed
misnamed_getters: support unsafe blocks around getters
In edition 2024, `unsafe` blocks must be used inside `unsafe fn` to do unsafe things. The `misnamed_getters` would not lint if the getter expression was embedded inside an `unsafe` block.
1 parent 20649a8 commit cd26de6

7 files changed

+118
-43
lines changed

clippy_lints/src/functions/misnamed_getters.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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;
5-
use rustc_hir::{Body, ExprKind, FnDecl, ImplicitSelfKind};
5+
use rustc_hir::{BlockCheckMode, Body, ExprKind, FnDecl, ImplicitSelfKind, UnsafeSource};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
88
use rustc_span::Span;
@@ -40,14 +40,25 @@ pub fn check_fn(cx: &LateContext<'_>, kind: FnKind<'_>, decl: &FnDecl<'_>, body:
4040
name
4141
};
4242

43-
// Body must be &(mut) <self_data>.name
43+
// Body must be `&(mut) <self_data>.name`, potentially in an `unsafe` block
4444
// self_data is not necessarily self, to also lint sub-getters, etc…
4545

4646
let block_expr = if let ExprKind::Block(block, _) = body.value.kind
4747
&& block.stmts.is_empty()
4848
&& let Some(block_expr) = block.expr
4949
{
50-
block_expr
50+
if let ExprKind::Block(unsafe_block, _) = block_expr.kind
51+
&& unsafe_block.stmts.is_empty()
52+
&& matches!(
53+
unsafe_block.rules,
54+
BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
55+
)
56+
&& let Some(unsafe_block_expr) = unsafe_block.expr
57+
{
58+
unsafe_block_expr
59+
} else {
60+
block_expr
61+
}
5162
} else {
5263
return;
5364
};

tests/ui/misnamed_getters.fixed

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,63 +54,63 @@ impl B {
5454
unsafe fn a(&self) -> &u8 {
5555
//~^ misnamed_getters
5656

57-
&self.a
57+
unsafe { &self.a }
5858
}
5959
unsafe fn a_mut(&mut self) -> &mut u8 {
6060
//~^ misnamed_getters
6161

62-
&mut self.a
62+
unsafe { &mut self.a }
6363
}
6464

6565
unsafe fn b(self) -> u8 {
6666
//~^ misnamed_getters
6767

68-
self.b
68+
unsafe { self.b }
6969
}
7070

7171
unsafe fn b_mut(&mut self) -> &mut u8 {
7272
//~^ misnamed_getters
7373

74-
&mut self.b
74+
unsafe { &mut self.b }
7575
}
7676

7777
unsafe fn c(&self) -> &u8 {
78-
&self.b
78+
unsafe { &self.b }
7979
}
8080

8181
unsafe fn c_mut(&mut self) -> &mut u8 {
82-
&mut self.a
82+
unsafe { &mut self.a }
8383
}
8484

8585
unsafe fn a_unchecked(&self) -> &u8 {
8686
//~^ misnamed_getters
8787

88-
&self.a
88+
unsafe { &self.a }
8989
}
9090
unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
9191
//~^ misnamed_getters
9292

93-
&mut self.a
93+
unsafe { &mut self.a }
9494
}
9595

9696
unsafe fn b_unchecked(self) -> u8 {
9797
//~^ misnamed_getters
9898

99-
self.b
99+
unsafe { self.b }
100100
}
101101

102102
unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
103103
//~^ misnamed_getters
104104

105-
&mut self.b
105+
unsafe { &mut self.b }
106106
}
107107

108108
unsafe fn c_unchecked(&self) -> &u8 {
109-
&self.b
109+
unsafe { &self.b }
110110
}
111111

112112
unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
113-
&mut self.a
113+
unsafe { &mut self.a }
114114
}
115115
}
116116

tests/ui/misnamed_getters.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,63 +54,63 @@ impl B {
5454
unsafe fn a(&self) -> &u8 {
5555
//~^ misnamed_getters
5656

57-
&self.b
57+
unsafe { &self.b }
5858
}
5959
unsafe fn a_mut(&mut self) -> &mut u8 {
6060
//~^ misnamed_getters
6161

62-
&mut self.b
62+
unsafe { &mut self.b }
6363
}
6464

6565
unsafe fn b(self) -> u8 {
6666
//~^ misnamed_getters
6767

68-
self.a
68+
unsafe { self.a }
6969
}
7070

7171
unsafe fn b_mut(&mut self) -> &mut u8 {
7272
//~^ misnamed_getters
7373

74-
&mut self.a
74+
unsafe { &mut self.a }
7575
}
7676

7777
unsafe fn c(&self) -> &u8 {
78-
&self.b
78+
unsafe { &self.b }
7979
}
8080

8181
unsafe fn c_mut(&mut self) -> &mut u8 {
82-
&mut self.a
82+
unsafe { &mut self.a }
8383
}
8484

8585
unsafe fn a_unchecked(&self) -> &u8 {
8686
//~^ misnamed_getters
8787

88-
&self.b
88+
unsafe { &self.b }
8989
}
9090
unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
9191
//~^ misnamed_getters
9292

93-
&mut self.b
93+
unsafe { &mut self.b }
9494
}
9595

9696
unsafe fn b_unchecked(self) -> u8 {
9797
//~^ misnamed_getters
9898

99-
self.a
99+
unsafe { self.a }
100100
}
101101

102102
unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
103103
//~^ misnamed_getters
104104

105-
&mut self.a
105+
unsafe { &mut self.a }
106106
}
107107

108108
unsafe fn c_unchecked(&self) -> &u8 {
109-
&self.b
109+
unsafe { &self.b }
110110
}
111111

112112
unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
113-
&mut self.a
113+
unsafe { &mut self.a }
114114
}
115115
}
116116

tests/ui/misnamed_getters.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ error: getter function appears to return the wrong field
7373
LL | / unsafe fn a(&self) -> &u8 {
7474
LL | |
7575
LL | |
76-
LL | | &self.b
77-
| | ------- help: consider using: `&self.a`
76+
LL | | unsafe { &self.b }
77+
| | ------- help: consider using: `&self.a`
7878
LL | | }
7979
| |_____^
8080

@@ -84,8 +84,8 @@ error: getter function appears to return the wrong field
8484
LL | / unsafe fn a_mut(&mut self) -> &mut u8 {
8585
LL | |
8686
LL | |
87-
LL | | &mut self.b
88-
| | ----------- help: consider using: `&mut self.a`
87+
LL | | unsafe { &mut self.b }
88+
| | ----------- help: consider using: `&mut self.a`
8989
LL | | }
9090
| |_____^
9191

@@ -95,8 +95,8 @@ error: getter function appears to return the wrong field
9595
LL | / unsafe fn b(self) -> u8 {
9696
LL | |
9797
LL | |
98-
LL | | self.a
99-
| | ------ help: consider using: `self.b`
98+
LL | | unsafe { self.a }
99+
| | ------ help: consider using: `self.b`
100100
LL | | }
101101
| |_____^
102102

@@ -106,8 +106,8 @@ error: getter function appears to return the wrong field
106106
LL | / unsafe fn b_mut(&mut self) -> &mut u8 {
107107
LL | |
108108
LL | |
109-
LL | | &mut self.a
110-
| | ----------- help: consider using: `&mut self.b`
109+
LL | | unsafe { &mut self.a }
110+
| | ----------- help: consider using: `&mut self.b`
111111
LL | | }
112112
| |_____^
113113

@@ -117,8 +117,8 @@ error: getter function appears to return the wrong field
117117
LL | / unsafe fn a_unchecked(&self) -> &u8 {
118118
LL | |
119119
LL | |
120-
LL | | &self.b
121-
| | ------- help: consider using: `&self.a`
120+
LL | | unsafe { &self.b }
121+
| | ------- help: consider using: `&self.a`
122122
LL | | }
123123
| |_____^
124124

@@ -128,8 +128,8 @@ error: getter function appears to return the wrong field
128128
LL | / unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
129129
LL | |
130130
LL | |
131-
LL | | &mut self.b
132-
| | ----------- help: consider using: `&mut self.a`
131+
LL | | unsafe { &mut self.b }
132+
| | ----------- help: consider using: `&mut self.a`
133133
LL | | }
134134
| |_____^
135135

@@ -139,8 +139,8 @@ error: getter function appears to return the wrong field
139139
LL | / unsafe fn b_unchecked(self) -> u8 {
140140
LL | |
141141
LL | |
142-
LL | | self.a
143-
| | ------ help: consider using: `self.b`
142+
LL | | unsafe { self.a }
143+
| | ------ help: consider using: `self.b`
144144
LL | | }
145145
| |_____^
146146

@@ -150,8 +150,8 @@ error: getter function appears to return the wrong field
150150
LL | / unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
151151
LL | |
152152
LL | |
153-
LL | | &mut self.a
154-
| | ----------- help: consider using: `&mut self.b`
153+
LL | | unsafe { &mut self.a }
154+
| | ----------- help: consider using: `&mut self.b`
155155
LL | | }
156156
| |_____^
157157

tests/ui/misnamed_getters_2021.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@edition: 2021
2+
#![allow(unused)]
3+
#![allow(clippy::struct_field_names)]
4+
#![warn(clippy::misnamed_getters)]
5+
6+
// Edition 2021 specific check, where `unsafe` blocks are not required
7+
// inside `unsafe fn`.
8+
9+
union B {
10+
a: u8,
11+
b: u8,
12+
}
13+
14+
impl B {
15+
unsafe fn a(&self) -> &u8 {
16+
//~^ misnamed_getters
17+
18+
&self.a
19+
}
20+
}
21+
22+
fn main() {
23+
// test code goes here
24+
}

tests/ui/misnamed_getters_2021.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@edition: 2021
2+
#![allow(unused)]
3+
#![allow(clippy::struct_field_names)]
4+
#![warn(clippy::misnamed_getters)]
5+
6+
// Edition 2021 specific check, where `unsafe` blocks are not required
7+
// inside `unsafe fn`.
8+
9+
union B {
10+
a: u8,
11+
b: u8,
12+
}
13+
14+
impl B {
15+
unsafe fn a(&self) -> &u8 {
16+
//~^ misnamed_getters
17+
18+
&self.b
19+
}
20+
}
21+
22+
fn main() {
23+
// test code goes here
24+
}

tests/ui/misnamed_getters_2021.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: getter function appears to return the wrong field
2+
--> tests/ui/misnamed_getters_2021.rs:15:5
3+
|
4+
LL | / unsafe fn a(&self) -> &u8 {
5+
LL | |
6+
LL | |
7+
LL | | &self.b
8+
| | ------- help: consider using: `&self.a`
9+
LL | | }
10+
| |_____^
11+
|
12+
= note: `-D clippy::misnamed-getters` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::misnamed_getters)]`
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)