Skip to content

Commit 6c48ef3

Browse files
committed
Auto merge of rust-lang#11557 - Alexendoo:dev-new-lint-msrv-test, r=Manishearth
Add msrv test template for `cargo dev new_lint --msrv` changelog: none
2 parents 78ddc8d + 39f7f69 commit 6c48ef3

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

clippy_dev/src/new_lint.rs

+51-12
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn create(
5858
};
5959

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

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

91-
fn create_test(lint: &LintData<'_>) -> io::Result<()> {
92-
fn create_project_layout<P: Into<PathBuf>>(lint_name: &str, location: P, case: &str, hint: &str) -> io::Result<()> {
91+
fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
92+
fn create_project_layout<P: Into<PathBuf>>(
93+
lint_name: &str,
94+
location: P,
95+
case: &str,
96+
hint: &str,
97+
msrv: bool,
98+
) -> io::Result<()> {
9399
let mut path = location.into().join(case);
94100
fs::create_dir(&path)?;
95101
write_file(path.join("Cargo.toml"), get_manifest_contents(lint_name, hint))?;
96102

97103
path.push("src");
98104
fs::create_dir(&path)?;
99-
write_file(path.join("main.rs"), get_test_file_contents(lint_name))?;
105+
write_file(path.join("main.rs"), get_test_file_contents(lint_name, msrv))?;
100106

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

109-
create_project_layout(lint.name, &test_dir, "fail", "Content that triggers the lint goes here")?;
110-
create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")?;
115+
create_project_layout(
116+
lint.name,
117+
&test_dir,
118+
"fail",
119+
"Content that triggers the lint goes here",
120+
msrv,
121+
)?;
122+
create_project_layout(
123+
lint.name,
124+
&test_dir,
125+
"pass",
126+
"This file should not trigger the lint",
127+
false,
128+
)?;
111129

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

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

197-
fn get_test_file_contents(lint_name: &str) -> String {
198-
formatdoc!(
215+
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
216+
let mut test = formatdoc!(
199217
r#"
200218
#![warn(clippy::{lint_name})]
201219
202220
fn main() {{
203221
// test code goes here
204222
}}
205223
"#
206-
)
224+
);
225+
226+
if msrv {
227+
let _ = writedoc!(
228+
test,
229+
r#"
230+
231+
// TODO: set xx to the version one below the MSRV used by the lint, and yy to
232+
// the version used by the lint
233+
#[clippy::msrv = "1.xx"]
234+
fn msrv_1_xx() {{
235+
// a simple example that would trigger the lint if the MSRV were met
236+
}}
237+
238+
#[clippy::msrv = "1.yy"]
239+
fn msrv_1_yy() {{
240+
// the same example as above
241+
}}
242+
"#
243+
);
244+
}
245+
246+
test
207247
}
208248

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

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

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

0 commit comments

Comments
 (0)