about summary refs log tree commit diff
path: root/clippy_lints
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-10-11 14:10:01 +0000
committerbors <bors@rust-lang.org>2024-10-11 14:10:01 +0000
commit8125cd5c2af8f609577c7d3b740a859fdde40c6c (patch)
tree9e73aad837cd77bd3a666c7d5e4e9d17e439e600 /clippy_lints
parent47903dbf972ea86009d35059002362b1bc3b9121 (diff)
parent13e2633f193838eadcddd5bf5842b9ce0a1f17dd (diff)
Auto merge of #13359 - blyxyas:declare_clippy_macro, r=Alexendoo
Turn declare_clippy_lint into a declarative macro

Ease of development, and hopefully compile times (the dependencies are still there because of ui-test). The procedural macro was doing just some very basic processing (like assigning a lint level to each category), so it didn't have a reason to stay IMO

changelog: None
Diffstat (limited to 'clippy_lints')
-rw-r--r--clippy_lints/Cargo.toml1
-rw-r--r--clippy_lints/src/declare_clippy_lint.rs162
-rw-r--r--clippy_lints/src/lib.rs11
3 files changed, 169 insertions, 5 deletions
diff --git a/clippy_lints/Cargo.toml b/clippy_lints/Cargo.toml
index d1188940b46..55f1d31b4ad 100644
--- a/clippy_lints/Cargo.toml
+++ b/clippy_lints/Cargo.toml
@@ -13,7 +13,6 @@ arrayvec = { version = "0.7", default-features = false }
 cargo_metadata = "0.18"
 clippy_config = { path = "../clippy_config" }
 clippy_utils = { path = "../clippy_utils" }
-declare_clippy_lint = { path = "../declare_clippy_lint" }
 itertools = "0.12"
 quine-mc_cluskey = "0.2"
 regex-syntax = "0.8"
diff --git a/clippy_lints/src/declare_clippy_lint.rs b/clippy_lints/src/declare_clippy_lint.rs
new file mode 100644
index 00000000000..b1e39c70baa
--- /dev/null
+++ b/clippy_lints/src/declare_clippy_lint.rs
@@ -0,0 +1,162 @@
+#[macro_export]
+#[allow(clippy::crate_in_macro_def)]
+macro_rules! declare_clippy_lint {
+    (@
+        $(#[doc = $lit:literal])*
+        pub $lint_name:ident,
+        $category:ident,
+        $lintcategory:expr,
+        $desc:literal,
+        $version_expr:expr,
+        $version_lit:literal
+    ) => {
+        rustc_session::declare_tool_lint! {
+            $(#[doc = $lit])*
+            #[clippy::version = $version_lit]
+            pub clippy::$lint_name,
+            $category,
+            $desc,
+            report_in_external_macro:true
+        }
+
+        pub(crate) static ${concat($lint_name, _INFO)}: &'static crate::LintInfo = &crate::LintInfo {
+            lint: &$lint_name,
+            category:  $lintcategory,
+            explanation: concat!($($lit,"\n",)*),
+            location: concat!(file!(), "#L", line!()),
+            version: $version_expr
+        };
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        restriction,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Allow, crate::LintCategory::Restriction, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        style,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Warn, crate::LintCategory::Style, $desc,
+            Some($version), $version
+
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        correctness,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Deny, crate::LintCategory::Correctness, $desc,
+            Some($version), $version
+
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        perf,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Warn, crate::LintCategory::Perf, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        complexity,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Warn, crate::LintCategory::Complexity, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        suspicious,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Warn, crate::LintCategory::Suspicious, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        nursery,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Allow, crate::LintCategory::Nursery, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        pedantic,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Allow, crate::LintCategory::Pedantic, $desc,
+            Some($version), $version
+        }
+    };
+    (
+        $(#[doc = $lit:literal])*
+        #[clippy::version = $version:literal]
+        pub $lint_name:ident,
+        cargo,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Allow, crate::LintCategory::Cargo, $desc,
+            Some($version), $version
+        }
+    };
+
+    (
+        $(#[doc = $lit:literal])*
+        pub $lint_name:ident,
+        internal,
+        $desc:literal
+    ) => {
+        declare_clippy_lint! {@
+            $(#[doc = $lit])*
+            pub $lint_name, Allow, crate::LintCategory::Internal, $desc,
+            None, "0.0.0"
+        }
+    };
+}
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 2eb6d99b761..cee8dd9b682 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -1,6 +1,7 @@
 #![feature(array_windows)]
 #![feature(binary_heap_into_iter_sorted)]
 #![feature(box_patterns)]
+#![feature(macro_metavar_expr_concat)]
 #![feature(control_flow_enum)]
 #![feature(f128)]
 #![feature(f16)]
@@ -59,9 +60,10 @@ extern crate rustc_trait_selection;
 extern crate thin_vec;
 
 #[macro_use]
-extern crate clippy_utils;
+mod declare_clippy_lint;
+
 #[macro_use]
-extern crate declare_clippy_lint;
+extern crate clippy_utils;
 
 #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
 mod utils;
@@ -394,7 +396,7 @@ mod zero_sized_map_values;
 mod zombie_processes;
 // end lints modules, do not remove this comment, it’s used in `update_lints`
 
-use clippy_config::{Conf, get_configuration_metadata};
+use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
 use clippy_utils::macros::FormatArgsStorage;
 use rustc_data_structures::fx::FxHashSet;
 use rustc_lint::{Lint, LintId};
@@ -522,8 +524,9 @@ impl LintInfo {
 
 pub fn explain(name: &str) -> i32 {
     let target = format!("clippy::{}", name.to_ascii_uppercase());
+
     if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
-        println!("{}", info.explanation);
+        println!("{}", sanitize_explanation(info.explanation));
         // Check if the lint has configuration
         let mut mdconf = get_configuration_metadata();
         let name = name.to_ascii_lowercase();