X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;ds=sidebyside;f=doc%2Fadding_lints.md;h=004eb28b44640fb59ab3d43c31203ab97c622870;hb=2a8d7bd0dd69ab1fe2a2af0f2984959f62b5631c;hp=99b86953d51a6287ceccb8bcb9207d881b0f368e;hpb=4be72b0936955b5820ca327fd9370303ccd9ef67;p=rust.git diff --git a/doc/adding_lints.md b/doc/adding_lints.md index 99b86953d51..004eb28b446 100644 --- a/doc/adding_lints.md +++ b/doc/adding_lints.md @@ -11,6 +11,7 @@ because that's clearly a non-descriptive name. - [Setup](#setup) - [Getting Started](#getting-started) - [Testing](#testing) + - [Cargo lints](#cargo-lints) - [Rustfix tests](#rustfix-tests) - [Edition 2018 tests](#edition-2018-tests) - [Testing manually](#testing-manually) @@ -179,14 +180,11 @@ the auto-generated lint declaration to have a real description, something like t ```rust declare_clippy_lint! { - /// **What it does:** + /// ### What it does /// - /// **Why is this bad?** - /// - /// **Known problems:** None. - /// - /// **Example:** + /// ### Why is this bad? /// + /// ### Example /// ```rust /// // example code /// ``` @@ -390,17 +388,23 @@ pass. ## 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, +Sometimes a lint makes suggestions that require a certain version of Rust. For example, the `manual_strip` lint suggests +using `str::strip_prefix` and `str::strip_suffix` which is only available after Rust 1.45. In such cases, you need to +ensure that the MSRV configured for the project is >= the MSRV of the required Rust feature. If multiple features are +required, just use the one with a lower MSRV. + +First, add an MSRV alias for the required feature in [`clippy_utils::msrvs`](/clippy_utils/src/msrvs.rs). This can be +accessed later as `msrvs::STR_STRIP_PREFIX`, for example. ```rust -const MANUAL_STRIP_MSRV: RustcVersion = RustcVersion::new(1, 45, 0); +msrv_aliases! { + .. + 1,45,0 { STR_STRIP_PREFIX } +} ``` -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` +In order to access the project-configured MSRV, you need to have an `msrv` field in the LintPass struct, and a +constructor to initialize the field. The `msrv` value is passed to the constructor in `clippy_lints/lib.rs`. ```rust pub struct ManualStrip { @@ -415,11 +419,11 @@ impl ManualStrip { } ``` -The project's MSRV can then be matched against the lint's `msrv` in the LintPass +The project's MSRV can then be matched against the feature MSRV in the LintPass using the `meets_msrv` utility function. ``` rust -if !meets_msrv(self.msrv.as_ref(), &MANUAL_STRIP_MSRV) { +if !meets_msrv(self.msrv.as_ref(), &msrvs::STR_STRIP_PREFIX) { return; } ``` @@ -448,7 +452,7 @@ in `clippy_lints/src/utils/conf.rs`: ```rust define_Conf! { /// Lint: LIST, OF, LINTS, . The minimum rust version that the project supports - (msrv, "msrv": Option, None), + (msrv: Option = None), ... } ``` @@ -481,13 +485,13 @@ Please document your lint with a doc comment akin to the following: ```rust declare_clippy_lint! { - /// **What it does:** Checks for ... (describe what the lint matches). - /// - /// **Why is this bad?** Supply the reason for linting the code. + /// ### What it does + /// Checks for ... (describe what the lint matches). /// - /// **Known problems:** None. (Or describe where it could go wrong.) + /// ### Why is this bad? + /// Supply the reason for linting the code. /// - /// **Example:** + /// ### Example /// /// ```rust,ignore /// // Bad @@ -552,14 +556,15 @@ directory. Adding a configuration to a lint can be useful for thresholds or to c behavior that can be seen as a false positive for some users. Adding a configuration is done in the following steps: -1. Adding a new configuration entry to [clippy_utils::conf](/clippy_utils/src/conf.rs) +1. Adding a new configuration entry to [clippy_lints::utils::conf](/clippy_lints/src/utils/conf.rs) like this: ```rust - /// Lint: LINT_NAME. - (configuration_ident, "configuration_value": Type, DefaultValue), + /// Lint: LINT_NAME. + /// + /// + (configuration_ident: Type = DefaultValue), ``` - The configuration value and identifier should usually be the same. The doc comment will be - automatically added to the lint documentation. + The doc comment will be automatically added to the lint documentation. 2. Adding the configuration value to the lint impl struct: 1. This first requires the definition of a lint impl struct. Lint impl structs are usually generated with the `declare_lint_pass!` macro. This struct needs to be defined manually @@ -625,7 +630,7 @@ in the following steps: Here are some pointers to things you are likely going to need for every lint: * [Clippy utils][utils] - Various helper functions. Maybe the function you need - is already in here (`implements_trait`, `match_path`, `snippet`, etc) + is already in here (`implements_trait`, `match_def_path`, `snippet`, etc) * [Clippy diagnostics][diagnostics] * [The `if_chain` macro][if_chain] * [`from_expansion`][from_expansion] and [`in_external_macro`][in_external_macro]