Skip to content

Commit

Permalink
Auto merge of #14030 - Muscraft:keep-lints-updated, r=epage
Browse files Browse the repository at this point in the history
Keep lints updated

In #14024, [it was noted](#14024 (review)) that we should probably have some automated way to ensure that `LINTS` stays up to date. I went ahead and did this, but I extended the idea to also include `LINT_GROUPS` and added support for ensuring they both stay sorted.

Error message for `LINTS` not being sorted:
![Screenshot from 2024-06-07 10-18-43](https://github.com/rust-lang/cargo/assets/23045215/aa44e25c-1bf9-4cba-8a1b-29458aa409f0)

Error message for missing `Lint`s:
![Screenshot from 2024-06-07 10-51-09](https://github.com/rust-lang/cargo/assets/23045215/8952d0aa-76e1-46b4-bccc-348400da6548)
  • Loading branch information
bors committed Jun 7, 2024
2 parents 3e89630 + 959b712 commit b1feb75
Showing 1 changed file with 120 additions and 1 deletion.
121 changes: 120 additions & 1 deletion src/cargo/util/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use toml_edit::ImDocument;

const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
pub const LINTS: &[Lint] = &[
IM_A_TEAPOT,
IMPLICIT_FEATURES,
IM_A_TEAPOT,
UNKNOWN_LINTS,
UNUSED_OPTIONAL_DEPENDENCY,
];
Expand Down Expand Up @@ -875,3 +875,122 @@ pub fn unused_dependencies(
}
Ok(())
}

#[cfg(test)]
mod tests {
use itertools::Itertools;
use snapbox::ToDebug;
use std::collections::HashSet;

#[test]
fn ensure_sorted_lints() {
// This will be printed out if the fields are not sorted.
let location = std::panic::Location::caller();
println!("\nTo fix this test, sort `LINTS` in {}\n", location.file(),);

let actual = super::LINTS
.iter()
.map(|l| l.name.to_uppercase())
.collect::<Vec<_>>();

let mut expected = actual.clone();
expected.sort();
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
}

#[test]
fn ensure_sorted_lint_groups() {
// This will be printed out if the fields are not sorted.
let location = std::panic::Location::caller();
println!(
"\nTo fix this test, sort `LINT_GROUPS` in {}\n",
location.file(),
);
let actual = super::LINT_GROUPS
.iter()
.map(|l| l.name.to_uppercase())
.collect::<Vec<_>>();

let mut expected = actual.clone();
expected.sort();
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
}

#[test]
fn ensure_updated_lints() {
let path = snapbox::utils::current_rs!();
let expected = std::fs::read_to_string(&path).unwrap();
let expected = expected
.lines()
.filter_map(|l| {
if l.ends_with(": Lint = Lint {") {
Some(
l.chars()
.skip(6)
.take_while(|c| *c != ':')
.collect::<String>(),
)
} else {
None
}
})
.collect::<HashSet<_>>();
let actual = super::LINTS
.iter()
.map(|l| l.name.to_uppercase())
.collect::<HashSet<_>>();
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();

let mut need_added = String::new();
for name in &diff {
need_added.push_str(&format!("{}\n", name));
}
assert!(
diff.is_empty(),
"\n`LINTS` did not contain all `Lint`s found in {}\n\
Please add the following to `LINTS`:\n\
{}",
path.display(),
need_added
);
}

#[test]
fn ensure_updated_lint_groups() {
let path = snapbox::utils::current_rs!();
let expected = std::fs::read_to_string(&path).unwrap();
let expected = expected
.lines()
.filter_map(|l| {
if l.ends_with(": LintGroup = LintGroup {") {
Some(
l.chars()
.skip(6)
.take_while(|c| *c != ':')
.collect::<String>(),
)
} else {
None
}
})
.collect::<HashSet<_>>();
let actual = super::LINT_GROUPS
.iter()
.map(|l| l.name.to_uppercase())
.collect::<HashSet<_>>();
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();

let mut need_added = String::new();
for name in &diff {
need_added.push_str(&format!("{}\n", name));
}
assert!(
diff.is_empty(),
"\n`LINT_GROUPS` did not contain all `LintGroup`s found in {}\n\
Please add the following to `LINT_GROUPS`:\n\
{}",
path.display(),
need_added
);
}
}

0 comments on commit b1feb75

Please sign in to comment.