Skip to content

Commit 6274ff4

Browse files
committed
Move internal lints to their own crate
1 parent c082bc2 commit 6274ff4

File tree

59 files changed

+336
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+336
-348
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ path = "src/driver.rs"
2323
[dependencies]
2424
clippy_config = { path = "clippy_config" }
2525
clippy_lints = { path = "clippy_lints" }
26+
clippy_internal_lints = { path = "clippy_internal_lints", optional = true }
2627
rustc_tools_util = "0.3.0"
2728
tempfile = { version = "3.3", optional = true }
2829
termize = "0.1"
@@ -51,9 +52,9 @@ tokio = { version = "1", features = ["io-util"] }
5152
rustc_tools_util = "0.3.0"
5253

5354
[features]
54-
deny-warnings = ["clippy_lints/deny-warnings"]
55-
integration = ["tempfile"]
56-
internal = ["clippy_lints/internal", "tempfile"]
55+
deny-warnings = ["clippy_lints/deny-warnings", "clippy_internal_lints?/deny-warnings"]
56+
integration = ["dep:tempfile"]
57+
internal = ["dep:clippy_internal_lints", "dep:tempfile"]
5758

5859
[package.metadata.rust-analyzer]
5960
# This package uses #[feature(rustc_private)]

clippy_deprecated_lints/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "clippy_deprecated_lints"
3+
version = "0.0.1"
4+
edition = "2021"

clippy_dev/src/update_lints.rs

