Skip to content
Merged
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
11 changes: 10 additions & 1 deletion clippy_lints/src/returns/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet_with_context;
use clippy_utils::{
binary_expr_needs_parentheses, is_from_proc_macro, leaks_droppable_temporary_with_limited_lifetime,
span_find_starting_semi, sym,
span_contains_cfg, span_find_starting_semi, sym,
};
use rustc_ast::MetaItemInner;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -83,6 +83,15 @@ fn check_block_return<'tcx>(cx: &LateContext<'tcx>, expr_kind: &ExprKind<'tcx>,
if let Some(block_expr) = block.expr {
check_final_expr(cx, block_expr, semi_spans, RetReplacement::Empty, None);
} else if let Some(stmt) = block.stmts.last() {
if span_contains_cfg(
cx,
Span::between(
stmt.span,
cx.sess().source_map().end_point(block.span), // the closing brace of the block
),
) {
return;
}
match stmt.kind {
StmtKind::Expr(expr) => {
check_final_expr(cx, expr, semi_spans, RetReplacement::Empty, None);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,10 @@ mod else_ifs {
}
}
}

fn issue14474() -> u64 {
return 456;

#[cfg(false)]
123
}
7 changes: 7 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,10 @@ mod else_ifs {
}
}
}

fn issue14474() -> u64 {
return 456;

#[cfg(false)]
123
}