Skip to content

handle another type of collapsible if-statement #15027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Arc8ne
Copy link

@Arc8ne Arc8ne commented Jun 10, 2025

fixes #812


changelog: [collapsible_if]: handle another type of collapsible if-statement

@rustbot
Copy link
Collaborator

rustbot commented Jun 10, 2025

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 10, 2025

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@samueltardieu
Copy link
Contributor

I'll take this one as I have already #14906 on my plate.
r? samueltardieu

@rustbot rustbot assigned samueltardieu and unassigned llogiq Jun 10, 2025
@@ -163,6 +164,54 @@ impl CollapsibleIf {
}
}

fn check_collapsible_if_nested_if_else(context: &LateContext<'_>, if_expr: &Expr<'_>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the same names as throughout the rest of Clippy:

Suggested change
fn check_collapsible_if_nested_if_else(context: &LateContext<'_>, if_expr: &Expr<'_>) {
fn check_collapsible_if_nested_if_else(cx: &LateContext<'_>, if_expr: &Expr<'_>) {

Also, if this doesn't require a self, you should get it out of the CollapsibleIf struct impl.

Comment on lines +168 to +192
let ExprKind::If(cond, then, else_opt) = if_expr.kind else {
return;
};

let ExprKind::Block(then_block, _) = then.kind else {
return;
};

let Some(then_expr) = then_block.expr else {
return;
};

let ExprKind::If(inner_cond, inner_then, _) = then_expr.kind else {
return;
};

let Some(else_expr) = else_opt else {
return;
};

let mut spanless_eq = SpanlessEq::new(context);

if !spanless_eq.eq_expr(inner_then, else_expr) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a if let chain instead of doing 6 early returns, this is as clear and much shorter.

if_expr.span,
"collapse else and nested if blocks",
format!(
"if !{} || {} {}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case with the first condition being a binary operation such as s != "foobar": in this case, the ! will be applied to s which is wrong.

You should use the Sugg interface to properly invert conditions and get parentheses if needed.

Comment on lines +205 to +207
cond.span.get_source_text(context).unwrap().to_owned(),
inner_cond.span.get_source_text(context).unwrap().to_owned(),
else_expr.span.get_source_text(context).unwrap().to_owned()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use the Sugg or snippet interface with proper applicability checking.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 10, 2025
@llogiq llogiq changed the title Resolve #812 by allowing clippy to handle another type of collapsible if-statement handle another type of collapsible if-statement Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve collapsible if in complex cases with same expressions
4 participants