mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-18 00:53:31 +00:00
add instructions to include msrv in lints
This commit is contained in:
parent
a7cfffef26
commit
8df11e431b
1 changed files with 54 additions and 3 deletions
|
@ -380,6 +380,57 @@ pass.
|
|||
[`FnKind::Fn`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/visit/enum.FnKind.html#variant.Fn
|
||||
[ident]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/symbol/struct.Ident.html
|
||||
|
||||
## Specifying the lint's minimum supported Rust version (msrv)
|
||||
|
||||
Projects supporting older versions of Rust would need to disable a lint if it targets features
|
||||
present in later versions. Support for this can be added by specifying an msrv in your lint like so,
|
||||
|
||||
```rust
|
||||
const MANUAL_STRIP_MSRV: RustcVersion = RustcVersion::new(1, 45, 0);
|
||||
```
|
||||
|
||||
The project's msrv will also have to be an attribute in the lint so you'll have to add a struct
|
||||
and constructor for your lint. The project's msrv needs to be passed when the lint is registered
|
||||
in `lib.rs`
|
||||
|
||||
```rust
|
||||
pub struct ManualStrip {
|
||||
msrv: Option<RustcVersion>,
|
||||
}
|
||||
|
||||
impl ManualStrip {
|
||||
#[must_use]
|
||||
pub fn new(msrv: Option<RustcVersion>) -> Self {
|
||||
Self { msrv }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The project's msrv can then be matched against the lint's msrv in the LintPass using the `meets_msrv` utility
|
||||
function.
|
||||
|
||||
``` rust
|
||||
if !meets_msrv(self.msrv.as_ref(), &MANUAL_STRIP_MSRV) {
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
The project's msrv can also be specified as an inner attribute, which overrides the value from
|
||||
`clippy.toml`. This can be accounted for using the `extract_msrv_attr!(LintContext)` macro and passing
|
||||
LateContext/EarlyContext.
|
||||
|
||||
```rust
|
||||
impl<'tcx> LateLintPass<'tcx> for ManualStrip {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
...
|
||||
}
|
||||
extract_msrv_attr!(LateContext);
|
||||
}
|
||||
```
|
||||
|
||||
Once the msrv is added to the lint, a relevant test case should be added to `tests/ui/min_rust_version_attr.rs`
|
||||
which verifies that the lint isn't emitted if the project's msrv is lower.
|
||||
|
||||
## Author lint
|
||||
|
||||
If you have trouble implementing your lint, there is also the internal `author`
|
||||
|
|
Loading…
Reference in a new issue