Lines changed: 31 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ fn generate_lint_files(
4343
deprecated_lints: &[DeprecatedLint],
4444
renamed_lints: &[RenamedLint],
4545
) {
46-
let internal_lints = Lint::internal_lints(lints);
47-
let mut usable_lints = Lint::usable_lints(lints);
48-
usable_lints.sort_by_key(|lint| lint.name.clone());
46+
let mut lints = lints.to_owned();
47+
lints.sort_by_key(|lint| lint.name.clone());
4948

5049
replace_region_in_file(
5150
update_mode,
5251
Path::new("README.md"),
5352
"[There are over ",
5453
" lints included in this crate!]",
5554
|res| {
56-
write!(res, "{}", round_to_fifty(usable_lints.len())).unwrap();
55+
write!(res, "{}", round_to_fifty(lints.len())).unwrap();
5756
},
5857
);
5958

@@ -63,7 +62,7 @@ fn generate_lint_files(
6362
"[There are over ",
6463
" lints included in this crate!]",
6564
|res| {
66-
write!(res, "{}", round_to_fifty(usable_lints.len())).unwrap();
65+
write!(res, "{}", round_to_fifty(lints.len())).unwrap();
6766
},
6867
);
6968

@@ -73,7 +72,7 @@ fn generate_lint_files(
7372
"<!-- begin autogenerated links to lint list -->\n",
7473
"<!-- end autogenerated links to lint list -->",
7574
|res| {
76-
for lint in usable_lints
75+
for lint in lints
7776
.iter()
7877
.map(|l| &*l.name)
7978
.chain(deprecated_lints.iter().filter_map(|l| l.name.strip_prefix("clippy::")))
@@ -92,7 +91,7 @@ fn generate_lint_files(
9291
"// begin lints modules, do not remove this comment, it’s used in `update_lints`\n",
9392
"// end lints modules, do not remove this comment, it’s used in `update_lints`",
9493
|res| {
95-
for lint_mod in usable_lints.iter().map(|l| &l.module).unique().sorted() {
94+
for lint_mod in lints.iter().map(|l| &l.module).unique().sorted() {
9695
writeln!(res, "mod {lint_mod};").unwrap();
9796
}
9897
},
@@ -101,7 +100,7 @@ fn generate_lint_files(
101100
process_file(
102101
"clippy_lints/src/declared_lints.rs",
103102
update_mode,
104-
&gen_declared_lints(internal_lints.iter(), usable_lints.iter()),
103+
&gen_declared_lints(lints.iter()),
105104
);
106105

107106
let content = gen_deprecated_lints_test(deprecated_lints);
@@ -112,10 +111,9 @@ fn generate_lint_files(
112111
}
113112

114113
pub fn print_lints() {
115-
let (lint_list, _, _) = gather_all();
116-
let usable_lints = Lint::usable_lints(&lint_list);
117-
let usable_lint_count = usable_lints.len();
118-
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
114+
let (lints, _, _) = gather_all();
115+
let lint_count = lints.len();
116+
let grouped_by_lint_group = Lint::by_lint_group(lints.into_iter());
119117

120118
for (lint_group, mut lints) in grouped_by_lint_group {
121119
println!("\n## {lint_group}");
@@ -127,7 +125,7 @@ pub fn print_lints() {
127125
}
128126
}
129127

130-
println!("there are {usable_lint_count} lints");
128+
println!("there are {lint_count} lints");
131129
}
132130

133131
/// Runs the `rename_lint` command.
@@ -191,11 +189,14 @@ pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
191189
.into_iter()
192190
.map(Result::unwrap)
193191
.filter(|f| {
194-
let name = f.path().file_name();
195-
let ext = f.path().extension();
196-
(ext == Some(OsStr::new("rs")) || ext == Some(OsStr::new("fixed")))
197-
&& name != Some(OsStr::new("rename.rs"))
198-
&& name != Some(OsStr::new("deprecated_lints.rs"))
192+
let path = f.path();
193+
path.extension().is_some_and(|ext| ext == "rs" || ext == "fixed")
194+
&& !(path.file_name().is_some_and(|n| n == "lib.rs")
195+
&& path
196+
.parent()
197+
.and_then(|p| p.parent())
198+
.and_then(|p| p.file_name())
199+
.is_some_and(|name| name == "clippy_deprecated_lints"))
199200
})
200201
{
201202
rewrite_file(file.path(), |s| {
@@ -204,7 +205,7 @@ pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
204205
}
205206

206207
let version = crate::new_lint::get_stabilization_version();
207-
rewrite_file(Path::new("clippy_lints/src/deprecated_lints.rs"), |s| {
208+
rewrite_file(Path::new("clippy_deprecated_lints/src/lib.rs"), |s| {
208209
insert_at_marker(
209210
s,
210211
"// end renamed lints. used by `cargo dev rename_lint`",
@@ -289,10 +290,7 @@ pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
289290
&replacements[0..1]
290291
};
291292

292-
// Don't change `clippy_utils/src/renamed_lints.rs` here as it would try to edit the lint being
293-
// renamed.
294-
for (_, file) in clippy_lints_src_files().filter(|(rel_path, _)| rel_path != OsStr::new("deprecated_lints.rs"))
295-
{
293+
for (_, file) in clippy_lints_src_files() {
296294
rewrite_file(file.path(), |s| replace_ident_like(s, replacements));
297295
}
298296

@@ -306,7 +304,7 @@ pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
306304
/// Runs the `deprecate` command
307305
///
308306
/// This does the following:
309-
/// * Adds an entry to `deprecated_lints.rs`.
307+
/// * Adds an entry to `clippy_deprecated_lints/src/lib.rs`.
310308
/// * Removes the lint declaration (and the entire file if applicable)
311309
///
312310
/// # Panics
@@ -336,7 +334,7 @@ pub fn deprecate(name: &str, reason: &str) {
336334
mod_path
337335
};
338336

339-
let deprecated_lints_path = &*clippy_project_root().join("clippy_lints/src/deprecated_lints.rs");
337+
let deprecated_lints_path = &*clippy_project_root().join("clippy_deprecated_lints/src/lib.rs");
340338

341339
if remove_lint_declaration(stripped_name, &mod_path, &mut lints).unwrap_or(false) {
342340
let version = crate::new_lint::get_stabilization_version();
@@ -541,22 +539,6 @@ impl Lint {
541539
}
542540
}
543541

544-
/// Returns all non-deprecated lints and non-internal lints
545-
#[must_use]
546-
fn usable_lints(lints: &[Self]) -> Vec<Self> {
547-
lints
548-
.iter()
549-
.filter(|l| !l.group.starts_with("internal"))
550-
.cloned()
551-
.collect()
552-
}
553-
554-
/// Returns all internal lints
555-
#[must_use]
556-
fn internal_lints(lints: &[Self]) -> Vec<Self> {
557-
lints.iter().filter(|l| l.group == "internal").cloned().collect()
558-
}
559-
560542
/// Returns the lints in a `HashMap`, grouped by the different lint groups
561543
#[must_use]
562544
fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
@@ -593,23 +575,14 @@ impl RenamedLint {
593575

594576
/// Generates the code for registering lints
595577
#[must_use]
596-
fn gen_declared_lints<'a>(
597-
internal_lints: impl Iterator<Item = &'a Lint>,
598-
usable_lints: impl Iterator<Item = &'a Lint>,
599-
) -> String {
600-
let mut details: Vec<_> = internal_lints
601-
.map(|l| (false, &l.module, l.name.to_uppercase()))
602-
.chain(usable_lints.map(|l| (true, &l.module, l.name.to_uppercase())))
603-
.collect();
578+
fn gen_declared_lints<'a>(lints: impl Iterator<Item = &'a Lint>) -> String {
579+
let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect();
604580
details.sort_unstable();
605581

606582
let mut output = GENERATED_FILE_COMMENT.to_string();
607583
output.push_str("pub(crate) static LINTS: &[&crate::LintInfo] = &[\n");
608584

609-
for (is_public, module_name, lint_name) in details {
610-
if !is_public {
611-
output.push_str(" #[cfg(feature = \"internal\")]\n");
612-
}
585+
for (module_name, lint_name) in details {
613586
let _: fmt::Result = writeln!(output, " crate::{module_name}::{lint_name}_INFO,");
614587
}
615588
output.push_str("];\n");
@@ -669,13 +642,12 @@ fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>, Vec<RenamedLint>) {
669642
} else {
670643
module.strip_suffix(".rs").unwrap_or(&module)
671644
};
672-
673-
if module == "deprecated_lints" {
674-
parse_deprecated_contents(&contents, &mut deprecated_lints, &mut renamed_lints);
675-
} else {
676-
parse_contents(&contents, module, &mut lints);
677-
}
645+
parse_contents(&contents, module, &mut lints);
678646
}
647+
let contents = fs::read_to_string(clippy_project_root().join("clippy_deprecated_lints/src/lib.rs"))
648+
.unwrap_or_else(|e| panic!("Cannot read from `clippy_deprecated_lints/src/lib.rs`: {e}"));
649+
parse_deprecated_contents(&contents, &mut deprecated_lints, &mut renamed_lints);
650+
679651
(lints, deprecated_lints, renamed_lints)
680652
}
681653

@@ -999,41 +971,6 @@ mod tests {
999971
assert_eq!(expected, result);
1000972
}
1001973

1002-
#[test]
1003-
fn test_usable_lints() {
1004-
let lints = vec![
1005-
Lint::new(
1006-
"should_assert_eq2",
1007-
"Not Deprecated",
1008-
"\"abc\"",
1009-
"module_name",
1010-
Range::default(),
1011-
),
1012-
Lint::new(
1013-
"should_assert_eq2",
1014-
"internal",
1015-
"\"abc\"",
1016-
"module_name",
1017-
Range::default(),
1018-
),
1019-
Lint::new(
1020-
"should_assert_eq2",
1021-
"internal_style",
1022-
"\"abc\"",
1023-
"module_name",
1024-
Range::default(),
1025-
),
1026-
];
1027-
let expected = vec![Lint::new(
1028-
"should_assert_eq2",
1029-
"Not Deprecated",
1030-
"\"abc\"",
1031-
"module_name",
1032-
Range::default(),
1033-
)];
1034-
assert_eq!(expected, Lint::usable_lints(&lints));
1035-
}
1036-
1037974
#[test]
1038975
fn test_by_lint_group() {
1039976
let lints = vec![

clippy_internal_lints/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "clippy_internal_lints"
3+
version = "0.0.1"
4+
edition = "2021"
5+
6+
[dependencies]
7+
clippy_deprecated_lints = { path = "../clippy_deprecated_lints" }
8+
clippy_config = { path = "../clippy_config" }
9+
clippy_utils = { path = "../clippy_utils" }
10+
itertools = "0.12"
11+
serde = { version = "1.0", features = ["derive"] }
12+
serde_json = { version = "1.0" }
13+
tempfile = { version = "3.3.0" }
14+
regex = { version = "1.5" }
15+
rustc-semver = "1.1"
16+
17+
[features]
18+
deny-warnings = ["clippy_config/deny-warnings", "clippy_utils/deny-warnings"]
19+
20+
[package.metadata.rust-analyzer]
21+
# This crate uses #[feature(rustc_private)]
22+
rustc_private = true

clippy_lints/src/utils/internal_lints/almost_standard_lint_formulation.rs renamed to clippy_internal_lints/src/almost_standard_lint_formulation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use crate::utils::internal_lints::lint_without_lint_pass::is_lint_ref_type;
1+
use crate::lint_without_lint_pass::is_lint_ref_type;
22
use clippy_utils::diagnostics::span_lint_and_help;
33
use regex::Regex;
44
use rustc_ast as ast;
55
use rustc_hir::{Item, ItemKind, Mutability};
66
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_lint_defs::declare_tool_lint;
78
use rustc_session::impl_lint_pass;
89

9-
declare_clippy_lint! {
10+
declare_tool_lint! {
1011
/// ### What it does
1112
/// Checks if lint formulations have a standardized format.
1213
///
@@ -15,9 +16,10 @@ declare_clippy_lint! {
1516
///
1617
/// ### Example
1718
/// `Checks for use...` can be written as `Checks for usage...` .
18-
pub ALMOST_STANDARD_LINT_FORMULATION,
19-
internal,
20-
"lint formulations must have a standardized format."
19+
pub clippy::ALMOST_STANDARD_LINT_FORMULATION,
20+
Warn,
21+
"lint formulations must have a standardized format.",
22+
report_in_external_macro: true
2123
}
2224

2325
impl_lint_pass!(AlmostStandardFormulation => [ALMOST_STANDARD_LINT_FORMULATION]);

clippy_lints/src/utils/internal_lints/collapsible_calls.rs renamed to clippy_internal_lints/src/collapsible_calls.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ use clippy_utils::{is_expr_path_def_path, is_lint_allowed, peel_blocks_with_stmt
44
use rustc_errors::Applicability;
55
use rustc_hir::{Closure, Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_lint_defs::declare_tool_lint;
78
use rustc_session::declare_lint_pass;
89

910
use std::borrow::{Borrow, Cow};
1011

11-
declare_clippy_lint! {
12+
declare_tool_lint! {
1213
/// ### What it does
1314
/// Lints `span_lint_and_then` function calls, where the
1415
/// closure argument has only one statement and that statement is a method
@@ -63,9 +64,10 @@ declare_clippy_lint! {
6364
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, Some(expr.span), note_msg);
6465
/// span_lint_and_note(cx, TEST_LINT, expr.span, lint_msg, None, note_msg);
6566
/// ```
66-
pub COLLAPSIBLE_SPAN_LINT_CALLS,
67-
internal,
68-
"found collapsible `span_lint_and_then` calls"
67+
pub clippy::COLLAPSIBLE_SPAN_LINT_CALLS,
68+
Warn,
69+
"found collapsible `span_lint_and_then` calls",
70+
report_in_external_macro: true
6971
}
7072

7173
declare_lint_pass!(CollapsibleCalls => [COLLAPSIBLE_SPAN_LINT_CALLS]);

0 commit comments

Comments
 (0)