-
Notifications
You must be signed in to change notification settings - Fork 36
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
Propagate lint attributes from the parent item to the constructors #19
Conversation
There isn't any established convention on this, is there? I'd really like to be able to select which attributes get inherited by which custom derive. Eg I want to disable Maybe this can go in as it is anyway (for when the user does want an attribute to be inherited by all custom derives), but I think the fix for #13 should be |
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.
Looks good, thanks! A couple of minor improvements inline.
src/lib.rs
Outdated
attrs.iter().filter(|attr| { | ||
if is_lint(&attr.value) { | ||
true | ||
} else if let syn::MetaItem::List(ref ident, ref items) = attr.value { |
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.
if you extract this out into a helper function like is_lint
, then the closure body could be just is_lint(...) || is_cfg_attr_lint(...)
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.
Good point, I've refactored that a bit which also shaved a few lines off.
src/lib.rs
Outdated
pub fn #new(#(#args),*) -> Self { | ||
#name #qual #inits | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn collect_parent_attrs(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> { |
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.
Either in the function name or a comment, you should make clear that this collects only attrs which are lints, not all attrs.
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.
Renamed to collect_parent_lint_attrs
, also the related variable.
160dffb
to
f389b8e
Compare
@Arnavion While #[derive(new)]
#[allow(non_snake_case)]
#[new(allow(non_snake_case))]
struct Foo { X: () } which isn't overly nice. Plus, that would complicate attr parsing somewhat since now we'd need to support all of that inner syntax (or I guess we could just propagate all Besides, if at some point we support (Similarly, one could argue that the way we detect |
@aldanor No, you misunderstand what I said. With this change, I'm saying that if I only want So I'm saying that this change is fine for the problem it's trying to solve, but that problem is not the one in #13 |
Personally I also think that a custom derive should not inherit any |
@Arnavion I understood you quite well, maybe I didn't make myself clear enough :) The easy solution is to just propagate all However, as I was thinking of a test, I don't think one exists (without using #![deny(some_lint)]
#[derive(new)]
struct Foo { ... } but this works #![deny(some_lint)]
#[derive(new)]
#[new(allow(some_lint))]
struct Foo { ... } ^ You're welcome to correct me if I'm wrong. (Even with clippy, I suspect If you use #![deny(unused_unsafe)]
#[derive(new)
struct Foo { #[new(value = "unsafe { 42 }")] x: i32 } but then this code is completely user-controlled so they could just fix it or add |
@aldanor thank you! |
Please make a PR of that then, otherwise @nrc could you please reopen #13 ? It is important to me to have no lint warnings so I'm using my own custom derive that always emits
It will be... unfortunate... if that is the decision, especially if other custom derives use this as a precedent to do the same. |
I'll leave this to @nrc to decide whether we need to support propagating arbitrary parent attrs on demand. If so, will need to figure the details and then I can push my local branch as a PR. I personally don't think a single clippy lint is worth adding new syntax; that being said, there may be non-lint use cases, like doing |
Ok, one more :) Now there should be no outstanding issues left other than releasing 1.0.
This propagates these types of attrs from the body to the ctors:
#[allow(..)]
#[deny(..)]
#[forbid(..)]
#[warn(..)]
#[cfg_attr(.., allow(..))]
#[cfg_attr(.., deny(..))]
#[cfg_attr(.., forbid(..))]
#[cfg_attr(.., warn(..))]
Closes #13.
@nrc @Arnavion @dtolnay