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

Ignore disallowed_types in foreign macros #12571

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

Conversation

stepantubanov
Copy link

changelog: [disallowed_types]: do not trigger in foreign 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.

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

None yet

5 participants