Skip to content

Commit 750b719

Browse files
committed
Document new clippy::version attribute and make it mandatory
1 parent 3eb20d2 commit 750b719

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

clippy_dev/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ opener = "0.5"
1212
regex = "1.5"
1313
shell-escape = "0.1"
1414
walkdir = "2.3"
15+
cargo_metadata = "0.12"
1516

1617
[features]
1718
deny-warnings = []

clippy_dev/src/new_lint.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ fn to_camel_case(name: &str) -> String {
104104
.collect()
105105
}
106106

107+
fn get_stabilisation_version() -> String {
108+
let mut command = cargo_metadata::MetadataCommand::new();
109+
command.no_deps();
110+
if let Ok(metadata) = command.exec() {
111+
if let Some(pkg) = metadata.packages.iter().find(|pkg| pkg.name == "clippy") {
112+
return format!("{}.{}.0", pkg.version.minor, pkg.version.patch);
113+
}
114+
}
115+
116+
String::from("<TODO set version(see doc/adding_lints.md)")
117+
}
118+
107119
fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
108120
let mut contents = format!(
109121
indoc! {"
@@ -150,6 +162,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
150162
},
151163
};
152164

165+
let version = get_stabilisation_version();
153166
let lint_name = lint.name;
154167
let pass_name = lint.pass;
155168
let category = lint.category;
@@ -185,7 +198,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
185198
});
186199

187200
result.push_str(&format!(
188-
indoc! {"
201+
indoc! {r#"
189202
declare_clippy_lint! {{
190203
/// ### What it does
191204
///
@@ -199,11 +212,13 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
199212
/// ```rust
200213
/// // example code which does not raise clippy warning
201214
/// ```
215+
#[clippy::version = "{version}"]
202216
pub {name_upper},
203217
{category},
204-
\"default lint description\"
218+
"default lint description"
205219
}}
206-
"},
220+
"#},
221+
version = version,
207222
name_upper = name_upper,
208223
category = category,
209224
));

clippy_dev/src/update_lints.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static DEC_CLIPPY_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
1818
r#"(?x)
1919
declare_clippy_lint!\s*[\{(]
2020
(?:\s+///.*)*
21-
(?:\s*\#\[clippy::version\s*=\s*"[^"]*"\])?
21+
(?:\s*\#\[clippy::version\s*=\s*"[^"]*"\])
2222
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
2323
(?P<cat>[a-z_]+)\s*,\s*
2424
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]

clippy_lints/src/utils/internal_lints/metadata_collector.rs

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
//! The module transforms all lint names to ascii lowercase to ensure that we don't have mismatches
88
//! during any comparison or mapping. (Please take care of this, it's not fun to spend time on such
99
//! a simple mistake)
10+
//!
11+
//! ---
12+
//!
13+
//! So while implementing the version attribute we needed to get all versions that a lint was
14+
//! introduced... and I wrote a script for nushell, my first script.. It's not pretty but special
15+
//! ```nu
16+
//! ls | where name =~ "rust-" | select name | format {name}/lints.json | each { open $it | select id | insert version $it | str substring "5,11" version} | group-by id | rotate counter-clockwise id version | update version {get version | first 1} | flatten | select id version | save "lint-version.csv"
17+
//! ```
18+
//! And it works, just run it on the `gh-pages` branch to get a beautiful csv file.
1019
1120
use if_chain::if_chain;
1221
use rustc_ast as ast;

doc/adding_lints.md

+6
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ declare_clippy_lint! {
188188
/// ```rust
189189
/// // example code
190190
/// ```
191+
#[clippy::version = "1.29.0"]
191192
pub FOO_FUNCTIONS,
192193
pedantic,
193194
"function named `foo`, which is not a descriptive name"
@@ -198,6 +199,10 @@ declare_clippy_lint! {
198199
section. This is the default documentation style and will be displayed
199200
[like this][example_lint_page]. To render and open this documentation locally
200201
in a browser, run `cargo dev serve`.
202+
* The `#[clippy::version]` attribute will be rendered as part of the lint documentation.
203+
The value should be set to the current Rust version that the lint is developed in,
204+
it can be retrieved by running `rustc -vV` in the rust-clippy directory. The version
205+
is listed under *release*. (Use the version without the `-nightly`) suffix.
201206
* `FOO_FUNCTIONS` is the name of our lint. Be sure to follow the
202207
[lint naming guidelines][lint_naming] here when naming your lint.
203208
In short, the name should state the thing that is being checked for and
@@ -500,6 +505,7 @@ declare_clippy_lint! {
500505
/// // Good
501506
/// Insert a short example of improved code that doesn't trigger the lint
502507
/// ```
508+
#[clippy::version = "1.29.0"]
503509
pub FOO_FUNCTIONS,
504510
pedantic,
505511
"function named `foo`, which is not a descriptive name"

0 commit comments

Comments
 (0)