about summary refs log tree commit diff
path: root/clippy_dev/src
diff options
context:
space:
mode:
authorflip1995 <philipp.krones@embecosm.com>2021-12-06 12:33:31 +0100
committerflip1995 <philipp.krones@embecosm.com>2021-12-06 12:33:31 +0100
commit8fea1d94f3bbcc02c3822dd43da9a1133e90f715 (patch)
treec571ad0e3fb19726b5e0fbe9df641885ddf0524d /clippy_dev/src
parent35a0060aba4bc9ff4e2b8faa20b91762c3109b18 (diff)
downloadrust-8fea1d94f3bbcc02c3822dd43da9a1133e90f715.tar.gz
rust-8fea1d94f3bbcc02c3822dd43da9a1133e90f715.zip
Merge commit 'a5d597637dcb78dc73f93561ce474f23d4177c35' into clippyup
Diffstat (limited to 'clippy_dev/src')
-rw-r--r--clippy_dev/src/fmt.rs44
-rw-r--r--clippy_dev/src/lib.rs1
-rw-r--r--clippy_dev/src/lint.rs20
-rw-r--r--clippy_dev/src/main.rs15
-rw-r--r--clippy_dev/src/new_lint.rs21
-rw-r--r--clippy_dev/src/update_lints.rs5
6 files changed, 84 insertions, 22 deletions
diff --git a/clippy_dev/src/fmt.rs b/clippy_dev/src/fmt.rs
index c81eb40d52f..9ceadee58ea 100644
--- a/clippy_dev/src/fmt.rs
+++ b/clippy_dev/src/fmt.rs
@@ -1,6 +1,7 @@
 use crate::clippy_project_root;
+use itertools::Itertools;
 use shell_escape::escape;
-use std::ffi::OsStr;
+use std::ffi::{OsStr, OsString};
 use std::path::Path;
 use std::process::{self, Command};
 use std::{fs, io};
@@ -56,15 +57,22 @@ pub fn run(check: bool, verbose: bool) {
         success &= cargo_fmt(context, &project_root.join("rustc_tools_util"))?;
         success &= cargo_fmt(context, &project_root.join("lintcheck"))?;
 
-        for entry in WalkDir::new(project_root.join("tests")) {
-            let entry = entry?;
-            let path = entry.path();
-
-            if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" {
-                continue;
-            }
-
-            success &= rustfmt(context, path)?;
+        let chunks = WalkDir::new(project_root.join("tests"))
+            .into_iter()
+            .filter_map(|entry| {
+                let entry = entry.expect("failed to find tests");
+                let path = entry.path();
+
+                if path.extension() != Some("rs".as_ref()) || entry.file_name() == "ice-3891.rs" {
+                    None
+                } else {
+                    Some(entry.into_path().into_os_string())
+                }
+            })
+            .chunks(250);
+
+        for chunk in &chunks {
+            success &= rustfmt(context, chunk)?;
         }
 
         Ok(success)
@@ -149,7 +157,7 @@ fn exec(
 }
 
 fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
-    let mut args = vec!["+nightly", "fmt", "--all"];
+    let mut args = vec!["fmt", "--all"];
     if context.check {
         args.push("--");
         args.push("--check");
@@ -162,7 +170,7 @@ fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
 fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
     let program = "rustfmt";
     let dir = std::env::current_dir()?;
-    let args = &["+nightly", "--version"];
+    let args = &["--version"];
 
     if context.verbose {
         println!("{}", format_command(&program, &dir, args));
@@ -185,14 +193,14 @@ fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
     }
 }
 
-fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
-    let mut args = vec!["+nightly".as_ref(), path.as_os_str()];
+fn rustfmt(context: &FmtContext, paths: impl Iterator<Item = OsString>) -> Result<bool, CliError> {
+    let mut args = Vec::new();
     if context.check {
-        args.push("--check".as_ref());
+        args.push(OsString::from("--check"));
     }
+    args.extend(paths);
+
     let success = exec(context, "rustfmt", std::env::current_dir()?, &args)?;
-    if !success {
-        eprintln!("rustfmt failed on {}", path.display());
-    }
+
     Ok(success)
 }
diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs
index 5538f62c8e7..59fde447547 100644
--- a/clippy_dev/src/lib.rs
+++ b/clippy_dev/src/lib.rs
@@ -7,6 +7,7 @@ use std::path::PathBuf;
 
 pub mod bless;
 pub mod fmt;
+pub mod lint;
 pub mod new_lint;
 pub mod serve;
 pub mod setup;
diff --git a/clippy_dev/src/lint.rs b/clippy_dev/src/lint.rs
new file mode 100644
index 00000000000..dfd16f71054
--- /dev/null
+++ b/clippy_dev/src/lint.rs
@@ -0,0 +1,20 @@
+use std::process::{self, Command};
+
+pub fn run(filename: &str) {
+    let code = Command::new("cargo")
+        .args(["run", "--bin", "clippy-driver", "--"])
+        .args(["-L", "./target/debug"])
+        .args(["-Z", "no-codegen"])
+        .args(["--edition", "2021"])
+        .arg(filename)
+        .env("__CLIPPY_INTERNAL_TESTS", "true")
+        .status()
+        .expect("failed to run cargo")
+        .code();
+
+    if code.is_none() {
+        eprintln!("Killed by signal");
+    }
+
+    process::exit(code.unwrap_or(1));
+}
diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs
index b5c04efce3b..30a241c8ba1 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::{App, AppSettings, Arg, ArgMatches, SubCommand};
-use clippy_dev::{bless, fmt, new_lint, serve, setup, update_lints};
+use clippy_dev::{bless, fmt, lint, new_lint, serve, setup, update_lints};
 fn main() {
     let matches = get_clap_config();
 
@@ -55,6 +55,10 @@ fn main() {
             let lint = matches.value_of("lint");
             serve::run(port, lint);
         },
+        ("lint", Some(matches)) => {
+            let filename = matches.value_of("filename").unwrap();
+            lint::run(filename);
+        },
         _ => {},
     }
 }
