Skip to content

Commit 0c87db2

Browse files
fix(needless_return): FP with cfgd code after return (#15669)
Fixes #14474 changelog: [`needless_return`]: FP with `cfg`d code after `return`
2 parents 1d0582a + 6f3f1f3 commit 0c87db2

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clippy_lints/src/returns/needless_return.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::{
44
binary_expr_needs_parentheses, is_from_proc_macro, leaks_droppable_temporary_with_limited_lifetime,
5-
span_find_starting_semi, sym,
5+
span_contains_cfg, span_find_starting_semi, sym,
66
};
77
use rustc_ast::MetaItemInner;
88
use rustc_errors::Applicability;
@@ -83,6 +83,15 @@ fn check_block_return<'tcx>(cx: &LateContext<'tcx>, expr_kind: &ExprKind<'tcx>,
8383
if let Some(block_expr) = block.expr {
8484
check_final_expr(cx, block_expr, semi_spans, RetReplacement::Empty, None);
8585
} else if let Some(stmt) = block.stmts.last() {
86+
if span_contains_cfg(
87+
cx,
88+
Span::between(
89+
stmt.span,
90+
cx.sess().source_map().end_point(block.span), // the closing brace of the block
91+
),
92+
) {
93+
return;
94+
}
8695
match stmt.kind {
8796
StmtKind::Expr(expr) => {
8897
check_final_expr(cx, expr, semi_spans, RetReplacement::Empty, None);

tests/ui/needless_return.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,10 @@ mod else_ifs {
517517
}
518518
}
519519
}
520+
521+
fn issue14474() -> u64 {
522+
return 456;
523+
524+
#[cfg(false)]
525+
123
526+
}

tests/ui/needless_return.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,10 @@ mod else_ifs {
526526
}
527527
}
528528
}
529+
530+
fn issue14474() -> u64 {
531+
return 456;
532+
533+
#[cfg(false)]
534+
123
535+
}

0 commit comments

Comments
 (0)