Skip to content

Commit a07c271

Browse files
bors[bot]phansch
andcommitted
Merge #3399
3399: RIIR update lints: Generate modules section and lint group sections r=flip1995 a=phansch This adds the last missing parts of the generating code. cc #2882 Co-authored-by: Philipp Hansch <[email protected]>
2 parents 71ec4ff + facfb5a commit a07c271

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

Diff for: ci/base-tests.sh

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ cargo build --features debugging
2222
cargo test --features debugging
2323
cd clippy_lints && cargo test && cd ..
2424
cd rustc_tools_util && cargo test && cd ..
25+
cd clippy_dev && cargo test && cd ..
2526
# check that the lint lists are up-to-date
2627
./util/update_lints.py -c
2728

Diff for: clippy_dev/src/lib.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ impl Lint {
7171
}
7272
}
7373

74+
/// Generates the Vec items for `register_lint_group` calls in `clippy_lints/src/lib.rs`.
75+
pub fn gen_lint_group_list(lints: Vec<Lint>) -> Vec<String> {
76+
lints.into_iter()
77+
.filter_map(|l| {
78+
if l.is_internal() || l.deprecation.is_some() {
79+
None
80+
} else {
81+
Some(format!(" {}::{},", l.module, l.name.to_uppercase()))
82+
}
83+
})
84+
.sorted()
85+
}
86+
87+
/// Generates the `pub mod module_name` list in `clippy_lints/src/lib.rs`.
88+
pub fn gen_modules_list(lints: Vec<Lint>) -> Vec<String> {
89+
lints.into_iter()
90+
.filter_map(|l| {
91+
if l.is_internal() || l.deprecation.is_some() { None } else { Some(l.module) }
92+
})
93+
.unique()
94+
.map(|module| {
95+
format!("pub mod {};", module)
96+
})
97+
.sorted()
98+
}
99+
74100
/// Generates the list of lint links at the bottom of the README
75101
pub fn gen_changelog_lint_list(lints: Vec<Lint>) -> Vec<String> {
76102
let mut lint_list_sorted: Vec<Lint> = lints;
@@ -112,7 +138,13 @@ fn gather_from_file(dir_entry: &walkdir::DirEntry) -> impl Iterator<Item=Lint> {
112138
let mut file = fs::File::open(dir_entry.path()).unwrap();
113139
let mut content = String::new();
114140
file.read_to_string(&mut content).unwrap();
115-
parse_contents(&content, dir_entry.path().file_stem().unwrap().to_str().unwrap())
141+
let mut filename = dir_entry.path().file_stem().unwrap().to_str().unwrap();
142+
// If the lints are stored in mod.rs, we get the module name from
143+
// the containing directory:
144+
if filename == "mod" {
145+
filename = dir_entry.path().parent().unwrap().file_stem().unwrap().to_str().unwrap()
146+
}
147+
parse_contents(&content, filename)
116148
}
117149

118150
fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item=Lint> {
@@ -214,7 +246,7 @@ pub fn replace_region_in_text<F>(text: &str, start: &str, end: &str, replace_sta
214246
// This happens if the provided regex in `clippy_dev/src/main.rs` is not found in the
215247
// given text or file. Most likely this is an error on the programmer's side and the Regex
216248
// is incorrect.
217-
println!("regex {:?} not found. You may have to update it.", start);
249+
eprintln!("error: regex `{:?}` not found. You may have to update it.", start);
218250
}
219251
new_lines.join("\n")
220252
}
@@ -355,3 +387,33 @@ fn test_gen_deprecated() {
355387
];
356388
assert_eq!(expected, gen_deprecated(&lints));
357389
}
390+
391+
#[test]
392+
fn test_gen_modules_list() {
393+
let lints = vec![
394+
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
395+
Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"),
396+
Lint::new("incorrect_stuff", "group3", "abc", None, "another_module"),
397+
Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"),
398+
];
399+
let expected = vec![
400+
"pub mod another_module;".to_string(),
401+
"pub mod module_name;".to_string(),
402+
];
403+
assert_eq!(expected, gen_modules_list(lints));
404+
}
405+
406+
#[test]
407+
fn test_gen_lint_group_list() {
408+
let lints = vec![
409+
Lint::new("abc", "group1", "abc", None, "module_name"),
410+
Lint::new("should_assert_eq", "group1", "abc", None, "module_name"),
411+
Lint::new("should_assert_eq2", "group2", "abc", Some("abc"), "deprecated"),
412+
Lint::new("incorrect_internal", "internal_style", "abc", None, "module_name"),
413+
];
414+
let expected = vec![
415+
" module_name::ABC,".to_string(),
416+
" module_name::SHOULD_ASSERT_EQ,".to_string(),
417+
];
418+
assert_eq!(expected, gen_lint_group_list(lints));
419+
}

Diff for: clippy_dev/src/main.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ fn main() {
1919
let matches = App::new("Clippy developer tooling")
2020
.subcommand(
2121
SubCommand::with_name("update_lints")
22-
.about("Update the lint list")
22+
.about("Makes sure that:\n \
23+
* the lint count in README.md is correct\n \
24+
* the changelog contains markdown link references at the bottom\n \
25+
* all lint groups include the correct lints\n \
26+
* lint modules in `clippy_lints/*` are visible in `src/lib.rs` via `pub mod`\n \
27+
* all lints are registered in the lint store")
2328
.arg(
2429
Arg::with_name("print-only")
2530
.long("print-only")
2631
.short("p")
27-
.help("Print a table of lints to STDOUT. Does not modify any files."),
32+
.help("Print a table of lints to STDOUT. This does not include deprecated and internal lints. (Does not modify any files)"),
2833
)
2934
)
3035
.get_matches();
@@ -90,4 +95,42 @@ fn update_lints() {
9095
false,
9196
|| { gen_deprecated(&lint_list) }
9297
);
98+
99+
replace_region_in_file(
100+
"../clippy_lints/src/lib.rs",
101+
"begin lints modules",
102+
"end lints modules",
103+
false,
104+
|| { gen_modules_list(lint_list.clone()) }
105+
);
106+
107+
// Generate lists of lints in the clippy::all lint group
108+
replace_region_in_file(
109+
"../clippy_lints/src/lib.rs",
110+
r#"reg.register_lint_group\("clippy::all""#,
111+
r#"\]\);"#,
112+
false,
113+
|| {
114+
// clippy::all should only include the following lint groups:
115+
let all_group_lints = usable_lints.clone().into_iter().filter(|l| {
116+
l.group == "correctness" ||
117+
l.group == "style" ||
118+
l.group == "complexity" ||
119+
l.group == "perf"
120+
}).collect();
121+
122+
gen_lint_group_list(all_group_lints)
123+
}
124+
);
125+
126+
// Generate the list of lints for all other lint groups
127+
for (lint_group, lints) in Lint::by_lint_group(&usable_lints) {
128+
replace_region_in_file(
129+
"../clippy_lints/src/lib.rs",
130+
&format!("reg.register_lint_group\\(\"clippy::{}\"", lint_group),
131+
r#"\]\);"#,
132+
false,
133+
|| { gen_lint_group_list(lints.clone()) }
134+
);
135+
}
93136
}

0 commit comments

Comments
 (0)