@@ -219,5 +223,14 @@ fn get_clap_config<'a>() -> ArgMatches<'a> {
                 )
                 .arg(Arg::with_name("lint").help("Which lint's page to load initially (optional)")),
         )
+        .subcommand(
+            SubCommand::with_name("lint")
+                .about("Manually run clippy on a file")
+                .arg(
+                    Arg::with_name("filename")
+                        .required(true)
+                        .help("The path to a file to lint"),
+                ),
+        )
         .get_matches()
 }
diff --git a/clippy_dev/src/new_lint.rs b/clippy_dev/src/new_lint.rs
index 43a478ee77d..59658b42c79 100644
--- a/clippy_dev/src/new_lint.rs
+++ b/clippy_dev/src/new_lint.rs
@@ -132,6 +132,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! {"
@@ -178,6 +190,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
         },
     };
 
+    let version = get_stabilisation_version();
     let lint_name = lint.name;
     let category = lint.category;
     let name_camel = to_camel_case(lint.name);
@@ -212,7 +225,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
                 ///
@@ -226,11 +239,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,
     ));
diff --git a/clippy_dev/src/update_lints.rs b/clippy_dev/src/update_lints.rs
index 23f58bc4915..8dd073ef405 100644
--- a/clippy_dev/src/update_lints.rs
+++ b/clippy_dev/src/update_lints.rs
@@ -18,6 +18,7 @@ static DEC_CLIPPY_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
         r#"(?x)
     declare_clippy_lint!\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*[})]
@@ -31,6 +32,7 @@ static DEC_DEPRECATED_LINT_RE: SyncLazy<Regex> = SyncLazy::new(|| {
         r#"(?x)
     declare_deprecated_lint!\s*[{(]\s*
     (?:\s+///.*)*
+    (?:\s*\#\[clippy::version\s*=\s*"[^"]*"\])?
     \s+pub\s+(?P<name>[A-Z_][A-Z_0-9]*)\s*,\s*
     "(?P<desc>(?:[^"\\]+|\\(?s).(?-s))*)"\s*[})]
 "#,
@@ -495,6 +497,7 @@ fn test_parse_contents() {
     let result: Vec<Lint> = parse_contents(
         r#"
 declare_clippy_lint! {
+    #[clippy::version = "Hello Clippy!"]
     pub PTR_ARG,
     style,
     "really long \
@@ -502,6 +505,7 @@ declare_clippy_lint! {
 }
 
 declare_clippy_lint!{
+    #[clippy::version = "Test version"]
     pub DOC_MARKDOWN,
     pedantic,
     "single line"
@@ -509,6 +513,7 @@ declare_clippy_lint!{
 
 /// some doc comment
 declare_deprecated_lint! {
+    #[clippy::version = "I'm a version"]
     pub SHOULD_ASSERT_EQ,
     "`assert!()` will be more flexible with RFC 2011"
 }