Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Specifying reason in expect(clippy::needless_return) no longer triggers false positive #13393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,23 @@ fn issue12907() -> String {
}

fn main() {}

fn a(x: Option<u8>) -> Option<u8> {
match x {
Some(_) => None,
None => {
#[expect(clippy::needless_return, reason = "Use early return for errors.")]
return None;
},
}
}

fn b(x: Option<u8>) -> Option<u8> {
match x {
Some(_) => None,
None => {
#[expect(clippy::needless_return)]
return None;
},
}
}
20 changes: 20 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,23 @@ fn issue12907() -> String {
}

fn main() {}

fn a(x: Option<u8>) -> Option<u8> {
match x {
Some(_) => None,
None => {
#[expect(clippy::needless_return, reason = "Use early return for errors.")]
return None;
},
}
}

fn b(x: Option<u8>) -> Option<u8> {
match x {
Some(_) => None,
None => {
#[expect(clippy::needless_return)]
return None;
},
}
}