Skip to content

Commit

Permalink
Auto merge of rust-lang#11557 - Alexendoo:dev-new-lint-msrv-test, r=M…
Browse files Browse the repository at this point in the history
…anishearth

Add msrv test template for `cargo dev new_lint --msrv`

changelog: none
  • Loading branch information
bors committed Sep 25, 2023
2 parents 78ddc8d + 39f7f69 commit 6c48ef3
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions clippy_dev/src/new_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn create(
};

create_lint(&lint, msrv).context("Unable to create lint implementation")?;
create_test(&lint).context("Unable to create a test for the new lint")?;
create_test(&lint, msrv).context("Unable to create a test for the new lint")?;

if lint.ty.is_none() {
add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?;
Expand Down Expand Up @@ -88,15 +88,21 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
}
}

fn create_test(lint: &LintData<'_>) -> io::Result<()> {
fn create_project_layout<P: Into<PathBuf>>(lint_name: &str, location: P, case: &str, hint: &str) -> io::Result<()> {
fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
fn create_project_layout<P: Into<PathBuf>>(
lint_name: &str,
location: P,
case: &str,
hint: &str,
msrv: bool,
) -> io::Result<()> {
let mut path = location.into().join(case);
fs::create_dir(&path)?;
write_file(path.join("Cargo.toml"), get_manifest_contents(lint_name, hint))?;

path.push("src");
fs::create_dir(&path)?;
write_file(path.join("main.rs"), get_test_file_contents(lint_name))?;
write_file(path.join("main.rs"), get_test_file_contents(lint_name, msrv))?;

Ok(())
}
Expand All @@ -106,13 +112,25 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
let test_dir = lint.project_root.join(&relative_test_dir);
fs::create_dir(&test_dir)?;

create_project_layout(lint.name, &test_dir, "fail", "Content that triggers the lint goes here")?;
create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")?;
create_project_layout(
lint.name,
&test_dir,
"fail",
"Content that triggers the lint goes here",
msrv,
)?;
create_project_layout(
lint.name,
&test_dir,
"pass",
"This file should not trigger the lint",
false,
)?;

println!("Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`");
} else {
let test_path = format!("tests/ui/{}.rs", lint.name);
let test_contents = get_test_file_contents(lint.name);
let test_contents = get_test_file_contents(lint.name, msrv);
write_file(lint.project_root.join(&test_path), test_contents)?;

println!("Generated test file: `{test_path}`");
Expand Down Expand Up @@ -194,16 +212,38 @@ pub(crate) fn get_stabilization_version() -> String {
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
}

fn get_test_file_contents(lint_name: &str) -> String {
formatdoc!(
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
let mut test = formatdoc!(
r#"
#![warn(clippy::{lint_name})]
fn main() {{
// test code goes here
}}
"#
)
);

if msrv {
let _ = writedoc!(
test,
r#"
// TODO: set xx to the version one below the MSRV used by the lint, and yy to
// the version used by the lint
#[clippy::msrv = "1.xx"]
fn msrv_1_xx() {{
// a simple example that would trigger the lint if the MSRV were met
}}
#[clippy::msrv = "1.yy"]
fn msrv_1_yy() {{
// the same example as above
}}
"#
);
}

test
}

fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
Expand Down Expand Up @@ -258,7 +298,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
)
});

let _: fmt::Result = write!(result, "{}", get_lint_declaration(&name_upper, category));
let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));

result.push_str(&if enable_msrv {
formatdoc!(
Expand All @@ -281,7 +321,6 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
}}
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
"#
)
Expand Down

0 comments on commit 6c48ef3

Please sign in to comment.