Skip to content

Make needless_bool less aggressive for chained ifs - #17598

Merged
flip1995 merged 1 commit into
rust-lang:masterfrom
flip1995:fix-needless-aggressive-bool
Aug 21, 2026
Merged

Make needless_bool less aggressive for chained ifs#17598
flip1995 merged 1 commit into
rust-lang:masterfrom
flip1995:fix-needless-aggressive-bool

Conversation

@flip1995

Copy link
Copy Markdown
Member

In a function with multiple if .. { return _; }s with a trailing !_ at the end, combining the last if+!_ doesn't make the code less complex necessarily. It just moves the "problem" one up. Piping this through the entire if chain in the function either requires a non-trivial refactor or makes the code potentially less readable. Both are not acceptable for a complexity lint.

This adds a check to the lint that makes sure that there is no other if statement before the last collapsible if statement.

changelog: none
(Will be synced together with the commit that introduces it, so no changelog entry required)

@flip1995 flip1995 added the I-sync-blocker Issue: Prevents a change to be synced to rust-lang/rust label Aug 21, 2026
@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 21, 2026
@rustbot

rustbot commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 21, 2026
Comment thread clippy_lints/src/needless_bool.rs Outdated
Comment on lines +255 to +262
&& let StmtKind::Semi(prev_if_expr) | StmtKind::Expr(prev_if_expr) =
block.stmts[block.stmts.len() - 2].kind
&& let Some(higher::If {
cond: _,
then: prev_then,
r#else: None,
}) = higher::If::hir(prev_if_expr)
&& let ExprKind::Ret(Some(_)) = peel_blocks_with_stmt(prev_then).kind

@flip1995 flip1995 Aug 21, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This part could in theory be shared with the if extraction in the above if-let chain. But that would mean putting it in a function with like 3 return values. Didn't seem worth it to me.

I intentionally do not check

  1. That the prev_then_val == then_val (added a test for this)
  2. That the prev_then_func == then_func, I think this should be given due to the return type of the block already

View changes since the review

@github-actions

github-actions Bot commented Aug 21, 2026

Copy link
Copy Markdown

Lintcheck changes for 6943d70

Lint Added Removed Changed
clippy::needless_bool 0 5 0

This comment will be updated if you push new changes

@flip1995

Copy link
Copy Markdown
Member Author

All of the lintcheck findings are the exact patterns I tried to exclude from linting ✔️

Comment thread clippy_lints/src/needless_bool.rs Outdated
@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 Aug 21, 2026

@Jarcho Jarcho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Preemptively approving since I'll be away all weekend.

View changes since this review

In a function with multiple `if .. { return _; }`s with a trailing `!_` at the end, combining the last `if`+`!_` doesn't
make the code less complex necessarily. It just moves the "problem" one up. Piping this through the entire `if` chain in
the function either requires a non-trivial refactor or makes the code potentially less readable. Both are not acceptable
for a complexity lint.

This adds a check to the lint that makes sure that there is no other `if` statement before the last collapsible `if`
statement.
@flip1995
flip1995 force-pushed the fix-needless-aggressive-bool branch from 7b61af6 to 6943d70 Compare August 21, 2026 16:20
@flip1995
flip1995 enabled auto-merge August 21, 2026 16:22
@flip1995
flip1995 added this pull request to the merge queue Aug 21, 2026
Merged via the queue into rust-lang:master with commit b962eb8 Aug 21, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 21, 2026
@flip1995
flip1995 deleted the fix-needless-aggressive-bool branch August 21, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-sync-blocker Issue: Prevents a change to be synced to rust-lang/rust 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.

3 participants