From a867c17ab36926a575a57e24a03e99db672055f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 2 Nov 2022 19:02:46 +0100 Subject: [PATCH] Improve code --- .../src/functions/misnamed_getters.rs | 13 ++++++----- tests/ui/misnamed_getters.rs | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs index 3859c7a62ea..0d50ec37989 100644 --- a/clippy_lints/src/functions/misnamed_getters.rs +++ b/clippy_lints/src/functions/misnamed_getters.rs @@ -16,7 +16,7 @@ pub fn check_fn( span: Span, _hir_id: HirId, ) { - let FnKind::Method(ref ident, sig) = kind else { + let FnKind::Method(ref ident, _) = kind else { return; }; @@ -27,7 +27,7 @@ pub fn check_fn( let name = ident.name.as_str(); - let name = match sig.decl.implicit_self { + let name = match decl.implicit_self { ImplicitSelfKind::MutRef => { let Some(name) = name.strip_suffix("_mut") else { return; @@ -53,11 +53,12 @@ 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; - } + let expr = if let ExprKind::AddrOf(_, _, tmp) = block_expr.kind { + tmp + } else { + block_expr + }; let (self_data, used_ident) = if_chain! { if let ExprKind::Field(self_data, ident) = expr.kind; if ident.name.as_str() != name; diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs index 7d490f4b143..6f161818251 100644 --- a/tests/ui/misnamed_getters.rs +++ b/tests/ui/misnamed_getters.rs @@ -60,6 +60,29 @@ unsafe fn c(&self) -> &u8 { unsafe fn c_mut(&mut self) -> &mut u8 { &mut self.a } + + unsafe fn a_unchecked(&self) -> &u8 { + &self.b + } + unsafe fn a_unchecked_mut(&mut self) -> &mut u8 { + &mut self.b + } + + unsafe fn b_unchecked(self) -> u8 { + self.a + } + + unsafe fn b_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } + + unsafe fn c_unchecked(&self) -> &u8 { + &self.b + } + + unsafe fn c_unchecked_mut(&mut self) -> &mut u8 { + &mut self.a + } } fn main() {