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

Add conf to disable disallowed_types in macros #12571

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stepantubanov
Copy link

@stepantubanov stepantubanov commented Mar 27, 2024

changelog: [disallowed_types]: add configuration to disable in external macros

@rustbot
Copy link
Collaborator

rustbot commented Mar 27, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @xFrednet (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 27, 2024
@@ -66,6 +68,11 @@ impl DisallowedTypes {
}

fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) {
if in_external_macro(cx.sess(), span) {
Copy link
Member

Choose a reason for hiding this comment

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

in_external_macro is kinda... buggy? I assume, it doesn't detects macros with spanned quote for some reason.

So, if you have a macro defined like this:

#[proc_macro_attribute]
pub fn use_std_hash_map(_args: TokenStream, input: TokenStream) -> TokenStream {
    let mut item = syn::parse_macro_input!(input as syn::ItemFn);
    let span = item.block.brace_token.span;
    let block = item.block;
    item.block = syn::parse_quote_spanned! {
        span =>
        {
            let _ = std::collections::HashMap::<i32, i32>::default();
        }
    };
    quote! {#item}.into()
}

It still triggers this lint, I think you'll need a clippy_utils::is_from_proc_macro check additionally.

Copy link
Author

Choose a reason for hiding this comment

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

Added (except for poly trait ref case - PolyTraitRef doesn't implement required trait there).

@y21
Copy link
Member

y21 commented Mar 27, 2024

Is this change motivated by some kind of "real" example? I think it would be reasonable that users might still want to be warned when macro-generated code uses a disallowed type? For example a program intended to be compiled to webassembly might want to disallow std::thread::Builder because webassembly has no threads, and they would also want to know about it even if a macro tries to do that.

@stepantubanov
Copy link
Author

stepantubanov commented Mar 27, 2024

Is this change motivated by some kind of "real" example? I think it would be reasonable that users might still want to be warned when macro-generated code uses a disallowed type? For example a program intended to be compiled to webassembly might want to disallow std::thread::Builder because webassembly has no threads, and they would also want to know about it even if a macro tries to do that.

In my case I had this issue with OpenApi macro https://docs.rs/poem-openapi/latest/poem_openapi/attr.OpenApi.html:

struct InternalApi {
  ...
}

#[OpenApi(tag = "super::ApiTags::Tables")]
impl InternalApi {
  ...
}

The problem is usage of disallowed types generated by the macro. And the only way to ignore it is to set #![allow(clippy::disallowed_types)] at the module level, which is not great.

May be we could add an attribute allow_in_external_macros (alongside path and reason attributes) for each type and apply based on that?

@xFrednet
Copy link
Member

xFrednet commented Mar 29, 2024

May be we could add an attribute allow_in_external_macros (alongside path and reason attributes) for each type and apply based on that?

I'd suggest adding a configuration value instead. Something like lint-disallowed-in-external-macros. Here are our docs on how to add a new configuration value: adding configuration to a lint

@xFrednet xFrednet 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 Apr 18, 2024
@xFrednet
Copy link
Member

Hey @stepantubanov, this is a ping from triage, since there hasn't been any activity in some time. Are you still planning to continue this implementation?

If you have any questions, you're always welcome to ask them in this PR or on Zulip.

@rustbot author

@stepantubanov
Copy link
Author

@xFrednet

May be we could add an attribute allow_in_external_macros (alongside path and reason attributes) for each type and apply based on that?

I'd suggest adding a configuration value instead. Something like lint-disallowed-in-external-macros. Here are our docs on how to add a new configuration value: adding configuration to a lint

You mean something like this?

# clippy.toml
lint-disallowed-types-in-external-macros = false # default is true

@stepantubanov stepantubanov force-pushed the disallowed-types-foreign-macros branch from ce5d856 to 73b0639 Compare June 21, 2024 20:50
@stepantubanov stepantubanov changed the title Ignore disallowed_types in foreign macros Add conf to disable disallowed_types in macros Jun 21, 2024
@stepantubanov
Copy link
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, 2024
@xFrednet
Copy link
Member

Yep, I meant something like that. Another option would be to have a list of macros, that can use disallowed types. But thinking about it, this might be too complicated for almost all use cases, unless we add something like "*" to say that all external macros are allowed to use them 🤔.

I believe your current implementation is what I had originally in mind. Any preference or thoughts?

@stepantubanov
Copy link
Author

Another option would be to have a list of macros, that can use disallowed types.

This could probably be useful in general, for other lints as well, so it'd be good to have a consistent way of configuring it.

I believe your current implementation is what I had originally in mind. Any preference or thoughts?

Seems good to me.

@xFrednet
Copy link
Member

xFrednet commented Jun 26, 2024

Another option would be to have a list of macros, that can use disallowed types.

This could probably be useful in general, for other lints as well, so it'd be good to have a consistent way of configuring it.

Do you mean the ability to allow lints in macros directly? While that would be cool, I don't think that most of our lints would currently support this. We would need to change all lints to take this new config into consideration.

I'd say it should be the responsibility of the macro author to make sure no lints are triggered, or they are allowed if needed. Even if this POV also has its downsides.

I believe your current implementation is what I had originally in mind. Any preference or thoughts?

Seems good to me.

With this, I assume you mean it seems good to you, to take the current implementation?

I'm now actually considering which option would be better. I'll create a Zulip thread for it, to ask the others. :)

Edit: Zulip Thread

@stepantubanov
Copy link
Author

With this, I assume you mean it seems good to you, to take the current implementation?

Personally I would prefer a simple true/false option. If necessary it can later be extended to support list of macros in a backward-compatible way (by implementing custom deserialize that'll accept either a bool or a list of macros).

@xFrednet xFrednet added the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Jun 30, 2024
@xFrednet xFrednet removed the I-nominated Issue: Nominated to be discussed at the next Clippy meeting label Jul 10, 2024
@xFrednet
Copy link
Member

Hey @stepantubanov, the Zulip thread sadly didn't get many responses but it seems like two others agree with my suggestion to have a set if lints instead of a true/false value with "*" as a magic value. Would you mind changing your implementation to support this?

@stepantubanov
Copy link
Author

@xFrednet
I started looking into it, but I'm not sure how to implement it. Is there an example of this logic somewhere in other lints? Specifically filtering based on which macro expanded the code.

Also, I'm wondering what would be desired behavior for this configuration when there's a chain of macro expansions (macro expansion contains other macros that also expand).

@xFrednet
Copy link
Member

You can take a look at the disallowed_macros implementation. Apparently, it uses macro_backtrace to check the macro chain.

Also, I'm wondering what would be desired behavior for this configuration when there's a chain of macro expansions (macro expansion contains other macros that also expand).

That's a good question I didn't even consider at first. I'd say that the lint should be allowed if any macro in the chain is in the configuration.

@bors
Copy link
Contributor

bors commented Jul 17, 2024

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

@xFrednet xFrednet mentioned this pull request Jul 19, 2024
@xFrednet xFrednet 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 Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants