Skip to content

Test that all interned symbols are referenced in Clippy sources #14842

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ filetime = "0.2.9"
itertools = "0.12"
pulldown-cmark = { version = "0.11", default-features = false, features = ["html"] }
askama = { version = "0.13", default-features = false, features = ["alloc", "config", "derive"] }
rayon = "1.10.0"

# UI test dependencies
if_chain = "1.0"
Expand Down
1 change: 0 additions & 1 deletion clippy_utils/src/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ generate! {
enum_glob_use,
enumerate,
err,
error,
exp,
expect_err,
expn_data,
Expand Down
92 changes: 92 additions & 0 deletions tests/symbols-used.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This test checks that all symbols defined in Clippy's `sym.rs` file
// are used in Clippy. Otherwise, it will fail with a list of symbols
// which are unused.
//
// This test is a no-op if run as part of the compiler test suite
// and will always succeed.

use std::collections::HashSet;
use std::fs;
use std::path::Path;

use rayon::prelude::*;
use regex::Regex;
use walkdir::{DirEntry, WalkDir};

const SYM_FILE: &str = "clippy_utils/src/sym.rs";

type Result<T, E = AnyError> = std::result::Result<T, E>;
type AnyError = Box<dyn std::error::Error>;

fn load_interned_symbols() -> Result<HashSet<String>> {
let content = fs::read_to_string(SYM_FILE)?;
let content = content
.split_once("generate! {")
.ok_or("cannot find symbols start")?
.1
.split_once("\n}\n")
.ok_or("cannot find symbols end")?
.0;
Ok(Regex::new(r"(?m)^ (\w+)")
.unwrap()
.captures_iter(content)
.map(|m| m[1].to_owned())
.collect())
}

fn load_symbols(file: impl AsRef<Path>, re: &Regex) -> Result<Vec<String>> {
Ok(re
.captures_iter(&fs::read_to_string(file)?)
.map(|m| m[1].to_owned())
.collect())
}

fn load_paths(file: impl AsRef<Path>, re: &Regex) -> Result<Vec<String>> {
Ok(re
.captures_iter(&fs::read_to_string(file)?)
.flat_map(|m| m[1].split("::").map(String::from).collect::<Vec<_>>())
.collect())
}

#[test]
#[allow(clippy::case_sensitive_file_extension_comparisons)]
fn all_symbols_are_used() -> Result<()> {
if option_env!("RUSTC_TEST_SUITE").is_some() {
return Ok(());
}

let interned = load_interned_symbols()?;

let used_re = Regex::new(r"\bsym::(\w+)\b").unwrap();
let mut used = ["clippy_lints", "clippy_lints_internal", "clippy_utils", "src"]
.par_iter()
.flat_map(|dir| {
WalkDir::new(dir)
.into_iter()
.filter_entry(|e| e.file_name().to_str().is_some_and(|s| s.ends_with(".rs")) || e.file_type().is_dir())
.flat_map(|e| e.map(DirEntry::into_path))
.par_bridge()
// Silently ignore errors, this can never make this test pass while it should fail anyway
.flat_map(|file| load_symbols(file, &used_re).unwrap_or_default())
})
.collect::<HashSet<_>>();

let paths_re = Regex::new(r"path!\(([\w:]+)\)").unwrap();
for path in [
"clippy_utils/src/paths.rs",
"clippy_lints_internal/src/internal_paths.rs",
] {
used.extend(load_paths(path, &paths_re)?);
}

let mut extra = interned.difference(&used).collect::<Vec<_>>();
if !extra.is_empty() {
extra.sort_unstable();
eprintln!("Unused symbols defined in {SYM_FILE}:");
for sym in extra {
eprintln!(" - {sym}");
}
Err(format!("extra symbols found — remove them {SYM_FILE}"))?;
}
Ok(())
}