Skip to content

Commit 7806190

Browse files
committed
Extend scoped attributes to lints
1 parent 3104ea0 commit 7806190

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

text/0000-tool-attributes.md

+60-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ crate, it will read the attibute and skip formatting `foo` (note that we make no
3333
provision for reading the attribute or doing anything with it, that is all up to
3434
the tool).
3535

36+
This RFC proposes a second mechanism for scoping lints for tools. Similar to
37+
attributes, we propose a subset of a hypothetical long-term solution.
38+
3639
This RFC supersedes #1755.
3740

3841
# Motivation
@@ -70,10 +73,16 @@ Rustfmt is mostly ready to move towards stabilisation, but requires some kind of
7073
reasonable long-term solution and addresses the needs of some important tools
7174
today.
7275

76+
Similarly, tools (e.g., Clippy) may want to use their own lints without the
77+
compiler warning about unused lints. E.g., we want a user to be able to write
78+
`#![allow(clippy::some_lint)]` in their crate without warning.
79+
7380

7481
# Guide-level explanation
7582
[guide-level-explanation]: #guide-level-explanation
7683

84+
### Attributes
85+
7786
This section assumes that attributes (e.g., `#[test]`) have already been taught.
7887

7988
You can use attibutes in your crate to pass information to tools. For now, this
@@ -99,6 +108,21 @@ mod baz {
99108
}
100109
```
101110

111+
### Lints
112+
113+
This section assumes lints have already been taught.
114+
115+
Lints can be defined hierarchically as a path, as well as just a single name.
116+
For example, `nonstandard_style::non_snake_case_functions` and
117+
`nonstandard_style::uppercase_variables`. Note this RFC is not proposing
118+
changing any existing lints, just extending the current lint naming system. Lint
119+
names cannot be imported using `use`.
120+
121+
Lints can be enforced by tools other than the compiler. For example, Clippy
122+
provides a large suite of lints to catch common mistakes and improve your Rust
123+
code. Lints for tools are prefixed with the tool name, e.g., `clippy::box_vec`.
124+
125+
102126
# Reference-level explanation
103127
[reference-level-explanation]: #reference-level-explanation
104128

@@ -127,6 +151,8 @@ tries to find a macro using the [macro name resolution rules](https://github.com
127151
If this fails, then it reports a macro not found error. The compiler *may*
128152
suggest mis-typed attributes (declared or built-in).
129153

154+
A similar opt-in mechanism will exist for lints.
155+
130156

131157
## Proposed for immediate implementation
132158

@@ -138,12 +164,12 @@ feature gate.
138164
E.g., `#[rustdoc::foo]` will be permitted in stable Rust code; `#[rustdoc]` will
139165
still be treated as a custom attribute.
140166

141-
The initial list of allowed prefixes is `rustc`, `rustdoc`, and `rls`. As tools
142-
are added to the distribution, they will be allowed as path prefixes in
143-
attributes. We expect to add `rustfmt` and `clippy` in the near future. Note
144-
that whether one of these names can be used does not depend on whether the
145-
relevant component is installed on the user's system; this is a simple,
146-
universal white list.
167+
The initial list of allowed prefixes is `rustc`, `rustdoc`, and `rls` (but see
168+
note below on activation). As tools are added to the distribution, they will be
169+
allowed as path prefixes in attributes. We expect to add `rustfmt` and `clippy`
170+
in the near future. Note that whether one of these names can be used does not
171+
depend on whether the relevant component is installed on the user's system; this
172+
is a simple, universal white list.
147173

148174
Given the earlier rules on name resolution, these attributes would shadow any
149175
attribute macro with the same name. This is not problematic because a macro
@@ -155,6 +181,29 @@ Tool-scoped attributes should be preserved by the compiler for as long as
155181
possible through compilation. This allows tools which plug into the compiler
156182
(like Clippy) to observe these attributes on items during type checking, etc.
157183

184+
Likewise, white-listed tools may be used a prefix for lints. So for example,
185+
`rustfmt::foo` and `clippy::bar` are both valid lint names, from the compiler's
186+
perspective.
187+
188+
189+
### Activation and unused attibutes/lints
190+
191+
For each name on the whitelist, it is indicated if the name is active for
192+
attributes or lints. A name is only activated if required. So for example,
193+
`rustdoc` will not be activated at all until it takes advantage of this feature.
194+
I expect `clippy` will be activated only for lints, and `rustfmt` only for
195+
attributes.
196+
197+
A tool that has an active name *must* check for unused lints/attibutes. For
198+
example, if `rustfmt` becomes active for attributes, and only recognises
199+
`rustfmt::skip`, it must produce a warning if a user uses `rustfmt::foo` in
200+
their code.
201+
202+
These two requirements together mean that we do not lose checking of unused
203+
attributes/lints in any circumstance and we can move to having the compiler
204+
check for unused attributes/lints as part of a possible long-term solution
205+
without introducing new warnings or errors.
206+
158207

159208
### Forward and backward compatability
160209

@@ -174,13 +223,6 @@ term solution? One could imagine either leaving them implicit (similar to the
174223
libraries prelude) or using warning cycles or an epoch to move them to explicit
175224
opt-in.
176225

177-
Another hazard is that if in the long-term solution, the compiler checks
178-
attribute paths for validity rather than only the prefix. For example, if
179-
Rustfmt only defines `skip`, then `#[rustfmt::foo]` would not give an error
180-
under this proposal, but would in the long term. Given that `#[rustfmt::foo]` is
181-
almost certainly a mistake today, it seems easy to migrate to a stricter system
182-
with a warning cycle.
183-
184226

185227
# Drawbacks
186228
[drawbacks]: #drawbacks
@@ -190,9 +232,9 @@ attributes (I consider this a feature, not a bug, but others may differ).
190232

191233
Some tools are clearly given special treatment.
192234

193-
We permit some useless attributes without warning (e.g., `#[rustfmt::foo]`,
194-
assuming Rustfmt does nothing with `foo`). Tools could (and probably should)
195-
warn or error on such attributes.
235+
We permit some useless attributes without warning from the compiler (e.g.,
236+
`#[rustfmt::foo]`, assuming Rustfmt does nothing with `foo`). However, tools
237+
should warn or error on such attributes.
196238

197239
We are not planning any infrastructure to help tools use these attributes. That
198240
seems fine for now, I imagine a long-term solution should include some library
@@ -216,3 +258,5 @@ Are there other tools that should be included on the whitelist (`#[test]` perhap
216258

217259
Should we try and move some top-level attributes that are compiler-specific
218260
(rather than language-specific) to use `#[rustc::]`? (E.g., `crate_type`).
261+
262+
How should the compiler expose path lints to lint plugins/lint tools?

0 commit comments

Comments
 (0)