Skip to content
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
31 changes: 14 additions & 17 deletions clippy_lints/src/needless_continue.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::higher;
use clippy_utils::source::{indent_of, snippet_block, snippet_with_context};
use rustc_ast::Label;
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, LoopSource, StmtKind};
use rustc_hir::{Block, Expr, ExprKind, HirId, LoopSource, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::declare_lint_pass;
use rustc_span::{ExpnKind, Span};
Expand Down Expand Up @@ -336,14 +336,9 @@ fn emit_warning(cx: &LateContext<'_>, data: &LintData<'_>, header: &str, typ: Li
data.if_expr,
),
};
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
expr.span,
message,
None,
format!("{header}\n{snip}"),
);
span_lint_hir_and_then(cx, NEEDLESS_CONTINUE, expr.hir_id, expr.span, message, |diag| {
diag.help(format!("{header}\n{snip}"));
});
}

fn suggestion_snippet_for_continue_inside_if(cx: &LateContext<'_>, data: &LintData<'_>) -> String {
Expand Down Expand Up @@ -424,11 +419,11 @@ fn suggestion_snippet_for_continue_inside_else(cx: &LateContext<'_>, data: &Lint

fn check_last_stmt_in_expr<F>(cx: &LateContext<'_>, inner_expr: &Expr<'_>, func: &F)
where
F: Fn(Option<&Label>, Span),
F: Fn(HirId, Option<&Label>, Span),
{
match inner_expr.kind {
ExprKind::Continue(continue_label) => {
func(continue_label.label.as_ref(), inner_expr.span);
func(inner_expr.hir_id, continue_label.label.as_ref(), inner_expr.span);
},
ExprKind::If(_, then_block, else_block) if let ExprKind::Block(then_block, _) = then_block.kind => {
check_last_stmt_in_block(cx, then_block, func);
Expand All @@ -454,7 +449,7 @@ where

fn check_last_stmt_in_block<F>(cx: &LateContext<'_>, b: &Block<'_>, func: &F)
where
F: Fn(Option<&Label>, Span),
F: Fn(HirId, Option<&Label>, Span),
{
if let Some(expr) = b.expr {
check_last_stmt_in_expr(cx, expr, func);
Expand All @@ -470,15 +465,17 @@ where

fn check_and_warn(cx: &LateContext<'_>, expr: &Expr<'_>) {
with_loop_block(expr, |loop_block, label| {
let p = |continue_label: Option<&Label>, span: Span| {
let p = |continue_hir_id, continue_label: Option<&Label>, span: Span| {
if compare_labels(label, continue_label) {
span_lint_and_help(
span_lint_hir_and_then(
cx,
NEEDLESS_CONTINUE,
continue_hir_id,
span,
MSG_REDUNDANT_CONTINUE_EXPRESSION,
None,
DROP_CONTINUE_EXPRESSION_MSG,
|diag| {
diag.help(DROP_CONTINUE_EXPRESSION_MSG);
},
);
}
};
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/needless_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,38 @@ fn issue15548() {
}
}
}

fn issue16256() {
fn some_condition() -> bool {
true
}
fn another_condition() -> bool {
true
}

for _ in 0..5 {
if some_condition() {
// ...
continue;
}

if another_condition() {
// ...
// "this `continue` expression is redundant" is posted on
// the `continue` node.
#[expect(clippy::needless_continue)]
continue;
}
}

for _ in 0..5 {
// "This `else` block is redundant" is posted on the
// `else` node.
#[expect(clippy::needless_continue)]
if some_condition() {
// ...
} else {
continue;
}
}
}
Loading