Skip to content

WIP: update_lints.py RIIR #3266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ impl Lint {
pub fn by_lint_group(lints: &[Self]) -> HashMap<String, Vec<Self>> {
lints.iter().map(|lint| (lint.group.to_string(), lint.clone())).into_group_map()
}

/// Generates `pub mod module_name` for the lints in the given `group`
pub fn gen_pub_mod_for_group(lints: &[Lint]) -> Vec<String> {
lints.into_iter().map(|l| format!("pub mod {};", l.module)).collect::<Vec<String>>()
}

pub fn gen_lint_group(lints: &[Lint]) -> Vec<String> {
lints.into_iter().map(|l| format!(" {}::{},", l.module, l.name.to_uppercase())).collect::<Vec<String>>()
}
}

pub fn gather_all() -> impl Iterator<Item=Lint> {
Expand Down Expand Up @@ -86,6 +95,67 @@ fn lint_files() -> impl Iterator<Item=fs::DirEntry> {
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
}


pub fn clippy_version_from_toml() -> String {
let mut file = fs::File::open("../Cargo.toml").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
let version_line = content.lines().find(|l| l.starts_with("version ="));
if let Some(version_line) = version_line {
let split = version_line.split(" ").collect::<Vec<&str>>();
split[2].trim_matches('"').to_string()
} else {
panic!("Error: version not found in Cargo.toml!");
}
}

pub fn replace_region_in_file<F>(path: &str, start: &str, end: &str, replace_start: bool, replacements: F) where F: Fn() -> Vec<String> {
let mut f = fs::File::open(path).expect(&format!("File not found: {}", path));
let mut contents = String::new();
f.read_to_string(&mut contents).expect("Something went wrong reading the file");
let replaced = replace_region_in_text(&contents, start, end, replace_start, replacements);

let mut f = fs::File::create(path).expect(&format!("File not found: {}", path));
f.write_all(replaced.as_bytes()).expect("Unable to write file");
}

// Replace a region in a file delimited by two lines matching regexes.
//
// A callback is called to write the new region.
// If `replace_start` is true, the start delimiter line is replaced as well.
// The end delimiter line is never replaced.
pub fn replace_region_in_text<F>(text: &str, start: &str, end: &str, replace_start: bool, replacements: F) -> String where F: Fn() -> Vec<String> {
let lines = text.lines();
let mut in_old_region = false;
let mut found = false;
let mut new_lines = vec![];
let start = Regex::new(start).unwrap();
let end = Regex::new(end).unwrap();

for line in lines {
if in_old_region {
if end.is_match(&line) {
in_old_region = false;
new_lines.extend(replacements());
new_lines.push(line.to_string());
}
} else if start.is_match(&line) {
if !replace_start {
new_lines.push(line.to_string());
}
in_old_region = true;
found = true;
} else {
new_lines.push(line.to_string());
}
}

if !found {
println!("regex {:?} not found", start);
}
new_lines.join("\n")
}

#[test]
fn test_parse_contents() {
let result: Vec<Lint> = parse_contents(
Expand Down Expand Up @@ -125,6 +195,44 @@ declare_deprecated_lint! {
assert_eq!(expected, result);
}


#[test]
fn test_replace_region() {
let text = r#"
abc
123
789
def
ghi"#;
let expected = r#"
abc
hello world
def
ghi"#;
let result = replace_region_in_text(text, r#"^\s*abc$"#, r#"^\s*def"#, false, || {
vec!["hello world".to_string()]
});
assert_eq!(expected, result);
}

#[test]
fn test_replace_region_with_start() {
let text = r#"
abc
123
789
def
ghi"#;
let expected = r#"
hello world
def
ghi"#;
let result = replace_region_in_text(text, r#"^\s*abc$"#, r#"^\s*def"#, true, || {
vec!["hello world".to_string()]
});
assert_eq!(expected, result);
}

#[test]
fn test_active_lints() {
let lints = vec![
Expand Down Expand Up @@ -154,3 +262,29 @@ fn test_by_lint_group() {
]);
assert_eq!(expected, Lint::by_lint_group(&lints));
}

#[test]
fn test_gen_pub_mod_for_group() {
let lints = vec![
Lint::new("should_assert_eq", "Deprecated", "abc", Some("Reason"), "abc"),
Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name"),
];
let expected = vec![
"pub mod abc;".to_string(),
"pub mod module_name;".to_string(),
];
assert_eq!(expected, Lint::gen_pub_mod_for_group(&lints));
}

#[test]
fn test_gen_lint_group() {
let lints = vec![
Lint::new("should_assert_eq", "Deprecated", "abc", Some("Reason"), "abc"),
Lint::new("should_assert_eq2", "Not Deprecated", "abc", None, "module_name"),
];;
let expected = vec![
" abc::SHOULD_ASSERT_EQ,".to_string(),
" module_name::SHOULD_ASSERT_EQ2,".to_string()
];
assert_eq!(expected, Lint::gen_lint_group(&lints));
}
86 changes: 84 additions & 2 deletions clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ fn main() {
Arg::with_name("print-only")
.long("print-only")
.short("p")
.help("Print a table of lints to STDOUT. Does not modify any files."),
)
.help("Print a table of lints to STDOUT. Does not modify any files."))
.arg(
Arg::with_name("changes")
.short("c")
.help("Print a warning and set exit status to 1 if files would be changed"),
),
)
.get_matches();

if let Some(matches) = matches.subcommand_matches("update_lints") {
if matches.is_present("print-only") {
print_lints();
} else {
update_lints();
}
}
}
Expand All @@ -43,3 +49,79 @@ fn print_lints() {

println!("there are {} lints", Lint::active_lints(&lint_list).count());
}


fn update_lints() {
let lint_list = gather_all().collect::<Vec<Lint>>();
let active_lints = Lint::active_lints(&lint_list);
let lint_count = active_lints.count();
let clippy_version = clippy_version_from_toml();

replace_region_in_file(
"../README.md",
r#"\[There are \d+ lints included in this crate!\]\(https://rust-lang-nursery.github.io/rust-clippy/master/index.html\)"#,
"",
true,
|| {
vec![
format!("[There are {} lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)", lint_count)
]
}
);
replace_region_in_file(
"../CHANGELOG.md",
"<!-- begin autogenerated links to lint list -->",
"<!-- end autogenerated links to lint list -->",
false,
|| {
vec![
// TODO
//
// lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], docs_link) for l in
// sorted(all_lints + deprecated_lints,
// key=lambda l: l[1])],
]
}
);
replace_region_in_file(
"../Cargo.toml",
"# begin automatic update",
"# end automatic update",
false,
|| {
vec![
format!("clippy_lints = {{ version = \"{}\", path = \"clippy_lints\" }}\n", clippy_version)
]
}
);
replace_region_in_file(
"../clippy_lints/Cargo.toml",
"# begin automatic update",
"# end automatic update",
false,
|| {
vec![format!("version = {}", clippy_version)]
}
);
replace_region_in_file(
"../clippy_lints/src/lib.rs",
"begin lints modules",
"end lints modules",
false,
|| {
Lint::gen_pub_mod_for_group(&active_lints)
}
);
replace_region_in_file(
"../clippy_lints/src/lib.rs",
r#"reg.register_lint_group\("clippy""#,
r#"\]\);"#,
false,
|| {
vec![
// TODO: gen_group
]
}
);
// TODO: Add the register_lint_group inside the loop, too
}