fix: fix false positives on duplicated_attributes - #17655
fix: fix false positives on duplicated_attributes#17655InvalidPathException wants to merge 1 commit into
Conversation
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. |
There was a problem hiding this comment.
Since I was pinged, here’s my opinion: I don't think this is the correct fix.
This is still making the assumption that all attributes or fragments of attributes not specifically identified otherwise (lints, doc) should be linted. This is, as I said in #13355, just not correct. The macro (or tool attribute) processor can do anything with the attributes it’s looking for. An example of this is even in the test cases: without inspecting the code of the proc-macro proc_macro_attr::DerivedAttrs, you can’t know whether the #[attr] it looks for is idempotent or not.
The lint should not fire on attributes it does not know the semantics of. It should have a list of the attributes it can check.
| attr: &MetaItem, | ||
| attr_paths: &mut FxHashMap<String, Span>, | ||
| parent: &mut Vec<Symbol>, | ||
| parent: &mut Vec<String>, |
There was a problem hiding this comment.
Can you refactor this to stay with Vec<Symbol>? I think it is a "thighter" type, so modeling the state space sligtly better, or?
Or would Symbol::intern be worse for performance?
Why was this change done?
There was a problem hiding this comment.
Hi, you are right, it's to avoid Symbol::intern, I don't think it meaningfully change performance but did not take the "tighter" aspect into account.
Thanks for the feedback! Seems like the right approach is to limit the linted attributes to lint-level ones. Another thing I noted: rustc catches most duplication patterns it knows, and clippy catches them again, for example What rustc gives
|
f11fa4b to
6316989
Compare
| return; | ||
| } | ||
| // Multiple lint level attributes may share the same `reason` | ||
| if attr.has_name(sym::reason) { |
There was a problem hiding this comment.
This should be inside a code path that knows that the containing attribute is a lint level attribute, not applying everywhere.
Perhaps checking parent would do the trick? Or maybe check_duplicated_attr should separately take an enum of the syntactic contexts it can process, something like:
enum Context {
/// top level or cfg_attr()
Attribute,
/// inside of allow() deny() etc
LintName,
}That would do the same thing as checking parent but be more efficient and likely easier to get right, at the price of writing more code.
There’s also a terminology problem that perpetuates the confusion this lint used to have: this attr is in general not an attribute — it’s a meta item that might be part of an attribute. (For example, allow(...) and doc = "..." are attributes, but clippy::pedantic and reason = "..." are not attributes, only meta items that appear in specific attributes.)
I’m not sure whether it’s worth renaming the variable and other terms here, but it might help.
There was a problem hiding this comment.
Thanks! looks like the enum actually reduces code and complexity.
59b1226 to
d2e46b1
Compare
d2e46b1 to
37d1b47
Compare
Fixes #13238
Previously, PR #13355 (partly) addressed similar issues, @kpreid identified the cause, I hope this fix matches the intention outlined there.
changelog: Limit duplicated_attribute to lint-level attributes to reduce false positives and overlaps with rustc