about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlejandra González <blyxyas@gmail.com>2025-02-17 23:33:46 +0000
committerGitHub <noreply@github.com>2025-02-17 23:33:46 +0000
commite2d9b9a32a83fe568e92083ecd3971d71e508031 (patch)
tree0e4c1a13522d06b2c2b1bb8b2835131f41cf8525
parent437014b951d48444838a6684fa879582165c687e (diff)
parentc47746ca67d53d5d8fc3ae9fd7ad144463a69e7d (diff)
fix: `needless_option_as_deref` FP in trait (#14210)
fixes #14148

Another case of #13077 and #8646

changelog: [`needless_option_as_deref`]: fix FP in trait
-rw-r--r--clippy_utils/src/lib.rs6
-rw-r--r--tests/ui/needless_option_as_deref.fixed13
-rw-r--r--tests/ui/needless_option_as_deref.rs13
3 files changed, 31 insertions, 1 deletions
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index 0d9502c50db..d1b3d5508b0 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -107,7 +107,7 @@ use rustc_hir::{
     self as hir, Arm, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
     Destination, Expr, ExprField, ExprKind, FnDecl, FnRetTy, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
     ImplItemRef, Item, ItemKind, LangItem, LetStmt, MatchSource, Mutability, Node, OwnerId, OwnerNode, Param, Pat,
-    PatExpr, PatExprKind, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind,
+    PatExpr, PatExprKind, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitFn, TraitItem, TraitItemKind,
     TraitItemRef, TraitRef, TyKind, UnOp, def,
 };
 use rustc_lexer::{TokenKind, tokenize};
@@ -1505,6 +1505,10 @@ pub fn get_enclosing_block<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Optio
         | Node::ImplItem(&ImplItem {
             kind: ImplItemKind::Fn(_, eid),
             ..
+        })
+        | Node::TraitItem(&TraitItem {
+            kind: TraitItemKind::Fn(_, TraitFn::Provided(eid)),
+            ..
         }) => match cx.tcx.hir().body(eid).value.kind {
             ExprKind::Block(block, _) => Some(block),
             _ => None,
diff --git a/tests/ui/needless_option_as_deref.fixed b/tests/ui/needless_option_as_deref.fixed
index 5bb0244ee09..fd639f3b57f 100644
--- a/tests/ui/needless_option_as_deref.fixed
+++ b/tests/ui/needless_option_as_deref.fixed
@@ -73,3 +73,16 @@ mod issue_non_copy_13077 {
         pub field: (),
     }
 }
+
+mod issue14148 {
+    pub trait SomeTrait {
+        fn something(&self, mut maybe_side_effect: Option<&mut String>) {
+            other(maybe_side_effect.as_deref_mut());
+            other(maybe_side_effect);
+        }
+    }
+
+    fn other(_maybe_side_effect: Option<&mut String>) {
+        unimplemented!()
+    }
+}
diff --git a/tests/ui/needless_option_as_deref.rs b/tests/ui/needless_option_as_deref.rs
index 9cd8afe0bc7..cbf7935794b 100644
--- a/tests/ui/needless_option_as_deref.rs
+++ b/tests/ui/needless_option_as_deref.rs
@@ -73,3 +73,16 @@ mod issue_non_copy_13077 {
         pub field: (),
     }
 }
+
+mod issue14148 {
+    pub trait SomeTrait {
+        fn something(&self, mut maybe_side_effect: Option<&mut String>) {
+            other(maybe_side_effect.as_deref_mut());
+            other(maybe_side_effect);
+        }
+    }
+
+    fn other(_maybe_side_effect: Option<&mut String>) {
+        unimplemented!()
+    }
+}