Skip to content

Commit 8125cd5

Browse files
committed
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
2 parents 47903db + 13e2633 commit 8125cd5

File tree

10 files changed

+196
-203
lines changed

10 files changed

+196
-203
lines changed

clippy_config/src/conf.rs

+24
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ impl ConfError {
9797
}
9898
}
9999

100+
// Remove code tags and code behind '# 's, as they are not needed for the lint docs and --explain
101+
pub fn sanitize_explanation(raw_docs: &str) -> String {
102+
// Remove tags and hidden code:
103+
let mut explanation = String::with_capacity(128);
104+
let mut in_code = false;
105+
for line in raw_docs.lines().map(str::trim) {
106+
if let Some(lang) = line.strip_prefix("```") {
107+
let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
108+
if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
109+
explanation += "```rust\n";
110+
} else {
111+
explanation += line;
112+
explanation.push('\n');
113+
}
114+
in_code = !in_code;
115+
} else if !(in_code && line.starts_with("# ")) {
116+
explanation += line;
117+
explanation.push('\n');
118+
}
119+
}
120+
121+
explanation
122+
}
123+
100124
macro_rules! wrap_option {
101125
() => {
102126
None

clippy_config/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ mod metadata;
2626
pub mod msrvs;
2727
pub mod types;
2828

29-
pub use conf::{Conf, get_configuration_metadata, lookup_conf_file};
29+
pub use conf::{Conf, get_configuration_metadata, lookup_conf_file, sanitize_explanation};
3030
pub use metadata::ClippyConfiguration;

clippy_lints/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ arrayvec = { version = "0.7", default-features = false }
1313
cargo_metadata = "0.18"
1414
clippy_config = { path = "../clippy_config" }
1515
clippy_utils = { path = "../clippy_utils" }
16-
declare_clippy_lint = { path = "../declare_clippy_lint" }
1716
itertools = "0.12"
1817
quine-mc_cluskey = "0.2"
1918
regex-syntax = "0.8"
+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#[macro_export]
2+
#[allow(clippy::crate_in_macro_def)]
3+
macro_rules! declare_clippy_lint {
4+
(@
5+
$(#[doc = $lit:literal])*
6+
pub $lint_name:ident,
7+
$category:ident,
8+
$lintcategory:expr,
9+
$desc:literal,
10+
$version_expr:expr,
11+
$version_lit:literal
12+
) => {
13+
rustc_session::declare_tool_lint! {
14+
$(#[doc = $lit])*
15+
#[clippy::version = $version_lit]
16+
pub clippy::$lint_name,
17+
$category,
18+
$desc,
19+
report_in_external_macro:true
20+
}
21+
22+
pub(crate) static ${concat($lint_name, _INFO)}: &'static crate::LintInfo = &crate::LintInfo {
23+
lint: &$lint_name,
24+
category: $lintcategory,
25+
explanation: concat!($($lit,"\n",)*),
26+
location: concat!(file!(), "#L", line!()),
27+
version: $version_expr
28+
};
29+
};
30+
(
31+
$(#[doc = $lit:literal])*
32+
#[clippy::version = $version:literal]
33+
pub $lint_name:ident,
34+
restriction,
35+
$desc:literal
36+
) => {
37+
declare_clippy_lint! {@
38+
$(#[doc = $lit])*
39+
pub $lint_name, Allow, crate::LintCategory::Restriction, $desc,
40+
Some($version), $version
41+
}
42+
};
43+
(
44+
$(#[doc = $lit:literal])*
45+
#[clippy::version = $version:literal]
46+
pub $lint_name:ident,
47+
style,
48+
$desc:literal
49+
) => {
50+
declare_clippy_lint! {@
51+
$(#[doc = $lit])*
52+
pub $lint_name, Warn, crate::LintCategory::Style, $desc,
53+
Some($version), $version
54+
55+
}
56+
};
57+
(
58+
$(#[doc = $lit:literal])*
59+
#[clippy::version = $version:literal]
60+
pub $lint_name:ident,
61+
correctness,
62+
$desc:literal
63+
) => {
64+
declare_clippy_lint! {@
65+
$(#[doc = $lit])*
66+
pub $lint_name, Deny, crate::LintCategory::Correctness, $desc,
67+
Some($version), $version
68+
69+
}
70+
};
71+
(
72+
$(#[doc = $lit:literal])*
73+
#[clippy::version = $version:literal]
74+
pub $lint_name:ident,
75+
perf,
76+
$desc:literal
77+
) => {
78+
declare_clippy_lint! {@
79+
$(#[doc = $lit])*
80+
pub $lint_name, Warn, crate::LintCategory::Perf, $desc,
81+
Some($version), $version
82+
}
83+
};
84+
(
85+
$(#[doc = $lit:literal])*
86+
#[clippy::version = $version:literal]
87+
pub $lint_name:ident,
88+
complexity,
89+
$desc:literal
90+
) => {
91+
declare_clippy_lint! {@
92+
$(#[doc = $lit])*
93+
pub $lint_name, Warn, crate::LintCategory::Complexity, $desc,
94+
Some($version), $version
95+
}
96+
};
97+
(
98+
$(#[doc = $lit:literal])*
99+
#[clippy::version = $version:literal]
100+
pub $lint_name:ident,
101+
suspicious,
102+
$desc:literal
103+
) => {
104+
declare_clippy_lint! {@
105+
$(#[doc = $lit])*
106+
pub $lint_name, Warn, crate::LintCategory::Suspicious, $desc,
107+
Some($version), $version
108+
}
109+
};
110+
(
111+
$(#[doc = $lit:literal])*
112+
#[clippy::version = $version:literal]
113+
pub $lint_name:ident,
114+
nursery,
115+
$desc:literal
116+
) => {
117+
declare_clippy_lint! {@
118+
$(#[doc = $lit])*
119+
pub $lint_name, Allow, crate::LintCategory::Nursery, $desc,
120+
Some($version), $version
121+
}
122+
};
123+
(
124+
$(#[doc = $lit:literal])*
125+
#[clippy::version = $version:literal]
126+
pub $lint_name:ident,
127+
pedantic,
128+
$desc:literal
129+
) => {
130+
declare_clippy_lint! {@
131+
$(#[doc = $lit])*
132+
pub $lint_name, Allow, crate::LintCategory::Pedantic, $desc,
133+
Some($version), $version
134+
}
135+
};
136+
(
137+
$(#[doc = $lit:literal])*
138+
#[clippy::version = $version:literal]
139+
pub $lint_name:ident,
140+
cargo,
141+
$desc:literal
142+
) => {
143+
declare_clippy_lint! {@
144+
$(#[doc = $lit])*
145+
pub $lint_name, Allow, crate::LintCategory::Cargo, $desc,
146+
Some($version), $version
147+
}
148+
};
149+
150+
(
151+
$(#[doc = $lit:literal])*
152+
pub $lint_name:ident,
153+
internal,
154+
$desc:literal
155+
) => {
156+
declare_clippy_lint! {@
157+
$(#[doc = $lit])*
158+
pub $lint_name, Allow, crate::LintCategory::Internal, $desc,
159+
None, "0.0.0"
160+
}
161+
};
162+
}

clippy_lints/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(array_windows)]
22
#![feature(binary_heap_into_iter_sorted)]
33
#![feature(box_patterns)]
4+
#![feature(macro_metavar_expr_concat)]
45
#![feature(control_flow_enum)]
56
#![feature(f128)]
67
#![feature(f16)]
@@ -59,9 +60,10 @@ extern crate rustc_trait_selection;
5960
extern crate thin_vec;
6061

6162
#[macro_use]
62-
extern crate clippy_utils;
63+
mod declare_clippy_lint;
64+
6365
#[macro_use]
64-
extern crate declare_clippy_lint;
66+
extern crate clippy_utils;
6567

6668
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
6769
mod utils;
@@ -394,7 +396,7 @@ mod zero_sized_map_values;
394396
mod zombie_processes;
395397
// end lints modules, do not remove this comment, it’s used in `update_lints`
396398

397-
use clippy_config::{Conf, get_configuration_metadata};
399+
use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
398400
use clippy_utils::macros::FormatArgsStorage;
399401
use rustc_data_structures::fx::FxHashSet;
400402
use rustc_lint::{Lint, LintId};
@@ -522,8 +524,9 @@ impl LintInfo {
522524

523525
pub fn explain(name: &str) -> i32 {
524526
let target = format!("clippy::{}", name.to_ascii_uppercase());
527+
525528
if let Some(info) = declared_lints::LINTS.iter().find(|info| info.lint.name == target) {
526-
println!("{}", info.explanation);
529+
println!("{}", sanitize_explanation(info.explanation));
527530
// Check if the lint has configuration
528531
let mut mdconf = get_configuration_metadata();
529532
let name = name.to_ascii_lowercase();

clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(f128)]
55
#![feature(f16)]
66
#![feature(if_let_guard)]
7+
#![feature(macro_metavar_expr_concat)]
78
#![feature(let_chains)]
89
#![feature(never_type)]
910
#![feature(rustc_private)]
@@ -129,7 +130,6 @@ use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
129130
use crate::higher::Range;
130131
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
131132
use crate::visitors::for_each_expr_without_closures;
132-
133133
use rustc_middle::hir::nested_filter;
134134

135135
#[macro_export]

declare_clippy_lint/Cargo.toml

-13
This file was deleted.

0 commit comments

Comments
 (0)