Skip to content

Commit

Permalink
fix: Specifying reason in expect(clippy::needless_return) no longer t…
Browse files Browse the repository at this point in the history
…riggers false positive

chore: Moved new tests into needless_return.rs

chore: Ran cargo uibless

Initial commit
  • Loading branch information
vHugoObject committed Sep 13, 2024
1 parent 2b7d80b commit f5efde7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
36 changes: 23 additions & 13 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,29 @@ fn check_final_expr<'tcx>(
match cx.tcx.hir().attrs(expr.hir_id) {
[] => {},
[attr] => {
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 [tool, lint_name] = meta_item.path.segments.as_slice()
&& tool.ident.name == sym::clippy
&& matches!(
lint_name.ident.name.as_str(),
"needless_return" | "style" | "all" | "warnings"
)
{
// This is an expectation of the `needless_return` lint
} else {
if !matches!(Level::from_attr(attr), Some(Level::Expect(_))) {
return;
}

let Some(metas) = attr.meta_item_list() else {
return;
};
let [NestedMetaItem::MetaItem(meta_item), ..] = metas.as_slice() else {
return;
};

let [tool, lint_name] = meta_item.path.segments.as_slice() else {
return;
};

if tool.ident.name != sym::clippy {
return;
}

if !matches!(
lint_name.ident.name.as_str(),
"needless_return" | "style" | "all" | "warnings"
) {
return;
}
},
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;
},
}
}

0 comments on commit f5efde7

Please sign in to comment.