diff options
| author | yanglsh <yanglsh@shanghaitech.edu.cn> | 2025-02-13 15:26:11 +0800 |
|---|---|---|
| committer | yanglsh <yanglsh@shanghaitech.edu.cn> | 2025-02-13 15:26:11 +0800 |
| commit | c47746ca67d53d5d8fc3ae9fd7ad144463a69e7d (patch) | |
| tree | e3b3c6e331588548e15a095e449cea34b0cf2de2 | |
| parent | 4129f5c8241ae2eafc9888fe784a536e0c29052b (diff) | |
| download | rust-c47746ca67d53d5d8fc3ae9fd7ad144463a69e7d.tar.gz rust-c47746ca67d53d5d8fc3ae9fd7ad144463a69e7d.zip | |
fix: `needless_option_as_deref` FP in trait
| -rw-r--r-- | clippy_utils/src/lib.rs | 6 | ||||
| -rw-r--r-- | tests/ui/needless_option_as_deref.fixed | 13 | ||||
| -rw-r--r-- | tests/ui/needless_option_as_deref.rs | 13 |
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 84eaf12fc13..577deb2793c 100644 --- a/tests/ui/needless_option_as_deref.fixed +++ b/tests/ui/needless_option_as_deref.fixed @@ -70,3 +70,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 fff1e45d846..47f1f90b21b 100644 --- a/tests/ui/needless_option_as_deref.rs +++ b/tests/ui/needless_option_as_deref.rs @@ -70,3 +70,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!() + } +} |
