Skip to content
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

Keep lints updated #14030

Merged
merged 2 commits into from
Jun 7, 2024
Merged
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
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(
epage marked this conversation as resolved.
Show resolved Hide resolved
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
);
}
}