Skip to content

Conversation

@samueltardieu
Copy link
Member

@samueltardieu samueltardieu commented Feb 16, 2025

Replace the use of Sugg::ast() which prevented combining if together when they contained comments by span manipulation.

A new configuration option lint_commented_code, which is true by default, allows opting out from this behavior.

If reviewed on GitHub, the second commit of this PR is best looked at side by side, with whitespace differences turned off.

changelog: [collapsible_if]: lint more cases

@rustbot
Copy link
Collaborator

rustbot commented Feb 16, 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 Feb 16, 2025
@samueltardieu samueltardieu force-pushed the push-uopxwlslsuwp branch 2 times, most recently from b8d9f37 to a94f321 Compare February 16, 2025 15:32
@samueltardieu
Copy link
Member Author

I think I'll add a configuration option to merge if containing comments, as some people might not like it.

@rustbot author

@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 Feb 18, 2025
@samueltardieu
Copy link
Member Author

Done, with the lint_commented_code option, true by default.

@rustbot review

@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 Feb 18, 2025
@samueltardieu samueltardieu force-pushed the push-uopxwlslsuwp branch 2 times, most recently from 419a0a2 to 24c2681 Compare February 20, 2025 16:52
@samueltardieu samueltardieu force-pushed the push-uopxwlslsuwp branch 2 times, most recently from 6d030ab to f065214 Compare February 28, 2025 00:42
@samueltardieu samueltardieu changed the title Lint more cases in collapsible_if, without suggestions Lint more cases in collapsible_if Feb 28, 2025
@samueltardieu
Copy link
Member Author

Rebased

@samueltardieu
Copy link
Member Author

Rebased

@samueltardieu
Copy link
Member Author

r? clippy

@rustbot rustbot assigned Jarcho and unassigned llogiq Mar 12, 2025
@samueltardieu
Copy link
Member Author

Rebased after rustup

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

LGTM overall, just a few NITs. After a full review, I noticed that the comment is kept, so defaulting the config to true is more reasonable. I would still be a bit more conservative and set it to false for this PR, in order to get it merged more quickly and then revisit it in a later PR.

/// Whether collapsible `if` chains are linted if they contain comments inside the parts
/// that would be collapsed.
#[lints(collapsible_if)]
lint_commented_code: bool = true,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
lint_commented_code: bool = true,
lint_commented_code: bool = false,

If there is a comment between the 2 if stmts, it is a strong indication that the separation is intentional and for readability.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

/// If the expression is a `||`, suggest parentheses around it.
fn par_around_or(expr: &ast::Expr) -> Vec<(Span, String)> {
Copy link
Member

Choose a reason for hiding this comment

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

NIT: parens is a more common abbreviation of parentheses.

Suggested change
fn par_around_or(expr: &ast::Expr) -> Vec<(Span, String)> {
fn parens_around_or(expr: &ast::Expr) -> Vec<(Span, String)> {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. I think I played with Sugg::maybe_par() first, and kept this abbreviation. I'll add checking if proposing to rename this to Sugg::maybe_paren() to my TODO list.

Replace the use of `Sugg::ast()` which prevented combining `if`
together when they contained comments by span manipulation.

A new configuration option `lint_commented_code`, which is `true` by
default, opts out from this behavior.
@samueltardieu
Copy link
Member Author

LGTM overall, just a few NITs. After a full review, I noticed that the comment is kept, so defaulting the config to true is more reasonable. I would still be a bit more conservative and set it to false for this PR, in order to get it merged more quickly and then revisit it in a later PR.

I've set the default to false for the moment, and rebased the patch to be able to set it to true for linting Clippy code and back to false when running lintcheck in the CI.

@samueltardieu
Copy link
Member Author

Interesting: the lintcheck test fails, because in the base test the option does not exist yet and thus cannot be overriden in the PR that introduces it. I'll have to split linting Clippy code itself with this new option into its own PR.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks!

@flip1995
Copy link
Member

r? @flip1995

@rustbot rustbot assigned flip1995 and unassigned Jarcho Mar 23, 2025
@flip1995 flip1995 added this pull request to the merge queue Mar 23, 2025
Merged via the queue into rust-lang:master with commit cad9083 Mar 23, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-uopxwlslsuwp branch March 23, 2025 17:50
github-merge-queue bot pushed a commit that referenced this pull request Mar 25, 2025
This PR enables the new ability to collapse `if` statements containing
comments (without losing them) in Clippy sources, excluding tests and
lintcheck, where the default behaviour (no collapsing in presence of
comments) is preserved.

To be applied after #14231. When it is applied, #14455 will be marked as
ready for review, then #14228 afterwards.

changelog: none

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

Labels

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.

5 participants