-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Add conf to disable disallowed_types
in macros
#12571
Conversation
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 (
|
clippy_lints/src/disallowed_types.rs
Outdated
@@ -66,6 +68,11 @@ impl DisallowedTypes { | |||
} | |||
|
|||
fn check_res_emit(&self, cx: &LateContext<'_>, res: &Res, span: Span) { | |||
if in_external_macro(cx.sess(), span) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
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 |
In my case I had this issue with 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 May be we could add an attribute |
I'd suggest adding a configuration value instead. Something like |
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 |
You mean something like this? # clippy.toml
lint-disallowed-types-in-external-macros = false # default is true |
ce5d856
to
73b0639
Compare
disallowed_types
in foreign macrosdisallowed_types
in macros
@rustbot ready |
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? |
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.
Seems good to me. |
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.
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 |
Personally I would prefer a simple |
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 |
@xFrednet 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). |
You can take a look at the
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. |
☔ The latest upstream changes (presumably #13088) made this pull request unmergeable. Please resolve the merge conflicts. |
changelog: [
disallowed_types
]: add configuration to disable in external macros