Skip to content

Commit 3f1a186

Browse files
committed
misnamed_getters: Trigger on unsafe with _unchecked
1 parent a867c17 commit 3f1a186

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

clippy_lints/src/functions/misnamed_getters.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use rustc_errors::Applicability;
4-
use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind};
4+
use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind, Unsafety};
55
use rustc_lint::LateContext;
66
use rustc_middle::ty;
77
use rustc_span::Span;
@@ -16,7 +16,7 @@ pub fn check_fn(
1616
span: Span,
1717
_hir_id: HirId,
1818
) {
19-
let FnKind::Method(ref ident, _) = kind else {
19+
let FnKind::Method(ref ident, sig) = kind else {
2020
return;
2121
};
2222

@@ -38,6 +38,12 @@ pub fn check_fn(
3838
ImplicitSelfKind::None => return,
3939
};
4040

41+
let name = if sig.header.unsafety == Unsafety::Unsafe {
42+
name.strip_suffix("_unchecked").unwrap_or(name)
43+
} else {
44+
name
45+
};
46+
4147
// Body must be &(mut) <self_data>.name
4248
// self_data is not neccessarilly self, to also lint sub-getters, etc…
4349

tests/ui/misnamed_getters.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,41 @@ LL | | &mut self.a
9090
LL | | }
9191
| |_____^
9292

93-
error: aborting due to 10 previous errors
93+
error: getter function appears to return the wrong field
94+
--> $DIR/misnamed_getters.rs:64:5
95+
|
96+
LL | / unsafe fn a_unchecked(&self) -> &u8 {
97+
LL | | &self.b
98+
| | ------- help: consider using: `&self.a`
99+
LL | | }
100+
| |_____^
101+
102+
error: getter function appears to return the wrong field
103+
--> $DIR/misnamed_getters.rs:67:5
104+
|
105+
LL | / unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
106+
LL | | &mut self.b
107+
| | ----------- help: consider using: `&mut self.a`
108+
LL | | }
109+
| |_____^
110+
111+
error: getter function appears to return the wrong field
112+
--> $DIR/misnamed_getters.rs:71:5
113+
|
114+
LL | / unsafe fn b_unchecked(self) -> u8 {
115+
LL | | self.a
116+
| | ------ help: consider using: `self.b`
117+
LL | | }
118+
| |_____^
119+
120+
error: getter function appears to return the wrong field
121+
--> $DIR/misnamed_getters.rs:75:5
122+
|
123+
LL | / unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
124+
LL | | &mut self.a
125+
| | ----------- help: consider using: `&mut self.b`
126+
LL | | }
127+
| |_____^
128+
129+
error: aborting due to 14 previous errors
94130

0 commit comments

Comments
 (0)