about summary refs log tree commit diff
path: root/clippy_dev
diff options
context:
space:
mode:
authorPhilipp Krones <hello@philkrones.com>2023-07-02 14:35:19 +0200
committerPhilipp Krones <hello@philkrones.com>2023-07-02 14:59:02 +0200
commitcb3ecf7b792fdc4b00e61935b9e40ca836752492 (patch)
treea183611f56d9139413f6ab1c78c0619512d3c751 /clippy_dev
parentbb33e0343fe37815f6180a861619a9fca6771ce9 (diff)
downloadrust-cb3ecf7b792fdc4b00e61935b9e40ca836752492.tar.gz
rust-cb3ecf7b792fdc4b00e61935b9e40ca836752492.zip
Merge commit '37f4c1725d3fd7e9c3ffd8783246bc5589debc53' into clippyup
Diffstat (limited to 'clippy_dev')
-rw-r--r--clippy_dev/src/bless.rs60
-rw-r--r--clippy_dev/src/fmt.rs1
-rw-r--r--clippy_dev/src/lib.rs7
-rw-r--r--clippy_dev/src/main.rs10
-rw-r--r--clippy_dev/src/new_lint.rs15
5 files changed, 24 insertions, 69 deletions
diff --git a/clippy_dev/src/bless.rs b/clippy_dev/src/bless.rs
deleted file mode 100644
index 92b2771f3fe..00000000000
--- a/clippy_dev/src/bless.rs
+++ /dev/null
@@ -1,60 +0,0 @@
-//! `bless` updates the reference files in the repo with changed output files
-//! from the last test run.
-
-use crate::cargo_clippy_path;
-use std::ffi::OsStr;
-use std::fs;
-use std::path::{Path, PathBuf};
-use std::sync::LazyLock;
-use walkdir::{DirEntry, WalkDir};
-
-static CLIPPY_BUILD_TIME: LazyLock<Option<std::time::SystemTime>> =
-    LazyLock::new(|| cargo_clippy_path().metadata().ok()?.modified().ok());
-
-/// # Panics
-///
-/// Panics if the path to a test file is broken
-pub fn bless(ignore_timestamp: bool) {
-    let extensions = ["stdout", "stderr", "fixed"].map(OsStr::new);
-
-    WalkDir::new(build_dir())
-        .into_iter()
-        .map(Result::unwrap)
-        .filter(|entry| entry.path().extension().map_or(false, |ext| extensions.contains(&ext)))
-        .for_each(|entry| update_reference_file(&entry, ignore_timestamp));
-}
-
-fn update_reference_file(test_output_entry: &DirEntry, ignore_timestamp: bool) {
-    let test_output_path = test_output_entry.path();
-
-    let reference_file_name = test_output_entry.file_name().to_str().unwrap().replace(".stage-id", "");
-    let reference_file_path = Path::new("tests")
-        .join(test_output_path.strip_prefix(build_dir()).unwrap())
-        .with_file_name(reference_file_name);
-
-    // If the test output was not updated since the last clippy build, it may be outdated
-    if !ignore_timestamp && !updated_since_clippy_build(test_output_entry).unwrap_or(true) {
-        return;
-    }
-
-    let test_output_file = fs::read(test_output_path).expect("Unable to read test output file");
-    let reference_file = fs::read(&reference_file_path).unwrap_or_default();
-
-    if test_output_file != reference_file {
-        // If a test run caused an output file to change, update the reference file
-        println!("updating {}", reference_file_path.display());
-        fs::copy(test_output_path, &reference_file_path).expect("Could not update reference file");
-    }
-}
-
-fn updated_since_clippy_build(entry: &DirEntry) -> Option<bool> {
-    let clippy_build_time = (*CLIPPY_BUILD_TIME)?;
-    let modified = entry.metadata().ok()?.modified().ok()?;
-    Some(modified >= clippy_build_time)
-}
-
-fn build_dir() -> PathBuf {
-    let mut path = std::env::current_exe().unwrap();
-    path.set_file_name("test");
-    path
-}
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index 25623144181..ee559d45dd1 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -35,6 +35,7 @@ struct FmtContext {
 }
 
 // the "main" function of cargo dev fmt
