Skip to content

Commit b1feb75

Browse files
committed
Auto merge of #14030 - Muscraft:keep-lints-updated, r=epage
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)
2 parents 3e89630 + 959b712 commit b1feb75

File tree

1 file changed

+120
-1
lines changed

1 file changed

+120
-1
lines changed

src/cargo/util/lints.rs

+120-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use toml_edit::ImDocument;
1414

1515
const LINT_GROUPS: &[LintGroup] = &[TEST_DUMMY_UNSTABLE];
1616
pub const LINTS: &[Lint] = &[
17-
IM_A_TEAPOT,
1817
IMPLICIT_FEATURES,
18+
IM_A_TEAPOT,
1919
UNKNOWN_LINTS,
2020
UNUSED_OPTIONAL_DEPENDENCY,
2121
];
@@ -875,3 +875,122 @@ pub fn unused_dependencies(
875875
}
876876
Ok(())
877877
}
878+
879+
#[cfg(test)]
880+
mod tests {
881+
use itertools::Itertools;
882+
use snapbox::ToDebug;
883+
use std::collections::HashSet;
884+
885+
#[test]
886+
fn ensure_sorted_lints() {
887+
// This will be printed out if the fields are not sorted.
888+
let location = std::panic::Location::caller();
889+
println!("\nTo fix this test, sort `LINTS` in {}\n", location.file(),);
890+
891+
let actual = super::LINTS
892+
.iter()
893+
.map(|l| l.name.to_uppercase())
894+
.collect::<Vec<_>>();
895+
896+
let mut expected = actual.clone();
897+
expected.sort();
898+
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
899+
}
900+
901+
#[test]
902+
fn ensure_sorted_lint_groups() {
903+
// This will be printed out if the fields are not sorted.
904+
let location = std::panic::Location::caller();
905+
println!(
906+
"\nTo fix this test, sort `LINT_GROUPS` in {}\n",
907+
location.file(),
908+
);
909+
let actual = super::LINT_GROUPS
910+
.iter()
911+
.map(|l| l.name.to_uppercase())
912+
.collect::<Vec<_>>();
913+
914+
let mut expected = actual.clone();
915+
expected.sort();
916+
snapbox::assert_data_eq!(actual.to_debug(), expected.to_debug());
917+
}
918+
919+
#[test]
920+
fn ensure_updated_lints() {
921+
let path = snapbox::utils::current_rs!();
922+
let expected = std::fs::read_to_string(&path).unwrap();
923+
let expected = expected
924+
.lines()
925+
.filter_map(|l| {
926+
if l.ends_with(": Lint = Lint {") {
927+
Some(
928+
l.chars()
929+
.skip(6)
930+
.take_while(|c| *c != ':')
931+
.collect::<String>(),
932+
)
933+
} else {
934+
None
935+
}
936+
})
937+
.collect::<HashSet<_>>();
938+
let actual = super::LINTS
939+
.iter()
940+
.map(|l| l.name.to_uppercase())
941+
.collect::<HashSet<_>>();
942+
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();
943+
944+
let mut need_added = String::new();
945+
for name in &diff {
946+
need_added.push_str(&format!("{}\n", name));
947+
}
948+
assert!(
949+
diff.is_empty(),
950+
"\n`LINTS` did not contain all `Lint`s found in {}\n\
951+
Please add the following to `LINTS`:\n\
952+
{}",
953+
path.display(),
954+
need_added
955+
);
956+
}
957+
958+
#[test]
959+
fn ensure_updated_lint_groups() {
960+
let path = snapbox::utils::current_rs!();
961+
let expected = std::fs::read_to_string(&path).unwrap();
962+
let expected = expected
963+
.lines()
964+
.filter_map(|l| {
965+
if l.ends_with(": LintGroup = LintGroup {") {
966+
Some(
967+
l.chars()
968+
.skip(6)
969+
.take_while(|c| *c != ':')
970+
.collect::<String>(),
971+
)
972+
} else {
973+
None
974+
}
975+
})
976+
.collect::<HashSet<_>>();
977+
let actual = super::LINT_GROUPS
978+
.iter()
979+
.map(|l| l.name.to_uppercase())
980+
.collect::<HashSet<_>>();
981+
let diff = expected.difference(&actual).sorted().collect::<Vec<_>>();
982+
983+
let mut need_added = String::new();
984+
for name in &diff {
985+
need_added.push_str(&format!("{}\n", name));
986+
}
987+
assert!(
988+
diff.is_empty(),
989+
"\n`LINT_GROUPS` did not contain all `LintGroup`s found in {}\n\
990+
Please add the following to `LINT_GROUPS`:\n\
991+
{}",
992+
path.display(),
993+
need_added
994+
);
995+
}
996+
}

0 commit comments

Comments
 (0)