From ae5f8570967d71ab56068372f4d08859a3c13d9f Mon Sep 17 00:00:00 2001 From: VictorHugoPilled Date: Wed, 11 Sep 2024 20:37:08 +0000 Subject: [PATCH] fix: Specifying reason in expect(clippy::needless_return) no longer triggers false positive chore: Moved new tests into needless_return.rs chore: Ran cargo uibless Initial commit --- clippy_lints/src/returns.rs | 2 +- tests/ui/needless_return.fixed | 20 ++++++++++++++++++++ tests/ui/needless_return.rs | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 1e7096496..423b9b397 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -421,7 +421,7 @@ fn check_final_expr<'tcx>( if matches!(Level::from_attr(attr), Some(Level::Expect(_))) && let metas = attr.meta_item_list() && let Some(lst) = metas - && let [NestedMetaItem::MetaItem(meta_item)] = lst.as_slice() + && let [NestedMetaItem::MetaItem(meta_item), ..] = lst.as_slice() && let [tool, lint_name] = meta_item.path.segments.as_slice() && tool.ident.name == sym::clippy && matches!( diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index c5c570690..ca422e605 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -360,3 +360,23 @@ fn issue12907() -> String { } fn main() {} + +fn a(x: Option) -> Option { + match x { + Some(_) => None, + None => { + #[expect(clippy::needless_return, reason = "Use early return for errors.")] + return None; + }, + } +} + +fn b(x: Option) -> Option { + match x { + Some(_) => None, + None => { + #[expect(clippy::needless_return)] + return None; + }, + } +} diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 738611391..aad6e1313 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -370,3 +370,23 @@ fn issue12907() -> String { } fn main() {} + +fn a(x: Option) -> Option { + match x { + Some(_) => None, + None => { + #[expect(clippy::needless_return, reason = "Use early return for errors.")] + return None; + }, + } +} + +fn b(x: Option) -> Option { + match x { + Some(_) => None, + None => { + #[expect(clippy::needless_return)] + return None; + }, + } +}