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

Closed
wants to merge 0 commits into 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

This comment has been minimized.

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

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Jun 16, 2025
@Arc8ne Arc8ne requested a review from samueltardieu June 16, 2025 11:09
@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 16, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

The current state of commits (merges, followed by revert of merges) makes it harder to review. Can you please cleanup the commits and have only one commit on top of master?

Some notes about the current code:

  • You should run cargo dev dogfood and fix the issues before requesting a review, otherwise the CI will be red. In particular, if statements can be collapsed, which is kinda ironical given that the changes are in "collapsible_if.rs".
  • You can inline the spanless_eq variable at the place of its only use.
  • Use Sugg::hir_with_applicability() in order to get a proper applicability to use in suggestion. Here, you use MachineApplicable in any case, even if Sugg::hir() used the placeholders.
  • What does it mean for cond_sugg to contain the empty string as a suggestion? You should not need to make it mutable, write it as let cond_sugg = if …. Mutability complicates the code, as you have to check every code path to make sure the variable isn't modified.
  • The check_collapsible_if_nested_if_else() function should be pushed down later in the file, it is surprising to see it right after the lint declaration.
  • How will the code behave in the presence of comments between the two if? It should probably not lint if comments are present and would be removed by the rewrite. Maybe the lint_commented_code config option could be used if we can find a sensible way to preserve comments.
  • Instead of dealing with DropTemps, you should look at higher::If and see if it could be used instead to deconstruct the if statements.

@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 18, 2025
@Arc8ne
Copy link
Author

Arc8ne commented Jun 18, 2025

The current state of commits (merges, followed by revert of merges) makes it harder to review. Can you please cleanup the commits and have only one commit on top of master?

Some notes about the current code:

  • You should run cargo dev dogfood and fix the issues before requesting a review, otherwise the CI will be red. In particular, if statements can be collapsed, which is kinda ironical given that the changes are in "collapsible_if.rs".

  • You can inline the spanless_eq variable at the place of its only use.

  • Use Sugg::hir_with_applicability() in order to get a proper applicability to use in suggestion. Here, you use MachineApplicable in any case, even if Sugg::hir() used the placeholders.

  • What does it mean for cond_sugg to contain the empty string as a suggestion? You should not need to make it mutable, write it as let cond_sugg = if …. Mutability complicates the code, as you have to check every code path to make sure the variable isn't modified.

  • The check_collapsible_if_nested_if_else() function should be pushed down later in the file, it is surprising to see it right after the lint declaration.

  • How will the code behave in the presence of comments between the two if? It should probably not lint if comments are present and would be removed by the rewrite. Maybe the lint_commented_code config option could be used if we can find a sensible way to preserve comments.

  • Instead of dealing with DropTemps, you should look at higher::If and see if it could be used instead to deconstruct the if statements.

Sorry for the inconvenience and thank you for the advice, I'll work on implementing your suggestions.

@Arc8ne Arc8ne closed this Jun 19, 2025
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 19, 2025
@Arc8ne
Copy link
Author

Arc8ne commented Jun 21, 2025

The current state of commits (merges, followed by revert of merges) makes it harder to review. Can you please cleanup the commits and have only one commit on top of master?

Some notes about the current code:

* You should run `cargo dev dogfood` and fix the issues before requesting a review, otherwise the CI will be red. In particular, `if` statements can be collapsed, which is kinda ironical given that the changes are in "collapsible_if.rs".

* You can inline the `spanless_eq` variable at the place of its only use.

* Use `Sugg::hir_with_applicability()` in order to get a proper applicability to use in suggestion. Here, you use `MachineApplicable` in any case, even if `Sugg::hir()` used the placeholders.

* What does it mean for `cond_sugg` to contain the empty string as a suggestion? You should not need to make it mutable, write it as `let cond_sugg = if …`. Mutability complicates the code, as you have to check every code path to make sure the variable isn't modified.

* The `check_collapsible_if_nested_if_else()` function should be pushed down later in the file, it is surprising to see it right after the lint declaration.

* How will the code behave in the presence of comments between the two `if`? It should probably not lint if comments are present and would be removed by the rewrite. Maybe the `lint_commented_code` config option could be used if we can find a sensible way to preserve comments.

* Instead of dealing with `DropTemps`, you should look at `higher::If` and see if it could be used instead to deconstruct the `if` statements.

@samueltardieu With regard to your last suggestion, I'm not sure if I can implement it as I'm already trying to handle cases where DropTemps is present/absent.

@samueltardieu
Copy link
Contributor

@samueltardieu With regard to your last suggestion, I'm not sure if I can implement it as I'm already trying to handle cases where DropTemps is present/absent.

Is there any case where DropTemps is absent? As far as I can see in the compiler source code, it is always inserted in the case of a regular if (no if let). And anyway, even if this was possible, that would be the job of higher::If to deal with this.

@Arc8ne
Copy link
Author

Arc8ne commented Jun 22, 2025

@samueltardieu With regard to your last suggestion, I'm not sure if I can implement it as I'm already trying to handle cases where DropTemps is present/absent.

Is there any case where DropTemps is absent? As far as I can see in the compiler source code, it is always inserted in the case of a regular if (no if let). And anyway, even if this was possible, that would be the job of higher::If to deal with this.

@samueltardieu I see, thanks for letting me know about that. Based on my current understanding, DropTemps seems to be absent in the case of an if let statement.

Now that I think about it, I should be able to implement the last suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

improve collapsible if in complex cases with same expressions
4 participants