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

More proc-macro detection #8694

Merged
merged 6 commits into from
Aug 8, 2022
Merged

More proc-macro detection #8694

merged 6 commits into from
Aug 8, 2022

Conversation

Jarcho
Copy link
Contributor

@Jarcho Jarcho commented Apr 13, 2022

fixes #6514
fixes #8683
fixes #6858
fixes #6594

This is a more general way of checking if an expression comes from a macro and could be trivially applied to other lints. Ideally this would be fixed in rustc's proc-macro api, but I don't see that happening any time soon.

changelog: FPs: [unit_arg] [default_trait_access] [missing_docs_in_private_items]: No longer trigger in code generated from proc-macros.

@rust-highfive
Copy link

r? @Manishearth

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 13, 2022
@Jarcho Jarcho force-pushed the check_proc_macro branch from c6c5d56 to f1b3615 Compare April 13, 2022 13:57
Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

I don't understand: don't we already have utils functions that check for external macros? Spans do get augmented if they're from proc macros

Also this seems to rely on some very specific span behavior in rustc, I'd like to see a lot more tests specific to this.

cc @rust-lang/clippy

@Jarcho
Copy link
Contributor Author

Jarcho commented Apr 13, 2022

The proc-macro issue is from using something like quote_spanned with the span of an input token. When this is done the span looks like a regular span, but it's attached to the wrong token. Either rustc needs to tag all those spans as coming from a different token somehow (I don't know if this is possible given the current proc-macro interface), or we need to look at the actual text associated with the span and see if it could possibly match the expected item (e.g. an if expression always has if as it's first token, exempting macros).

@Manishearth
Copy link
Member

Ah, I see!

@flip1995
Copy link
Member

Do we need to separate the ìs_proc_macro and the in_external_macro functions? I would assume that if we want to prevent linting in proc macros we most likely also want to prevent linting in declarative external macros.

@Manishearth
Copy link
Member

A thing to point out is in_external_macro is fast and should be the first thing we run, whereas is_proc_macro may be much slower and probably should be run later in the checks.

@Jarcho
Copy link
Contributor Author

Jarcho commented Apr 14, 2022

It shouldn't be so expensive that we can't run them at the same time. in_external_macro is a rustc function, so there's still going to be both functions.

@Manishearth
Copy link
Member

@Jarcho right, but check_foo() for each lint is called for each item/expr/etc in the codebase, so it does matter for performance for the things you call first to be very fast operations that can quickly reject things, and heavier stuff goes later on. So while I do think it's a bit more error prone I do feel like this check kinda belongs after the initial AST matching has happened to reject most cases, since this check is doing a lot more in-depth comparisons

@Jarcho
Copy link
Contributor Author

Jarcho commented Apr 14, 2022

True. But, in_external_macro should also be called after the initial ast matching as well. It just matters significantly less in that case.

Ideally it would be run once for all lint passes, but that's a totally different problem.

@Manishearth
Copy link
Member

Hm, fair!

@Manishearth Manishearth removed their assignment Apr 14, 2022
@Manishearth
Copy link
Member

Gonna unassign myself from complex PRs for a while since I'm a bit swamped.

@bors
Copy link
Contributor

bors commented Apr 15, 2022

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

@flip1995
Copy link
Member

Ideally it would be run once for all lint passes, but that's a totally different problem.

I mean we could just stop linting in external macros altogether: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/struct.Lint.html#structfield.report_in_external_macro

macro_rules! declare_clippy_lint {
{ $(#[$attr:meta])* pub $name:tt, style, $description:tt } => {
declare_tool_lint! {
$(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
}
};

I think last time we thought about doing this, we decided to not do it for some reason, I can't remember anymore. Maybe we can continue linting in external macros in correctness and sus lints and don't lint for all other lint groups?

@Manishearth
Copy link
Member

@flip1995 that might be good! I think overall for things like, for example, the clippy unwrap lints, folks might want them to lint inside macros. It's also tricky because "external" may still mean "in same workspace".

@flip1995
Copy link
Member

flip1995 commented Apr 15, 2022

for example, the clippy unwrap lints, folks might want them to lint inside macros

Good point, but then external macros would be just like external function calls. Clippy also doesn't look into those. It may make sense then that in the declare_clippy_lint! macro we add an optional external-macros argument so lints can opt-in to external macro linting regardless of the lint group.

It's also tricky because "external" may still mean "in same workspace".

True, but we don't have a good way to check this AFAIK

@bors
Copy link
Contributor

bors commented May 31, 2022

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

@daxpedda
Copy link
Contributor

daxpedda commented Jun 30, 2022

While fixing missing_const_for_fn, see #8854, I found that your PR basically does half of my work already. So I made Jarcho#1.

Does that make sense or is there no intention to merge this PR? Apart from the need to re-base of course.

bors added a commit that referenced this pull request Jun 30, 2022
…ishearth

Fix false-positive in `equatable_if_let`

Was linting in external macros. I guess now that I know about #8694 it seems all kinda pointless until we resolve that.

Nevertheless, it's an improvement.

Fixes #9066.

changelog:`equatable_if_let` No longer lint on macros
@Jarcho
Copy link
Contributor Author

Jarcho commented Jul 1, 2022

r? @xFrednet

Turns out nobody is assigned here. Time to bother somebody without a long review queue.

@xFrednet
Copy link
Member

xFrednet commented Jul 1, 2022

Time to bother somebody without a long review queue.

Psst, before somebody else notices xD. Totally fine, I'll add this to my backlog. Would you mind rebasing? 🙃

@Jarcho Jarcho force-pushed the check_proc_macro branch 2 times, most recently from 856b11c to bc25171 Compare July 2, 2022 03:01
@xFrednet
Copy link
Member

Hey @Jarcho, sadly, I'm a bit swarmed with RL stuff at the moment. It will take a bit longer until I can do a full review, but it's still on my todo list :)

@Jarcho
Copy link
Contributor Author

Jarcho commented Jul 11, 2022

Feel free to pass it off to someone else if needed.

@bors
Copy link
Contributor

bors commented Jul 27, 2022

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

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

The implementation looks good to me, I have some smaller suggestions and questions just to make sure that everything is correct. If they have been solved, we can merge this.

Also, thank you for your patience. The next review should be much quicker!

Out of interest, have you done any kind of benchmarks of this implementation? I suspect that it shouldn't add much, if the check is directly before the lint emission. Still, it could be interesting 🙃

clippy_lints/src/default.rs Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Outdated Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Outdated Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Show resolved Hide resolved
clippy_utils/src/check_proc_macro.rs Show resolved Hide resolved
@Jarcho Jarcho force-pushed the check_proc_macro branch from c01d2d1 to 745b194 Compare August 8, 2022 02:02
@xFrednet
Copy link
Member

xFrednet commented Aug 8, 2022

Looks good to me. Thank you for this implementation, I'm interested to see if we get any new FPs with this new checks 🙃

@bors r+

@bors
Copy link
Contributor

bors commented Aug 8, 2022

📌 Commit 745b194 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 8, 2022

⌛ Testing commit 745b194 with merge 29f3dc3...

bors added a commit that referenced this pull request Aug 8, 2022
More proc-macro detection

fixes #6514
fixes #8683
fixes #6858
fixes #6594

This is a more general way of checking if an expression comes from a macro and could be trivially applied to other lints. Ideally this would be fixed in rustc's proc-macro api, but I don't see that happening any time soon.

changelog: Don't lint `unit_arg` when generated by a proc-macro.
@xFrednet
Copy link
Member

xFrednet commented Aug 8, 2022

@bors r-

(The changelog entries are not yet up to day)

@xFrednet
Copy link
Member

xFrednet commented Aug 8, 2022

Updated the changelog entry.

@bors r+

@bors
Copy link
Contributor

bors commented Aug 8, 2022

📌 Commit 745b194 has been approved by xFrednet

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 8, 2022

⌛ Testing commit 745b194 with merge 10853f7...

@bors
Copy link
Contributor

bors commented Aug 8, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: xFrednet
Pushing 10853f7 to master...

@bors bors merged commit 10853f7 into rust-lang:master Aug 8, 2022
bors added a commit that referenced this pull request Aug 9, 2022
Use `check_proc_macro` for `missing_const_for_fn`

This uses `@Jarcho's` #8694 implementation to fix `missing_const_for_fn` linting in proc-macros.
I'm not 100% sure what I'm doing here, any feedback is appreciated.

Previously: Jarcho#1.
Fixes #8854.

changelog: [`missing_const_for_fn`]: No longer lints in proc-macros
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
7 participants