Skip to content
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

don't lint [mixed_attributes_style] when mixing docs and other attrs #12486

Merged
merged 1 commit into from
Mar 23, 2024

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Mar 14, 2024

fixes: #12435
fixes: #12436
fixes: #12530


changelog: don't lint [mixed_attributes_style] when mixing different kind of attrs; and move it to late pass;

@rustbot
Copy link
Collaborator

rustbot commented Mar 14, 2024

r? @y21

rustbot has assigned @y21.
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 Mar 14, 2024
@J-ZhengLi J-ZhengLi marked this pull request as draft March 14, 2024 09:14
@J-ZhengLi
Copy link
Member Author

I know ui-cargo is probably not a good place to put these tests, but I don't know where else should I put them.

@J-ZhengLi J-ZhengLi marked this pull request as ready for review March 14, 2024 11:57
@J-ZhengLi J-ZhengLi marked this pull request as draft March 15, 2024 01:40
@J-ZhengLi J-ZhengLi force-pushed the issue12435 branch 4 times, most recently from 95646b8 to b434eda Compare March 18, 2024 07:59
@J-ZhengLi J-ZhengLi marked this pull request as ready for review March 18, 2024 08:01
@GuillaumeGomez
Copy link
Member

Instead of changing the lint pass and having to handle new cases, why not handle doc comments separately from the other attributes? Should be simpler, no?

@y21
Copy link
Member

y21 commented Mar 20, 2024

Pre expansion passes are broken and basically-deprecated afaik. Lint level attrs dont really work well on them so they should be avoided, and moving away from it is necessary for fixing #12436.

See #6610 for more context and where this has caused issues in the past

@GuillaumeGomez
Copy link
Member

Oh I see, EarlyAttributes was in the pre-expansion pass. So let me correct my comment: why not keeping it into the early pass but as a post-expansion lint? (And why not doing the same for all other attribute lints?)

@y21
Copy link
Member

y21 commented Mar 20, 2024

why not keeping it into the early pass but as a post-expansion lint?

I wasn't sure if a post-expansion early pass wouldn't just run into the same issue.

