Skip to content

Commit fe93b8d

Browse files
committed
Don't lint needless_return if return has attrs
Fixes #9361
1 parent 602bec2 commit fe93b8d

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

clippy_lints/src/returns.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::{snippet_opt, snippet_with_context};
33
use clippy_utils::{fn_def_id, path_to_local_id};
44
use if_chain::if_chain;
5-
use rustc_ast::ast::Attribute;
65
use rustc_errors::Applicability;
76
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
87
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, MatchSource, PatKind, StmtKind};
@@ -11,7 +10,6 @@ use rustc_middle::lint::in_external_macro;
1110
use rustc_middle::ty::subst::GenericArgKind;
1211
use rustc_session::{declare_lint_pass, declare_tool_lint};
1312
use rustc_span::source_map::Span;
14-
use rustc_span::sym;
1513

1614
declare_clippy_lint! {
1715
/// ### What it does
@@ -152,10 +150,6 @@ impl<'tcx> LateLintPass<'tcx> for Return {
152150
}
153151
}
154152

155-
fn attr_is_cfg(attr: &Attribute) -> bool {
156-
attr.meta_item_list().is_some() && attr.has_name(sym::cfg)
157-
}
158-
159153
fn check_block_return<'tcx>(cx: &LateContext<'tcx>, block: &Block<'tcx>) {
160154
if let Some(expr) = block.expr {
161155
check_final_expr(cx, expr, Some(expr.span), RetReplacement::Empty);
@@ -178,9 +172,7 @@ fn check_final_expr<'tcx>(
178172
match expr.kind {
179173
// simple return is always "bad"
180174
ExprKind::Ret(ref inner) => {
181-
// allow `#[cfg(a)] return a; #[cfg(b)] return b;`
182-
let attrs = cx.tcx.hir().attrs(expr.hir_id);
183-
if !attrs.iter().any(attr_is_cfg) {
175+
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
184176
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
185177
if !borrows {
186178
emit_return_lint(

tests/ui/needless_return.fixed

+3-7
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,9 @@ fn needless_return_macro() -> String {
228228
format!("Hello {}", "world!")
229229
}
230230

231-
fn check_expect() -> bool {
232-
if true {
233-
// no error!
234-
return true;
235-
}
236-
#[expect(clippy::needless_return)]
237-
return true;
231+
fn issue_9361() -> i32 {
232+
#[allow(clippy::integer_arithmetic)]
233+
return 1 + 2;
238234
}
239235

240236
fn main() {}

tests/ui/needless_return.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,9 @@ fn needless_return_macro() -> String {
228228
return format!("Hello {}", "world!");
229229
}
230230

231-
fn check_expect() -> bool {
232-
if true {
233-
// no error!
234-
return true;
235-
}
236-
#[expect(clippy::needless_return)]
237-
return true;
231+
fn issue_9361() -> i32 {
232+
#[allow(clippy::integer_arithmetic)]
233+
return 1 + 2;
238234
}
239235

240236
fn main() {}

0 commit comments

Comments
 (0)