@@ -33,6 +33,9 @@ crate, it will read the attibute and skip formatting `foo` (note that we make no
33
33
provision for reading the attribute or doing anything with it, that is all up to
34
34
the tool).
35
35
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
+
36
39
This RFC supersedes #1755 .
37
40
38
41
# Motivation
@@ -70,10 +73,16 @@ Rustfmt is mostly ready to move towards stabilisation, but requires some kind of
70
73
reasonable long-term solution and addresses the needs of some important tools
71
74
today.
72
75
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
+
73
80
74
81
# Guide-level explanation
75
82
[ guide-level-explanation ] : #guide-level-explanation
76
83
84
+ ### Attributes
85
+
77
86
This section assumes that attributes (e.g., ` #[test] ` ) have already been taught.
78
87
79
88
You can use attibutes in your crate to pass information to tools. For now, this
@@ -99,6 +108,21 @@ mod baz {
99
108
}
100
109
```
101
110
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
+
102
126
# Reference-level explanation
103
127
[ reference-level-explanation ] : #reference-level-explanation
104
128
@@ -127,6 +151,8 @@ tries to find a macro using the [macro name resolution rules](https://github.com
127
151
If this fails, then it reports a macro not found error. The compiler * may*
128
152
suggest mis-typed attributes (declared or built-in).
129
153
154
+ A similar opt-in mechanism will exist for lints.
155
+
130
156
131
157
## Proposed for immediate implementation
132
158
@@ -138,12 +164,12 @@ feature gate.
138
164
E.g., ` #[rustdoc::foo] ` will be permitted in stable Rust code; ` #[rustdoc] ` will
139
165
still be treated as a custom attribute.
140
166
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.
147
173
148
174
Given the earlier rules on name resolution, these attributes would shadow any
149
175
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
155
181
possible through compilation. This allows tools which plug into the compiler
156
182
(like Clippy) to observe these attributes on items during type checking, etc.
157
183
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
+
158
207
159
208
### Forward and backward compatability
160
209
@@ -174,13 +223,6 @@ term solution? One could imagine either leaving them implicit (similar to the
174
223
libraries prelude) or using warning cycles or an epoch to move them to explicit
175
224
opt-in.
176
225
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
-
184
226
185
227
# Drawbacks
186
228
[ drawbacks ] : #drawbacks
@@ -190,9 +232,9 @@ attributes (I consider this a feature, not a bug, but others may differ).
190
232
191
233
Some tools are clearly given special treatment.
192
234
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.
196
238
197
239
We are not planning any infrastructure to help tools use these attributes. That
198
240
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
216
258
217
259
Should we try and move some top-level attributes that are compiler-specific
218
260
(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