+#[allow(clippy::missing_panics_doc)]
 pub fn run(check: bool, verbose: bool) {
     fn try_run(context: &FmtContext) -> Result<bool, CliError> {
         let mut success = true;
diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs
index 56a269288c0..4624451cff4 100644
--- a/clippy_dev/src/lib.rs
+++ b/clippy_dev/src/lib.rs
@@ -14,7 +14,6 @@ use std::io;
 use std::path::PathBuf;
 use std::process::{self, ExitStatus};
 
-pub mod bless;
 pub mod dogfood;
 pub mod fmt;
 pub mod lint;
@@ -29,6 +28,10 @@ static CARGO_CLIPPY_EXE: &str = "cargo-clippy";
 static CARGO_CLIPPY_EXE: &str = "cargo-clippy.exe";
 
 /// Returns the path to the `cargo-clippy` binary
+///
+/// # Panics
+///
+/// Panics if the path of current executable could not be retrieved.
 #[must_use]
 pub fn cargo_clippy_path() -> PathBuf {
     let mut path = std::env::current_exe().expect("failed to get current executable name");
@@ -61,6 +64,8 @@ pub fn clippy_project_root() -> PathBuf {
     panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
 }
 
+/// # Panics
+/// Panics if given command result was failed.
 pub fn exit_if_err(status: io::Result<ExitStatus>) {
     match status.expect("failed to run command").code() {
         Some(0) => {},
diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs
index c03fbe9d275..43eaccdf5a3 100644
--- a/clippy_dev/src/main.rs
+++ b/clippy_dev/src/main.rs
@@ -3,7 +3,7 @@
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
 use clap::{Arg, ArgAction, ArgMatches, Command};
-use clippy_dev::{bless, dogfood, fmt, lint, new_lint, serve, setup, update_lints};
+use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, update_lints};
 use indoc::indoc;
 use std::convert::Infallible;
 
@@ -11,8 +11,8 @@ fn main() {
     let matches = get_clap_config();
 
     match matches.subcommand() {
-        Some(("bless", matches)) => {
-            bless::bless(matches.get_flag("ignore-timestamp"));
+        Some(("bless", _)) => {
+            eprintln!("use `cargo bless` to automatically replace `.stderr` and `.fixed` files as tests are being run");
         },
         Some(("dogfood", matches)) => {
             dogfood::dogfood(
@@ -35,7 +35,7 @@ fn main() {
         },
         Some(("new_lint", matches)) => {
             match new_lint::create(
-                matches.get_one::<String>("pass"),
+                matches.get_one::<String>("pass").unwrap(),
                 matches.get_one::<String>("name"),
                 matches.get_one::<String>("category").map(String::as_str),
                 matches.get_one::<String>("type").map(String::as_str),
@@ -176,7 +176,7 @@ fn get_clap_config() -> ArgMatches {
                         .help("Specify whether the lint runs during the early or late pass")
                         .value_parser(["early", "late"])
                         .conflicts_with("type")
-                        .required_unless_present("type"),
+                        .default_value("late"),
                     Arg::new("name")
                         .short('n')
                         .long("name")
diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs
index 13a27703427..f11aa547bd7 100644
--- a/clippy_dev/src/new_lint.rs
+++ b/clippy_dev/src/new_lint.rs
@@ -36,8 +36,9 @@ impl<T> Context for io::Result<T> {
 /// # Errors
 ///
 /// This function errors out if the files couldn't be created or written to.
+#[allow(clippy::missing_panics_doc)]
 pub fn create(
-    pass: Option<&String>,
+    pass: &String,
     lint_name: Option<&String>,
     category: Option<&str>,
     mut ty: Option<&str>,
@@ -49,7 +50,7 @@ pub fn create(
     }
 
     let lint = LintData {
-        pass: pass.map_or("", String::as_str),
+        pass,
         name: lint_name.expect("`name` argument is validated by clap"),
         category: category.expect("`category` argument is validated by clap"),
         ty,
@@ -63,6 +64,14 @@ pub fn create(
         add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?;
     }
 
+    if pass == "early" {
+        println!(
+            "\n\
+            NOTE: Use a late pass unless you need something specific from\
+            an early pass, as they lack many features and utilities"
+        );
+    }
+
     Ok(())
 }
 
@@ -87,7 +96,7 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
 
         path.push("src");
         fs::create_dir(&path)?;
-        let header = format!("// compile-flags: --crate-name={lint_name}");
+        let header = format!("//@compile-flags: --crate-name={lint_name}");
         write_file(path.join("main.rs"), get_test_file_contents(lint_name, Some(&header)))?;
 
         Ok(())