about summary refs log tree commit diff
path: root/clippy_dev
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2023-10-06 17:35:45 +0200
committerPhilipp Krones <hello@philkrones.com>2023-10-06 17:35:45 +0200
commit8ebed4cc1a2e21844c58f3b0bfbad1a069bb09b6 (patch)
tree917baf90e5136e5f6beca54bbc49ff775a1b9877 /clippy_dev
parent9de3e6c9281e284f81515345330fb74b0c4ad312 (diff)
downloadrust-8ebed4cc1a2e21844c58f3b0bfbad1a069bb09b6.tar.gz
rust-8ebed4cc1a2e21844c58f3b0bfbad1a069bb09b6.zip
Merge commit 'b105fb4c39bc1a010807a6c076193cef8d93c109' into clippyup
Diffstat (limited to 'clippy_dev')
-rw-r--r--clippy_dev/src/new_lint.rs63
1 files changed, 51 insertions, 12 deletions
diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs
index e64cf2c8749..be2386bb1d2 100644
--- a/clippy_dev/src/new_lint.rs
+++ b/clippy_dev/src/new_lint.rs
@@ -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")?;
@@ -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(())
     }
@@ -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}`");
@@ -194,8 +212,8 @@ 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})]
 
@@ -203,7 +221,29 @@ fn get_test_file_contents(lint_name: &str) -> String {
             // 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 {
@@ -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!(
@@ -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`
         "#
         )