I could be wrong on this, but my understanding of the issue here is that outlined mod foo; declarations start out as ModKind::Unloaded and so their inner attributes are not yet filled in when pre-expansion lints are run (and would be why it didn't lint on that initially).
But when changing it to a post-expansion pass, the macro expander will have expanded the outlined mod declarations and filled in the attributes. So I assumed that post-expansion early pass vs. late pass wouldn't make a difference in that regard; you'd see both inner and outer attributes for outlined mod decls no matter what.

(And why not doing the same for all other attribute lints?)

I'm not sure. One "downside" I suppose is that post-expansion lints don't see the disabled #[cfg] attributes anymore, so you wouldn't see all of the #[cfg] lints unless they're all active. But I don't know if that's the main reason (this argument could be made for all other lints).
For lints like mismatched_target_os that definitely sounds like an issue, since that one looks for cfgs that would never be true

@GuillaumeGomez
Copy link
Member

I could be wrong on this, but my understanding of the issue here is that outlined mod foo; declarations start out as ModKind::Unloaded and so their inner attributes are not yet filled in when pre-expansion lints are run (and would be why it didn't lint on that initially).
But when changing it to a post-expansion pass, the macro expander will have expanded the outlined mod declarations and filled in the attributes. So I assumed that post-expansion early pass vs. late pass wouldn't make a difference in that regard; you'd see both inner and outer attributes for outlined mod decls no matter what.

I'm surprised that modules would get inlined in the AST pass. Would be worth checking it.

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

I do think we should avoid linting on differing attributes, but I would also be fine with just landing the PR with allowing doc comments for now if that makes it easier (like it does rn), just so the lint level attribute bug is fixed and people who disagree with the lint can at least allow it properly

clippy_lints/src/attrs/mixed_attributes_style.rs Outdated Show resolved Hide resolved
clippy_lints/src/attrs/mixed_attributes_style.rs Outdated Show resolved Hide resolved
@y21
Copy link
Member

y21 commented Mar 21, 2024

I also tested it locally and yes, a post-expansion early lint pass also seems to have the same issue (inner attributes of the other file being part of the mod declaration), so I don't think we can really get around doing it like that

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Mar 22, 2024

I do think we should avoid linting on differing attributes, but I would also be fine with just landing the PR with allowing doc comments for now if that makes it easier (like it does rn), just so the lint level attribute bug is fixed and people who disagree with the lint can at least allow it properly

Make sense, I'll open another issue addressing the other problem

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Mar 22, 2024

I do think we should avoid linting on differing attributes, but I would also be fine with just landing the PR with allowing doc comments for now if that makes it easier (like it does rn)...

After experimenting on this a little bit, I ended up changes almost everything in the src file, might as well try to fix it...

The only problem was it still has some false-negative afaik. One of them being it's unable to detect the problem inside a nested module inside of test module, idk if it's by design or not, since is very specific. (If it's unexpected, I can put it in the lint's descriptions as well. I changed my mind, maybe an issue is better)
The other one is about the same attribute but with different path symbols, like #[foo] and #[krate::foo], that I have no idea how to prevent.

But hey, having FN is better than having FP right?!

@J-ZhengLi J-ZhengLi force-pushed the issue12435 branch 3 times, most recently from f69739b to 1c71163 Compare March 22, 2024 15:36
@workingjubilee
Copy link
Member

Please prefer false negatives over false positives, especially for a novel lint!

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

changes make sense to me, 👍 for also only linting on same attributes
just some minor comments and I think it's ready

tests/ui/mixed_attributes_style.rs Outdated Show resolved Hide resolved
clippy_lints/src/attrs/mixed_attributes_style.rs Outdated Show resolved Hide resolved
@J-ZhengLi J-ZhengLi marked this pull request as draft March 23, 2024 02:39
@J-ZhengLi J-ZhengLi marked this pull request as ready for review March 23, 2024 02:43
@y21
Copy link
Member

y21 commented Mar 23, 2024

Inner proc macro attributes are unstable anyway so I would say that false negative is fine

Copy link
Member

@y21 y21 left a comment

Choose a reason for hiding this comment

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

Looks good to me, just some doc nits I found
Can you also squash the commits?

clippy_lints/src/attrs/mod.rs Outdated Show resolved Hide resolved
clippy_lints/src/attrs/mod.rs Outdated Show resolved Hide resolved
don't lint [`mixed_attributes_style`] when mixing docs and other attrs

add test files for issue rust-lang#12436

move [`mixed_attributes_style`] to `LateLintPass` to enable global `allow`

stop [`mixed_attributes_style`] from linting on different attributes

add `@compile-flags` to [`mixed_attributes_style`]'s test;

turns out not linting in test mod is not a FN.

Apply suggestions from code review

Co-authored-by: Timo <[email protected]>

move [`mixed_attributes_style`] to late pass and stop it from linting on different kind of attributes
@J-ZhengLi
Copy link
Member Author

Looks good to me, just some doc nits I found Can you also squash the commits?

@y21 oops, mess up the commit message, should've put the last line first... but anyway, it should be good now, thank you for all the patience~

@y21
Copy link
Member

y21 commented Mar 23, 2024

yep, looks good, thank you for fixing these issues!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 23, 2024

📌 Commit e9f25b3 has been approved by y21

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 23, 2024

⌛ Testing commit e9f25b3 with merge db41621...

@bors
Copy link
Contributor

bors commented Mar 23, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: y21
Pushing db41621 to master...

@bors bors merged commit db41621 into rust-lang:master Mar 23, 2024
5 checks passed
@flip1995 flip1995 added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Mar 27, 2024
@flip1995 flip1995 added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. labels Apr 25, 2024
@flip1995
Copy link
Member

rust-lang/rust#124369

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2024
…ulacrum

[beta] Clippy backport

r? `@Mark-Simulacrum`

Backports:

- rust-lang/rust-clippy#12486
- rust-lang/rust-clippy#12572
- rust-lang/rust-clippy#12508
- rust-lang/rust-clippy#12617

The first one is a bit bigger as usual for a backport. But it fixes a major issue with this lint that we overlooked. So I think this is worth it. After that was merged into nightly, there were no new issues opened about this lint, so IMO this is safe to backport to `beta` and put into stable.
@xFrednet xFrednet removed the beta-accepted Accepted for backporting to the compiler in the beta channel. label Apr 29, 2024
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
8 participants