Skip to content

Commit

Permalink
Document new clippy::version attribute and make it mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Oct 19, 2021
1 parent 3eb20d2 commit 40877dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions clippy_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ opener = "0.5"
regex = "1.5"
shell-escape = "0.1"
walkdir = "2.3"
cargo_metadata = "0.12"

[features]
deny-warnings = []
21 changes: 18 additions & 3 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ fn to_camel_case(name: &str) -> String {
.collect()
}

fn get_stabilisation_version() -> String {
let mut command = cargo_metadata::MetadataCommand::new();
command.no_deps();
if let Ok(metadata) = command.exec() {
if let Some(pkg) = metadata.packages.iter().find(|pkg| pkg.name == "clippy") {
return format!("{}.{}.0", pkg.version.minor, pkg.version.patch);
}
}

String::from("<TODO set version(see doc/adding_lints.md)")
}

fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
let mut contents = format!(
indoc! {"
Expand Down Expand Up @@ -150,6 +162,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
},
};

let version = get_stabilisation_version();
let lint_name = lint.name;
let pass_name = lint.pass;
let category = lint.category;
Expand Down Expand Up @@ -185,7 +198,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
});

result.push_str(&format!(
indoc! {"
indoc! {r#"
declare_clippy_lint! {{
/// ### What it does
///
Expand All @@ -199,11 +212,13 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
/// ```rust
/// // example code which does not raise clippy warning
/// ```
#[clippy::version = "{version}"]
pub {name_upper},
{category},
\"default lint description\"
"default lint description"
}}
"},
"#},
version = version,
name_upper = name_upper,
category = category,
));
Expand Down
5 changes: 4 additions & 1 deletion clippy_dev/src/update_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static DEC_CLIPPY_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
r#"(?x)
declare_clippy_lint!\s*[\{(]
(?:\s+///.*)*
(?:\s*\#\[clippy::version\s*=\s*"[^"]*"\])?
(?:\s*\#\[clippy::version\s*=\s*"[^"]*"\])
\s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
(?P<cat>[a-z_]+)\s*,\s*
"(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
Expand Down Expand Up @@ -496,20 +496,23 @@ fn test_parse_contents() {
let result: Vec<Lint> = parse_contents(
r#"
declare_clippy_lint! {
#[clippy::version = "Hello Clippy!"]
pub PTR_ARG,
style,
"really long \
text"
}
declare_clippy_lint!{
#[clippy::version = "Test version"]
pub DOC_MARKDOWN,
pedantic,
"single line"
}
/// some doc comment
declare_deprecated_lint! {
#[clippy::version = "I'm a version"]
pub SHOULD_ASSERT_EQ,
"`assert!()` will be more flexible with RFC 2011"
}
Expand Down
6 changes: 6 additions & 0 deletions doc/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ declare_clippy_lint! {
/// ```rust
/// // example code
/// ```
#[clippy::version = "1.29.0"]
pub FOO_FUNCTIONS,
pedantic,
"function named `foo`, which is not a descriptive name"
Expand All @@ -198,6 +199,10 @@ declare_clippy_lint! {
section. This is the default documentation style and will be displayed
[like this][example_lint_page]. To render and open this documentation locally
in a browser, run `cargo dev serve`.
* The `#[clippy::version]` attribute will be rendered as part of the lint documentation.
The value should be set to the current Rust version that the lint is developed in,
it can be retrieved by running `rustc -vV` in the rust-clippy directory. The version
is listed under *release*. (Use the version without the `-nightly`) suffix.
* `FOO_FUNCTIONS` is the name of our lint. Be sure to follow the
[lint naming guidelines][lint_naming] here when naming your lint.
In short, the name should state the thing that is being checked for and
Expand Down Expand Up @@ -500,6 +505,7 @@ declare_clippy_lint! {
/// // Good
/// Insert a short example of improved code that doesn't trigger the lint
/// ```
#[clippy::version = "1.29.0"]
pub FOO_FUNCTIONS,
pedantic,
"function named `foo`, which is not a descriptive name"
Expand Down

0 comments on commit 40877dc

Please sign in to comment.