about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-04-13 11:30:05 +0200
committerSamuel Tardieu <sam@rfc1149.net>2025-04-15 20:33:01 +0200
commitcd26de607aa8e421bf0b3365c9e7a3854c764c67 (patch)
treec18d07203c98438ef3fa987060aa3642f5188bd1
parent20649a87d1d7184a24b90bf93bc3cdbe080c2fb5 (diff)
downloadrust-cd26de607aa8e421bf0b3365c9e7a3854c764c67.tar.gz
rust-cd26de607aa8e421bf0b3365c9e7a3854c764c67.zip
`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.
-rw-r--r--clippy_lints/src/functions/misnamed_getters.rs17
-rw-r--r--tests/ui/misnamed_getters.fixed24
-rw-r--r--tests/ui/misnamed_getters.rs24
-rw-r--r--tests/ui/misnamed_getters.stderr32
-rw-r--r--tests/ui/misnamed_getters_2021.fixed24
-rw-r--r--tests/ui/misnamed_getters_2021.rs24
-rw-r--r--tests/ui/misnamed_getters_2021.stderr16
7 files changed, 118 insertions, 43 deletions
diff --git a/clippy_lints/src/functions/misnamed_getters.rs b/clippy_lints/src/functions/misnamed_getters.rs
index 854fe144c29..fa63876410f 100644
--- a/clippy_lints/src/functions/misnamed_getters.rs
+++ b/clippy_lints/src/functions/misnamed_getters.rs
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::source::snippet;
 use rustc_errors::Applicability;
 use rustc_hir::intravisit::FnKind;
-use rustc_hir::{Body, ExprKind, FnDecl, ImplicitSelfKind};
+use rustc_hir::{BlockCheckMode, Body, ExprKind, FnDecl, ImplicitSelfKind, UnsafeSource};
 use rustc_lint::LateContext;
 use rustc_middle::ty;
 use rustc_span::Span;
@@ -40,14 +40,25 @@ pub fn check_fn(cx: &LateContext<'_>, kind: FnKind<'_>, decl: &FnDecl<'_>, body:
         name
     };
 
-    // Body must be &(mut) <self_data>.name
+    // Body must be `&(mut) <self_data>.name`, potentially in an `unsafe` block
     // self_data is not necessarily self, to also lint sub-getters, etc…
 
     let block_expr = if let ExprKind::Block(block, _) = body.value.kind
         && block.stmts.is_empty()
         && let Some(block_expr) = block.expr
     {
-        block_expr
+        if let ExprKind::Block(unsafe_block, _) = block_expr.kind
+            && unsafe_block.stmts.is_empty()
+            && matches!(
+                unsafe_block.rules,
+                BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
+            )
+            && let Some(unsafe_block_expr) = unsafe_block.expr
+        {
+            unsafe_block_expr
+        } else {
+            block_expr
+        }
     } else {
         return;
     };
diff --git a/tests/ui/misnamed_getters.fixed b/tests/ui/misnamed_getters.fixed
index cada5307b1c..bc123d1a40b 100644
--- a/tests/ui/misnamed_getters.fixed
+++ b/tests/ui/misnamed_getters.fixed
@@ -54,63 +54,63 @@ impl B {
     unsafe fn a(&self) -> &u8 {
         //~^ misnamed_getters
 
-        &self.a
+        unsafe { &self.a }
     }
     unsafe fn a_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn b(self) -> u8 {
         //~^ misnamed_getters
 
-        self.b
+        unsafe { self.b }
     }
 
     unsafe fn b_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.b
+        unsafe { &mut self.b }
     }
 
     unsafe fn c(&self) -> &u8 {
-        &self.b
+        unsafe { &self.b }
     }
 
     unsafe fn c_mut(&mut self) -> &mut u8 {
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn a_unchecked(&self) -> &u8 {
         //~^ misnamed_getters
 
-        &self.a
+        unsafe { &self.a }
     }
     unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn b_unchecked(self) -> u8 {
         //~^ misnamed_getters
 
-        self.b
+        unsafe { self.b }
     }
 
     unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.b
+        unsafe { &mut self.b }
     }
 
     unsafe fn c_unchecked(&self) -> &u8 {
-        &self.b
+        unsafe { &self.b }
     }
 
     unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
-        &mut self.a
+        unsafe { &mut self.a }
     }
 }
 
diff --git a/tests/ui/misnamed_getters.rs b/tests/ui/misnamed_getters.rs
index f529c56b471..6590101157c 100644
--- a/tests/ui/misnamed_getters.rs
+++ b/tests/ui/misnamed_getters.rs
@@ -54,63 +54,63 @@ impl B {
     unsafe fn a(&self) -> &u8 {
         //~^ misnamed_getters
 
-        &self.b
+        unsafe { &self.b }
     }
     unsafe fn a_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.b
+        unsafe { &mut self.b }
     }
 
     unsafe fn b(self) -> u8 {
         //~^ misnamed_getters
 
-        self.a
+        unsafe { self.a }
     }
 
     unsafe fn b_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn c(&self) -> &u8 {
-        &self.b
+        unsafe { &self.b }
     }
 
     unsafe fn c_mut(&mut self) -> &mut u8 {
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn a_unchecked(&self) -> &u8 {
         //~^ misnamed_getters
 
-        &self.b
+        unsafe { &self.b }
     }
     unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.b
+        unsafe { &mut self.b }
     }
 
     unsafe fn b_unchecked(self) -> u8 {
         //~^ misnamed_getters
 
-        self.a
+        unsafe { self.a }
     }
 
     unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
         //~^ misnamed_getters
 
-        &mut self.a
+        unsafe { &mut self.a }
     }
 
     unsafe fn c_unchecked(&self) -> &u8 {
-        &self.b
+        unsafe { &self.b }
     }
 
     unsafe fn c_unchecked_mut(&mut self) -> &mut u8 {
-        &mut self.a
+        unsafe { &mut self.a }
     }
 }
 
diff --git a/tests/ui/misnamed_getters.stderr b/tests/ui/misnamed_getters.stderr
index 5dd1d75bcf6..aaf21cecb92 100644
--- a/tests/ui/misnamed_getters.stderr
+++ b/tests/ui/misnamed_getters.stderr
@@ -73,8 +73,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn a(&self) -> &u8 {
 LL | |
 LL | |
-LL | |         &self.b
-   | |         ------- help: consider using: `&self.a`
+LL | |         unsafe { &self.b }
+   | |                  ------- help: consider using: `&self.a`
 LL | |     }
    | |_____^
 
@@ -84,8 +84,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn a_mut(&mut self) -> &mut u8 {
 LL | |
 LL | |
-LL | |         &mut self.b
-   | |         ----------- help: consider using: `&mut self.a`
+LL | |         unsafe { &mut self.b }
+   | |                  ----------- help: consider using: `&mut self.a`
 LL | |     }
    | |_____^
 
@@ -95,8 +95,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn b(self) -> u8 {
 LL | |
 LL | |
-LL | |         self.a
-   | |         ------ help: consider using: `self.b`
+LL | |         unsafe { self.a }
+   | |                  ------ help: consider using: `self.b`
 LL | |     }
    | |_____^
 
@@ -106,8 +106,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn b_mut(&mut self) -> &mut u8 {
 LL | |
 LL | |
-LL | |         &mut self.a
-   | |         ----------- help: consider using: `&mut self.b`
+LL | |         unsafe { &mut self.a }
+   | |                  ----------- help: consider using: `&mut self.b`
 LL | |     }
    | |_____^
 
@@ -117,8 +117,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn a_unchecked(&self) -> &u8 {
 LL | |
 LL | |
-LL | |         &self.b
-   | |         ------- help: consider using: `&self.a`
+LL | |         unsafe { &self.b }
+   | |                  ------- help: consider using: `&self.a`
 LL | |     }
    | |_____^
 
@@ -128,8 +128,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn a_unchecked_mut(&mut self) -> &mut u8 {
 LL | |
 LL | |
-LL | |         &mut self.b
-   | |         ----------- help: consider using: `&mut self.a`
+LL | |         unsafe { &mut self.b }
+   | |                  ----------- help: consider using: `&mut self.a`
 LL | |     }
    | |_____^
 
@@ -139,8 +139,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn b_unchecked(self) -> u8 {
 LL | |
 LL | |
-LL | |         self.a
-   | |         ------ help: consider using: `self.b`
+LL | |         unsafe { self.a }
+   | |                  ------ help: consider using: `self.b`
 LL | |     }
    | |_____^
 
@@ -150,8 +150,8 @@ error: getter function appears to return the wrong field
 LL | /     unsafe fn b_unchecked_mut(&mut self) -> &mut u8 {
 LL | |
 LL | |
-LL | |         &mut self.a
-   | |         ----------- help: consider using: `&mut self.b`
+LL | |         unsafe { &mut self.a }
+   | |                  ----------- help: consider using: `&mut self.b`
 LL | |     }
    | |_____^
 
diff --git a/tests/ui/misnamed_getters_2021.fixed b/tests/ui/misnamed_getters_2021.fixed
new file mode 100644
index 00000000000..7112719a9f2
--- /dev/null
+++ b/tests/ui/misnamed_getters_2021.fixed
@@ -0,0 +1,24 @@
+//@edition: 2021
+#![allow(unused)]
+#![allow(clippy::struct_field_names)]
+#![warn(clippy::misnamed_getters)]
+
+// Edition 2021 specific check, where `unsafe` blocks are not required
+// inside `unsafe fn`.
+
+union B {
+    a: u8,
+    b: u8,
+}
+
+impl B {
+    unsafe fn a(&self) -> &u8 {
+        //~^ misnamed_getters
+
+        &self.a
+    }
+}
+
+fn main() {
+    // test code goes here
+}
diff --git a/tests/ui/misnamed_getters_2021.rs b/tests/ui/misnamed_getters_2021.rs
new file mode 100644
index 00000000000..19b5d086041
--- /dev/null
+++ b/tests/ui/misnamed_getters_2021.rs
@@ -0,0 +1,24 @@
+//@edition: 2021
+#![allow(unused)]
+#![allow(clippy::struct_field_names)]
+#![warn(clippy::misnamed_getters)]
+
+// Edition 2021 specific check, where `unsafe` blocks are not required
+// inside `unsafe fn`.
+
+union B {
+    a: u8,
+    b: u8,
+}
+
+impl B {
+    unsafe fn a(&self) -> &u8 {
+        //~^ misnamed_getters
+
+        &self.b
+    }
+}
+
+fn main() {
+    // test code goes here
+}
diff --git a/tests/ui/misnamed_getters_2021.stderr b/tests/ui/misnamed_getters_2021.stderr
new file mode 100644
index 00000000000..5495e2e3733
--- /dev/null
+++ b/tests/ui/misnamed_getters_2021.stderr
@@ -0,0 +1,16 @@
+error: getter function appears to return the wrong field
+  --> tests/ui/misnamed_getters_2021.rs:15:5
+   |
+LL | /     unsafe fn a(&self) -> &u8 {
+LL | |
+LL | |
+LL | |         &self.b
+   | |         ------- help: consider using: `&self.a`
+LL | |     }
+   | |_____^
+   |
+   = note: `-D clippy::misnamed-getters` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::misnamed_getters)]`
+
+error: aborting due to 1 previous error
+