Skip to content

Add ifs_as_logical_ops lint - #14904

Open
barun511 wants to merge 2 commits into
rust-lang:masterfrom
barun511:parruck/ifs-as-logical-ops
Open

barun511 wants to merge 2 commits into
rust-lang:masterfrom
barun511:parruck/ifs-as-logical-ops

Conversation

@barun511

@barun511 barun511 commented May 26, 2025 •

Copy link
Copy Markdown
Contributor

View all comments

Fixes #14865 by adding the ifs_as_logical_ops lint.

Only adds the conjunctive version (transform if x else y => x && y) for now.

Please write a short comment explaining your change (or "none" for internal only changes)

changelog: [ifs_as_logical_ops] : "Warn about cases where x && y could be used in place of if conditions"

@rustbot

rustbot commented May 26, 2025

Copy link
Copy Markdown
Collaborator

r? @Jarcho

rustbot has assigned @Jarcho.
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 May 26, 2025
@rustbot

This comment has been minimized.

@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from ed0c921 to a277b18 Compare June 2, 2025 18:36
@barun511

barun511 commented Jun 2, 2025

Copy link
Copy Markdown
Contributor Author

Hm, it doesn't look like my rebase really worked, let me have another go...

@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from a277b18 to ed0c921 Compare June 2, 2025 19:27
@barun511

barun511 commented Jun 2, 2025

Copy link
Copy Markdown
Contributor Author

I'll take a look at merge conflicts once I've got a review, I think

@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.

Did a first pass over this. You'll need a late pass to avoid an annoying false positive.

Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.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 Jun 3, 2025
@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from ed0c921 to 5baaae2 Compare June 5, 2025 22:16
@barun511

barun511 commented Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I had a go at writing it with a late lint pass

@barun511

barun511 commented Jun 5, 2025

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 5, 2025

@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.

First just a couple of small changes. These are a little nitpicky.

Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.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 Jun 20, 2025
@barun511

Copy link
Copy Markdown
Contributor Author

Thanks! Responded

@barun511

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 21, 2025

@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.

Next you'll need to handle macros and fill in the docs.

Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.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 Jun 24, 2025
@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from e7123b6 to bcfe2fe Compare July 5, 2025 14:19
@barun511

barun511 commented Jul 5, 2025

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jul 5, 2025
@Jarcho

Jarcho commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Ping @barun511 are you willing to continue working on this? There's #17449 which continues this if you'd prefer to let this continue there.

Sorry for originally letting this sit for a while.

@barun511

barun511 commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

No worries!

Sorry for not responding to the message above, it wasn't clear to me how far away from approval the PR was - and given that it didn't seem to get any traction after a point I didn't want to keep maintaining the PR if the work wasn't a priority.

I'm happy to clean this up tomorrow

@barun511

Copy link
Copy Markdown
Contributor Author

Oh I still had my unanswered question about proc_macro above, but I can try and figure that out myself

@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from 009e0c6 to 2809710 Compare September 11, 2026 08:39
@rustbot

rustbot commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch 2 times, most recently from 4b3db68 to 432d723 Compare September 11, 2026 09:03

@ada4a ada4a 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.

The first commit still adds the .DS_Store file (which the second commit then removes), please remove 👉👈

View changes since this review

Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
Comment thread clippy_lints/src/ifs_as_logical_ops.rs Outdated
@ada4a

ada4a commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

it wasn't clear to me how far away from approval the PR was

I'd say it looks pretty much ready to merge, after we figure out the proc-macro stuff (if you don't manage to figure it out yourself, consider asking @.Jarcho). Thanks again for all your work so far:)

@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch 4 times, most recently from 84354f9 to 121afb8 Compare September 11, 2026 12:22
@barun511

Copy link
Copy Markdown
Contributor Author

Regarding the question: "Why does removing the is_proc_macro check not cause the test to fail?"

I investigated by doing the following:

  • I first tried to remove is_proc_macro check from other similiar tests, and see if they failed. In fact, they did - so I concluded that is_proc_macro was not at fault.
  • I then theorized that potentially is_external_macro, or some other checks before this were triggering and causing is_proc_macro to be irrelevant, but even on removing all these checks the test continued to pass.
  • After doing all of this, I realised I was quite dumb and just put in some dbg statements between the ifs to see what exact condition was causing it to fail.

It seems like this piece of code returns false for proc_macros, because the span of the macro usually does not have the character '{' in it.

I'm not quite sure what to make of this - of course after we add the check we won't be hitting this case, but we then don't have a test case that would fail if the in_proc_macro or in_external_macro checks are removed.

We could always consider taking this as a follow up/finding a more robust way of checking that there are no comments in the span

@barun511

Copy link
Copy Markdown
Contributor Author

(or also potentially I'm not walking the span to the context correctly)?

@Jarcho

Jarcho commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

You can practically ignore the proc macro check since you're basically already doing it when you check for comments. If you wrote the test as if b1 $({ num > 30 }) else $({ false }) then you would need it ($() escapes the span assignment), but I don't think there are any real macros that would pose a problem here.

@Jarcho

Jarcho commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

You might want to add .DS_store to your global .gitignore just to avoid problems with it in the future. We should add it and some other OS files to ours as well.

All the original work

Address all comments really fr fr
@barun511
barun511 force-pushed the parruck/ifs-as-logical-ops branch from 121afb8 to 3c7a831 Compare September 12, 2026 14:57
@barun511

Copy link
Copy Markdown
Contributor Author

Yep, I did update my global gitignore, and i've updated to no longer use proc macro check!

Lmk if anything else is blocking

@barun511
barun511 requested a review from ada4a September 12, 2026 14:57

@ada4a ada4a 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.

LGTM, thank you again:)

Now let's start the FCP

@rustbot label lint-nominated

View changes since this review

@rustbot rustbot added the lint-nominated Create an FCP-thread on Zulip for this PR label Sep 12, 2026
@rustbot

rustbot commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@barun511

Copy link
Copy Markdown
Contributor Author

@Jarcho just pinging for re-review here!

@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #13598) made this pull request unmergeable. Please resolve the merge conflicts.

@ada4a ada4a 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.

Sorry, forgot to finish the FCP... There has been one approval by @llogiq, so I'd say let's merge this after the rebase and one fix

View changes since this review

/// b1 && a()
/// }
/// ```
#[clippy::version = "1.100.0"]

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.

Suggested change
#[clippy::version = "1.100.0"]
#[clippy::version = "1.101.0"]

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest A && B for if A { B } else { false }

7 participants