Skip to content

Commit 8df11e4

Browse files
committed
add instructions to include msrv in lints
1 parent a7cfffe commit 8df11e4

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

doc/adding_lints.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ store.register_early_pass(|| box foo_functions::FooFunctions);
226226
```
227227

228228
As one may expect, there is a corresponding `register_late_pass` method
229-
available as well. Without a call to one of `register_early_pass` or
229+
available as well. Without a call to one of `register_early_pass` or
230230
`register_late_pass`, the lint pass in question will not be run.
231231

232-
One reason that `cargo dev` does not automate this step is that multiple lints
232+
One reason that `cargo dev` does not automate this step is that multiple lints
233233
can use the same lint pass, so registering the lint pass may already be done
234234
when adding a new lint. Another reason that this step is not automated is that
235-
the order that the passes are registered determines the order the passes
235+
the order that the passes are registered determines the order the passes
236236
actually run, which in turn affects the order that any emitted lints are output
237237
in.
238238

@@ -380,6 +380,57 @@ pass.
380380
[`FnKind::Fn`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/visit/enum.FnKind.html#variant.Fn
381381
[ident]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/symbol/struct.Ident.html
382382

383+
## Specifying the lint's minimum supported Rust version (msrv)
384+
385+
Projects supporting older versions of Rust would need to disable a lint if it targets features
386+
present in later versions. Support for this can be added by specifying an msrv in your lint like so,
387+
388+
```rust
389+
const MANUAL_STRIP_MSRV: RustcVersion = RustcVersion::new(1, 45, 0);
390+
```
391+
392+
The project's msrv will also have to be an attribute in the lint so you'll have to add a struct
393+
and constructor for your lint. The project's msrv needs to be passed when the lint is registered
394+
in `lib.rs`
395+
396+
```rust
397+
pub struct ManualStrip {
398+
msrv: Option<RustcVersion>,
399+
}
400+
401+
impl ManualStrip {
402+
#[must_use]
403+
pub fn new(msrv: Option<RustcVersion>) -> Self {
404+
Self { msrv }
405+
}
406+
}
407+
```
408+
409+
The project's msrv can then be matched against the lint's msrv in the LintPass using the `meets_msrv` utility
410+
function.
411+
412+
``` rust
413+
if !meets_msrv(self.msrv.as_ref(), &MANUAL_STRIP_MSRV) {
414+
return;
415+
}
416+
```
417+
418+
The project's msrv can also be specified as an inner attribute, which overrides the value from
419+
`clippy.toml`. This can be accounted for using the `extract_msrv_attr!(LintContext)` macro and passing
420+
LateContext/EarlyContext.
421+
422+
```rust
423+
impl<'tcx> LateLintPass<'tcx> for ManualStrip {
424+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
425+
...
426+
}
427+
extract_msrv_attr!(LateContext);
428+
}
429+
```
430+
431+
Once the msrv is added to the lint, a relevant test case should be added to `tests/ui/min_rust_version_attr.rs`
432+
which verifies that the lint isn't emitted if the project's msrv is lower.
433+
383434
## Author lint
384435

385436
If you have trouble implementing your lint, there is also the internal `author`

0 commit comments

Comments
 (0)