Replace the metadata collector with tests

This commit is contained in:
Alex Macleod 2024-08-05 14:08:04 +00:00
parent 8827107526
commit 182cd5f278
15 changed files with 451 additions and 1209 deletions

View File

@ -1,10 +1,10 @@
[alias] [alias]
bless = "test --config env.RUSTC_BLESS='1'"
uitest = "test --test compile-test" uitest = "test --test compile-test"
uibless = "test --test compile-test -- -- --bless" uibless = "bless --test compile-test"
bless = "test -- -- --bless"
dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --" dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
lintcheck = "run --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- " lintcheck = "run --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
collect-metadata = "test --test dogfood --features internal -- collect_metadata" collect-metadata = "test --test compile-test --config env.COLLECT_METADATA='1'"
[build] [build]
# -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests # -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests

View File

@ -136,11 +136,6 @@ jobs:
- name: Test metadata collection - name: Test metadata collection
run: cargo collect-metadata run: cargo collect-metadata
- name: Test lint_configuration.md is up-to-date
run: |
echo "run \`cargo collect-metadata\` if this fails"
git update-index --refresh
integration_build: integration_build:
needs: changelog needs: changelog
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -30,8 +30,11 @@ color-print = "0.3.4"
anstream = "0.6.0" anstream = "0.6.0"
[dev-dependencies] [dev-dependencies]
cargo_metadata = "0.18.1"
ui_test = "0.25" ui_test = "0.25"
regex = "1.5.5" regex = "1.5.5"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.122"
toml = "0.7.3" toml = "0.7.3"
walkdir = "2.3" walkdir = "2.3"
filetime = "0.2.9" filetime = "0.2.9"
@ -41,7 +44,6 @@ itertools = "0.12"
clippy_utils = { path = "clippy_utils" } clippy_utils = { path = "clippy_utils" }
if_chain = "1.0" if_chain = "1.0"
quote = "1.0.25" quote = "1.0.25"
serde = { version = "1.0.145", features = ["derive"] }
syn = { version = "2.0", features = ["full"] } syn = { version = "2.0", features = ["full"] }
futures = "0.3" futures = "0.3"
parking_lot = "0.12" parking_lot = "0.12"

View File

@ -739,7 +739,7 @@ for some users. Adding a configuration is done in the following steps:
5. Update [Lint Configuration](../lint_configuration.md) 5. Update [Lint Configuration](../lint_configuration.md)
Run `cargo collect-metadata` to generate documentation changes for the book. Run `cargo bless --test config-metadata` to generate documentation changes for the book.
[`clippy_config::conf`]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_config/src/conf.rs [`clippy_config::conf`]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_config/src/conf.rs
[`clippy_lints` lib file]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/lib.rs [`clippy_lints` lib file]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/lib.rs

View File

@ -1,5 +1,5 @@
<!-- <!--
This file is generated by `cargo collect-metadata`. This file is generated by `cargo bless --test config-metadata`.
Please use that command to update the file and do not edit it by hand. Please use that command to update the file and do not edit it by hand.
--> -->
@ -949,5 +949,3 @@ Whether to also emit warnings for unsafe blocks with metavariable expansions in
--- ---
**Affected lints:** **Affected lints:**
* [`macro_metavars_in_unsafe`](https://rust-lang.github.io/rust-clippy/master/index.html#macro_metavars_in_unsafe) * [`macro_metavars_in_unsafe`](https://rust-lang.github.io/rust-clippy/master/index.html#macro_metavars_in_unsafe)

View File

@ -604,7 +604,7 @@ fn gen_declared_lints<'a>(
details.sort_unstable(); details.sort_unstable();
let mut output = GENERATED_FILE_COMMENT.to_string(); let mut output = GENERATED_FILE_COMMENT.to_string();
output.push_str("pub(crate) static LINTS: &[&crate::LintInfo] = &[\n"); output.push_str("pub static LINTS: &[&crate::LintInfo] = &[\n");
for (is_public, module_name, lint_name) in details { for (is_public, module_name, lint_name) in details {
if !is_public { if !is_public {

View File

@ -2,7 +2,7 @@
// Use that command to update this file and do not edit by hand. // Use that command to update this file and do not edit by hand.
// Manual edits will be overwritten. // Manual edits will be overwritten.
pub(crate) static LINTS: &[&crate::LintInfo] = &[ pub static LINTS: &[&crate::LintInfo] = &[
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
crate::utils::internal_lints::almost_standard_lint_formulation::ALMOST_STANDARD_LINT_FORMULATION_INFO, crate::utils::internal_lints::almost_standard_lint_formulation::ALMOST_STANDARD_LINT_FORMULATION_INFO,
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
@ -22,8 +22,6 @@
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
crate::utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE_INFO, crate::utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE_INFO,
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
crate::utils::internal_lints::metadata_collector::METADATA_COLLECTOR_INFO,
#[cfg(feature = "internal")]
crate::utils::internal_lints::msrv_attr_impl::MISSING_MSRV_ATTR_IMPL_INFO, crate::utils::internal_lints::msrv_attr_impl::MISSING_MSRV_ATTR_IMPL_INFO,
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
crate::utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA_INFO, crate::utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA_INFO,

View File

@ -66,8 +66,8 @@
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))] #[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
mod utils; mod utils;
mod declared_lints; pub mod declared_lints;
mod deprecated_lints; pub mod deprecated_lints;
// begin lints modules, do not remove this comment, its used in `update_lints` // begin lints modules, do not remove this comment, its used in `update_lints`
mod absolute_paths; mod absolute_paths;
@ -440,7 +440,7 @@ fn register(self, store: &mut rustc_lint::LintStore) {
} }
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone, Debug)]
pub(crate) enum LintCategory { pub(crate) enum LintCategory {
Cargo, Cargo,
Complexity, Complexity,
@ -479,11 +479,39 @@ fn group(self, groups: &mut RegistrationGroups) -> &mut Vec<LintId> {
} }
} }
pub(crate) struct LintInfo { pub struct LintInfo {
/// Double reference to maintain pointer equality /// Double reference to maintain pointer equality
lint: &'static &'static Lint, pub lint: &'static &'static Lint,
category: LintCategory, category: LintCategory,
explanation: &'static str, pub explanation: &'static str,
/// e.g. `clippy_lints/src/absolute_paths.rs#43`
pub location: &'static str,
pub version: Option<&'static str>,
}
impl LintInfo {
/// Returns the lint name in lowercase without the `clippy::` prefix
#[allow(clippy::missing_panics_doc)]
pub fn name_lower(&self) -> String {
self.lint.name.strip_prefix("clippy::").unwrap().to_ascii_lowercase()
}
/// Returns the name of the lint's category in lowercase (`style`, `pedantic`)
pub fn category_str(&self) -> &'static str {
match self.category {
Cargo => "cargo",
Complexity => "complexity",
Correctness => "correctness",
Nursery => "nursery",
Pedantic => "pedantic",
Perf => "perf",
Restriction => "restriction",
Style => "style",
Suspicious => "suspicious",
#[cfg(feature = "internal")]
Internal => "internal",
}
}
} }
pub fn explain(name: &str) -> i32 { pub fn explain(name: &str) -> i32 {
@ -538,14 +566,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_removed(name, reason); store.register_removed(name, reason);
} }
#[cfg(feature = "internal")]
{
if std::env::var("ENABLE_METADATA_COLLECTION").eq(&Ok("1".to_string())) {
store.register_late_pass(|_| Box::new(utils::internal_lints::metadata_collector::MetadataCollector::new()));
return;
}
}
let format_args_storage = FormatArgsStorage::default(); let format_args_storage = FormatArgsStorage::default();
let format_args = format_args_storage.clone(); let format_args = format_args_storage.clone();
store.register_early_pass(move || { store.register_early_pass(move || {

View File

@ -3,7 +3,6 @@
pub mod interning_defined_symbol; pub mod interning_defined_symbol;
pub mod invalid_paths; pub mod invalid_paths;
pub mod lint_without_lint_pass; pub mod lint_without_lint_pass;
pub mod metadata_collector;
pub mod msrv_attr_impl; pub mod msrv_attr_impl;
pub mod outer_expn_data_pass; pub mod outer_expn_data_pass;
pub mod produce_ice; pub mod produce_ice;

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
#![feature(let_chains)] #![feature(let_chains, proc_macro_span)]
// warn on lints, that are included in `rust-lang/rust`s bootstrap // warn on lints, that are included in `rust-lang/rust`s bootstrap
#![warn(rust_2018_idioms, unused_lifetimes)] #![warn(rust_2018_idioms, unused_lifetimes)]
@ -23,6 +23,7 @@
struct ClippyLint { struct ClippyLint {
attrs: Vec<Attribute>, attrs: Vec<Attribute>,
version: Option<LitStr>,
explanation: String, explanation: String,
name: Ident, name: Ident,
category: Ident, category: Ident,
@ -41,8 +42,14 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
let value = lit.value(); let value = lit.value();
let line = value.strip_prefix(' ').unwrap_or(&value); let line = value.strip_prefix(' ').unwrap_or(&value);
if line.starts_with("```") { if let Some(lang) = line.strip_prefix("```") {
explanation += "```\n"; let tag = lang.split_once(',').map_or(lang, |(left, _)| left);
if !in_code && matches!(tag, "" | "rust" | "ignore" | "should_panic" | "no_run" | "compile_fail") {
explanation += "```rust\n";
} else {
explanation += line;
explanation.push('\n');
}
in_code = !in_code; in_code = !in_code;
} else if !(in_code && line.starts_with("# ")) { } else if !(in_code && line.starts_with("# ")) {
explanation += line; explanation += line;
@ -68,6 +75,7 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
Ok(Self { Ok(Self {
attrs, attrs,
version,
explanation, explanation,
name, name,
category, category,
@ -123,6 +131,7 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
pub fn declare_clippy_lint(input: TokenStream) -> TokenStream { pub fn declare_clippy_lint(input: TokenStream) -> TokenStream {
let ClippyLint { let ClippyLint {
attrs, attrs,
version,
explanation, explanation,
name, name,
category, category,
@ -146,6 +155,14 @@ pub fn declare_clippy_lint(input: TokenStream) -> TokenStream {
(&mut category[0..1]).make_ascii_uppercase(); (&mut category[0..1]).make_ascii_uppercase();
let category_variant = format_ident!("{category}"); let category_variant = format_ident!("{category}");
let name_span = name.span().unwrap();
let location = format!("{}#{}", name_span.source_file().path().display(), name_span.line());
let version = match version {
Some(version) => quote!(Some(#version)),
None => quote!(None),
};
let output = quote! { let output = quote! {
rustc_session::declare_tool_lint! { rustc_session::declare_tool_lint! {
#(#attrs)* #(#attrs)*
@ -159,6 +176,8 @@ pub fn declare_clippy_lint(input: TokenStream) -> TokenStream {
lint: &#name, lint: &#name,
category: crate::LintCategory::#category_variant, category: crate::LintCategory::#category_variant,
explanation: #explanation, explanation: #explanation,
location: #location,
version: #version,
}; };
}; };

View File

@ -1,22 +1,31 @@
#![feature(rustc_private, let_chains)]
#![warn(rust_2018_idioms, unused_lifetimes)] #![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unused_extern_crates)] #![allow(unused_extern_crates)]
use cargo_metadata::diagnostic::{Applicability, Diagnostic};
use cargo_metadata::Message;
use clippy_config::ClippyConfiguration;
use clippy_lints::declared_lints::LINTS;
use clippy_lints::deprecated_lints::{DEPRECATED, DEPRECATED_VERSION, RENAMED};
use clippy_lints::LintInfo;
use serde::{Deserialize, Serialize};
use test_utils::IS_RUSTC_TEST_SUITE;
use ui_test::custom_flags::rustfix::RustfixMode; use ui_test::custom_flags::rustfix::RustfixMode;
use ui_test::custom_flags::Flag;
use ui_test::spanned::Spanned; use ui_test::spanned::Spanned;
use ui_test::test_result::TestRun;
use ui_test::{status_emitter, Args, CommandBuilder, Config, Match, OutputConflictHandling}; use ui_test::{status_emitter, Args, CommandBuilder, Config, Match, OutputConflictHandling};
use std::collections::BTreeMap; use std::collections::{BTreeMap, HashMap};
use std::env::{self, set_var, var_os}; use std::env::{self, set_var, var_os};
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use std::fs; use std::fmt::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::LazyLock; use std::sync::mpsc::{channel, Sender};
use test_utils::IS_RUSTC_TEST_SUITE; use std::{fs, iter, thread};
// Test dependencies may need an `extern crate` here to ensure that they show up // Test dependencies may need an `extern crate` here to ensure that they show up
// in the depinfo file (otherwise cargo thinks they are unused) // in the depinfo file (otherwise cargo thinks they are unused)
extern crate clippy_lints;
extern crate clippy_utils;
extern crate futures; extern crate futures;
extern crate if_chain; extern crate if_chain;
extern crate itertools; extern crate itertools;
@ -52,7 +61,7 @@
/// dependencies must be added to Cargo.toml at the project root. Test /// dependencies must be added to Cargo.toml at the project root. Test
/// dependencies that are not *directly* used by this test module require an /// dependencies that are not *directly* used by this test module require an
/// `extern crate` declaration. /// `extern crate` declaration.
static EXTERN_FLAGS: LazyLock<Vec<String>> = LazyLock::new(|| { fn extern_flags() -> Vec<String> {
let current_exe_depinfo = { let current_exe_depinfo = {
let mut path = env::current_exe().unwrap(); let mut path = env::current_exe().unwrap();
path.set_extension("d"); path.set_extension("d");
@ -100,70 +109,93 @@
.into_iter() .into_iter()
.map(|(name, path)| format!("--extern={name}={path}")) .map(|(name, path)| format!("--extern={name}={path}"))
.collect() .collect()
}); }
// whether to run internal tests or not // whether to run internal tests or not
const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal"); const RUN_INTERNAL_TESTS: bool = cfg!(feature = "internal");
fn base_config(test_dir: &str) -> (Config, Args) { struct TestContext {
let mut args = Args::test().unwrap(); args: Args,
args.bless |= var_os("RUSTC_BLESS").is_some_and(|v| v != "0"); extern_flags: Vec<String>,
diagnostic_collector: Option<DiagnosticCollector>,
let target_dir = PathBuf::from(var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into())); collector_thread: Option<thread::JoinHandle<()>>,
let mut config = Config {
output_conflict_handling: OutputConflictHandling::Error,
filter_files: env::var("TESTNAME")
.map(|filters| filters.split(',').map(str::to_string).collect())
.unwrap_or_default(),
target: None,
bless_command: Some("cargo uibless".into()),
out_dir: target_dir.join("ui_test"),
..Config::rustc(Path::new("tests").join(test_dir))
};
config.comment_defaults.base().exit_status = None.into();
config.comment_defaults.base().require_annotations = None.into();
config
.comment_defaults
.base()
.set_custom("rustfix", RustfixMode::Everything);
config.comment_defaults.base().diagnostic_code_prefix = Some(Spanned::dummy("clippy::".into())).into();
config.with_args(&args);
let current_exe_path = env::current_exe().unwrap();
let deps_path = current_exe_path.parent().unwrap();
let profile_path = deps_path.parent().unwrap();
config.program.args.extend(
[
"--emit=metadata",
"-Aunused",
"-Ainternal_features",
"-Zui-testing",
"-Zdeduplicate-diagnostics=no",
"-Dwarnings",
&format!("-Ldependency={}", deps_path.display()),
]
.map(OsString::from),
);
config.program.args.extend(EXTERN_FLAGS.iter().map(OsString::from));
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
config.program.envs.push(("RUSTC_ICE".into(), Some("0".into())));
if let Some(host_libs) = option_env!("HOST_LIBS") {
let dep = format!("-Ldependency={}", Path::new(host_libs).join("deps").display());
config.program.args.push(dep.into());
}
config.program.program = profile_path.join(if cfg!(windows) {
"clippy-driver.exe"
} else {
"clippy-driver"
});
(config, args)
} }
fn run_ui() { impl TestContext {
let (mut config, args) = base_config("ui"); fn new() -> Self {
let mut args = Args::test().unwrap();
args.bless |= var_os("RUSTC_BLESS").is_some_and(|v| v != "0");
let (diagnostic_collector, collector_thread) = var_os("COLLECT_METADATA")
.is_some()
.then(DiagnosticCollector::spawn)
.unzip();
Self {
args,
extern_flags: extern_flags(),
diagnostic_collector,
collector_thread,
}
}
fn base_config(&self, test_dir: &str) -> Config {
let target_dir = PathBuf::from(var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into()));
let mut config = Config {
output_conflict_handling: OutputConflictHandling::Error,
filter_files: env::var("TESTNAME")
.map(|filters| filters.split(',').map(str::to_string).collect())
.unwrap_or_default(),
target: None,
bless_command: Some("cargo uibless".into()),
out_dir: target_dir.join("ui_test"),
..Config::rustc(Path::new("tests").join(test_dir))
};
let defaults = config.comment_defaults.base();
defaults.exit_status = None.into();
defaults.require_annotations = None.into();
defaults.diagnostic_code_prefix = Some(Spanned::dummy("clippy::".into())).into();
defaults.set_custom("rustfix", RustfixMode::Everything);
if let Some(collector) = self.diagnostic_collector.clone() {
defaults.set_custom("diagnostic-collector", collector);
}
config.with_args(&self.args);
let current_exe_path = env::current_exe().unwrap();
let deps_path = current_exe_path.parent().unwrap();
let profile_path = deps_path.parent().unwrap();
config.program.args.extend(
[
"--emit=metadata",
"-Aunused",
"-Ainternal_features",
"-Zui-testing",
"-Zdeduplicate-diagnostics=no",
"-Dwarnings",
&format!("-Ldependency={}", deps_path.display()),
]
.map(OsString::from),
);
config.program.args.extend(self.extern_flags.iter().map(OsString::from));
// Prevent rustc from creating `rustc-ice-*` files the console output is enough.
config.program.envs.push(("RUSTC_ICE".into(), Some("0".into())));
if let Some(host_libs) = option_env!("HOST_LIBS") {
let dep = format!("-Ldependency={}", Path::new(host_libs).join("deps").display());
config.program.args.push(dep.into());
}
config.program.program = profile_path.join(if cfg!(windows) {
"clippy-driver.exe"
} else {
"clippy-driver"
});
config
}
}
fn run_ui(cx: &TestContext) {
let mut config = cx.base_config("ui");
config config
.program .program
.envs .envs
@ -173,30 +205,29 @@ fn run_ui() {
vec![config], vec![config],
ui_test::default_file_filter, ui_test::default_file_filter,
ui_test::default_per_file_config, ui_test::default_per_file_config,
status_emitter::Text::from(args.format), status_emitter::Text::from(cx.args.format),
) )
.unwrap(); .unwrap();
} }
fn run_internal_tests() { fn run_internal_tests(cx: &TestContext) {
// only run internal tests with the internal-tests feature
if !RUN_INTERNAL_TESTS { if !RUN_INTERNAL_TESTS {
return; return;
} }
let (mut config, args) = base_config("ui-internal"); let mut config = cx.base_config("ui-internal");
config.bless_command = Some("cargo uitest --features internal -- -- --bless".into()); config.bless_command = Some("cargo uitest --features internal -- -- --bless".into());
ui_test::run_tests_generic( ui_test::run_tests_generic(
vec![config], vec![config],
ui_test::default_file_filter, ui_test::default_file_filter,
ui_test::default_per_file_config, ui_test::default_per_file_config,
status_emitter::Text::from(args.format), status_emitter::Text::from(cx.args.format),
) )
.unwrap(); .unwrap();
} }
fn run_ui_toml() { fn run_ui_toml(cx: &TestContext) {
let (mut config, args) = base_config("ui-toml"); let mut config = cx.base_config("ui-toml");
config config
.comment_defaults .comment_defaults
@ -214,19 +245,19 @@ fn run_ui_toml() {
.envs .envs
.push(("CLIPPY_CONF_DIR".into(), Some(path.parent().unwrap().into()))); .push(("CLIPPY_CONF_DIR".into(), Some(path.parent().unwrap().into())));
}, },
status_emitter::Text::from(args.format), status_emitter::Text::from(cx.args.format),
) )
.unwrap(); .unwrap();
} }
// Allow `Default::default` as `OptWithSpan` is not nameable // Allow `Default::default` as `OptWithSpan` is not nameable
#[allow(clippy::default_trait_access)] #[allow(clippy::default_trait_access)]
fn run_ui_cargo() { fn run_ui_cargo(cx: &TestContext) {
if IS_RUSTC_TEST_SUITE { if IS_RUSTC_TEST_SUITE {
return; return;
} }
let (mut config, args) = base_config("ui-cargo"); let mut config = cx.base_config("ui-cargo");
config.program.input_file_flag = CommandBuilder::cargo().input_file_flag; config.program.input_file_flag = CommandBuilder::cargo().input_file_flag;
config.program.out_dir_flag = CommandBuilder::cargo().out_dir_flag; config.program.out_dir_flag = CommandBuilder::cargo().out_dir_flag;
config.program.args = vec!["clippy".into(), "--color".into(), "never".into(), "--quiet".into()]; config.program.args = vec!["clippy".into(), "--color".into(), "never".into(), "--quiet".into()];
@ -261,23 +292,25 @@ fn run_ui_cargo() {
.then(|| ui_test::default_any_file_filter(path, config) && !ignored_32bit(path)) .then(|| ui_test::default_any_file_filter(path, config) && !ignored_32bit(path))
}, },
|_config, _file_contents| {}, |_config, _file_contents| {},
status_emitter::Text::from(args.format), status_emitter::Text::from(cx.args.format),
) )
.unwrap(); .unwrap();
} }
fn main() { fn main() {
set_var("CLIPPY_DISABLE_DOCS_LINKS", "true"); set_var("CLIPPY_DISABLE_DOCS_LINKS", "true");
let cx = TestContext::new();
// The SPEEDTEST_* env variables can be used to check Clippy's performance on your PR. It runs the // The SPEEDTEST_* env variables can be used to check Clippy's performance on your PR. It runs the
// affected test 1000 times and gets the average. // affected test 1000 times and gets the average.
if let Ok(speedtest) = std::env::var("SPEEDTEST") { if let Ok(speedtest) = std::env::var("SPEEDTEST") {
println!("----------- STARTING SPEEDTEST -----------"); println!("----------- STARTING SPEEDTEST -----------");
let f = match speedtest.as_str() { let f = match speedtest.as_str() {
"ui" => run_ui as fn(), "ui" => run_ui,
"cargo" => run_ui_cargo as fn(), "cargo" => run_ui_cargo,
"toml" => run_ui_toml as fn(), "toml" => run_ui_toml,
"internal" => run_internal_tests as fn(), "internal" => run_internal_tests,
"ui-cargo-toml-metadata" => ui_cargo_toml_metadata as fn(),
_ => panic!("unknown speedtest: {speedtest} || accepted speedtests are: [ui, cargo, toml, internal]"), _ => panic!("unknown speedtest: {speedtest} || accepted speedtests are: [ui, cargo, toml, internal]"),
}; };
@ -294,7 +327,7 @@ fn main() {
let mut sum = 0; let mut sum = 0;
for _ in 0..iterations { for _ in 0..iterations {
let start = std::time::Instant::now(); let start = std::time::Instant::now();
f(); f(&cx);
sum += start.elapsed().as_millis(); sum += start.elapsed().as_millis();
} }
println!( println!(
@ -303,11 +336,17 @@ fn main() {
sum / u128::from(iterations) sum / u128::from(iterations)
); );
} else { } else {
run_ui(); run_ui(&cx);
run_ui_toml(); run_ui_toml(&cx);
run_ui_cargo(); run_ui_cargo(&cx);
run_internal_tests(); run_internal_tests(&cx);
drop(cx.diagnostic_collector);
ui_cargo_toml_metadata(); ui_cargo_toml_metadata();
if let Some(thread) = cx.collector_thread {
thread.join().unwrap();
}
} }
} }
@ -346,3 +385,180 @@ fn ui_cargo_toml_metadata() {
); );
} }
} }
#[derive(Deserialize)]
#[serde(untagged)]
enum DiagnosticOrMessage {
Diagnostic(Diagnostic),
Message(Message),
}
/// Collects applicabilities from the diagnostics produced for each UI test, producing the
/// `util/gh-pages/lints.json` file used by <https://rust-lang.github.io/rust-clippy/>
#[derive(Debug, Clone)]
struct DiagnosticCollector {
sender: Sender<Vec<u8>>,
}
impl DiagnosticCollector {
#[allow(clippy::assertions_on_constants)]
fn spawn() -> (Self, thread::JoinHandle<()>) {
assert!(!IS_RUSTC_TEST_SUITE && !RUN_INTERNAL_TESTS);
let (sender, receiver) = channel::<Vec<u8>>();
let handle = thread::spawn(|| {
let mut applicabilities = HashMap::new();
for stderr in receiver {
for line in stderr.split(|&byte| byte == b'\n') {
let diag = match serde_json::from_slice(line) {
Ok(DiagnosticOrMessage::Diagnostic(diag)) => diag,
Ok(DiagnosticOrMessage::Message(Message::CompilerMessage(message))) => message.message,
_ => continue,
};
if let Some(lint) = diag.code.as_ref().and_then(|code| code.code.strip_prefix("clippy::")) {
let applicability = applicabilities
.entry(lint.to_string())
.or_insert(Applicability::Unspecified);
let diag_applicability = diag
.children
.iter()
.flat_map(|child| &child.spans)
.filter_map(|span| span.suggestion_applicability.clone())
.max_by_key(applicability_ord);
if let Some(diag_applicability) = diag_applicability
&& applicability_ord(&diag_applicability) > applicability_ord(applicability)
{
*applicability = diag_applicability;
}
}
}
}
let configs = clippy_config::get_configuration_metadata();
let mut metadata: Vec<LintMetadata> = LINTS
.iter()
.map(|lint| LintMetadata::new(lint, &applicabilities, &configs))
.chain(
iter::zip(DEPRECATED, DEPRECATED_VERSION)
.map(|((lint, reason), version)| LintMetadata::new_deprecated(lint, reason, version)),
)
.collect();
metadata.sort_unstable_by(|a, b| a.id.cmp(&b.id));
let json = serde_json::to_string_pretty(&metadata).unwrap();
fs::write("util/gh-pages/lints.json", json).unwrap();
});
(Self { sender }, handle)
}
}
fn applicability_ord(applicability: &Applicability) -> u8 {
match applicability {
Applicability::MachineApplicable => 4,
Applicability::HasPlaceholders => 3,
Applicability::MaybeIncorrect => 2,
Applicability::Unspecified => 1,
_ => unimplemented!(),
}
}
impl Flag for DiagnosticCollector {
fn post_test_action(
&self,
_config: &ui_test::per_test_config::TestConfig<'_>,
_cmd: &mut std::process::Command,
output: &std::process::Output,
_build_manager: &ui_test::build_manager::BuildManager<'_>,
) -> Result<Vec<TestRun>, ui_test::Errored> {
if !output.stderr.is_empty() {
self.sender.send(output.stderr.clone()).unwrap();
}
Ok(Vec::new())
}
fn clone_inner(&self) -> Box<dyn Flag> {
Box::new(self.clone())
}
fn must_be_unique(&self) -> bool {
true
}
}
#[derive(Debug, Serialize)]
struct LintMetadata {
id: String,
id_location: Option<&'static str>,
group: &'static str,
level: &'static str,
docs: String,
version: &'static str,
applicability: Applicability,
}
impl LintMetadata {
fn new(lint: &LintInfo, applicabilities: &HashMap<String, Applicability>, configs: &[ClippyConfiguration]) -> Self {
let name = lint.name_lower();
let applicability = applicabilities
.get(&name)
.cloned()
.unwrap_or(Applicability::Unspecified);
let past_names = RENAMED
.iter()
.filter(|(_, new_name)| new_name.strip_prefix("clippy::") == Some(&name))
.map(|(old_name, _)| old_name.strip_prefix("clippy::").unwrap())
.collect::<Vec<_>>();
let mut docs = lint.explanation.to_string();
if !past_names.is_empty() {
docs.push_str("\n### Past names\n\n");
for past_name in past_names {
writeln!(&mut docs, " * {past_name}").unwrap();
}
}
let configs: Vec<_> = configs
.iter()
.filter(|conf| conf.lints.contains(&name.as_str()))
.collect();
if !configs.is_empty() {
docs.push_str("\n### Configuration\n\n");
for config in configs {
writeln!(&mut docs, "{config}").unwrap();
}
}
Self {
id: name,
id_location: Some(lint.location),
group: lint.category_str(),
level: lint.lint.default_level.as_str(),
docs,
version: lint.version.unwrap(),
applicability,
}
}
fn new_deprecated(name: &str, reason: &str, version: &'static str) -> Self {
// The reason starts with a lowercase letter and ends without a period.
// This needs to be fixed for the website.
let mut reason = reason.to_owned();
if let Some(reason) = reason.get_mut(0..1) {
reason.make_ascii_uppercase();
}
Self {
id: name.strip_prefix("clippy::").unwrap().into(),
id_location: None,
group: "deprecated",
level: "none",
version,
docs: format!(
"### What it does\n\n\
Nothing. This lint has been deprecated\n\n\
### Deprecation reason\n\n{reason}.\n",
),
applicability: Applicability::Unspecified,
}
}
}

76
tests/config-metadata.rs Normal file
View File

@ -0,0 +1,76 @@
use clippy_config::{get_configuration_metadata, ClippyConfiguration};
use itertools::Itertools;
use regex::Regex;
use std::borrow::Cow;
use std::{env, fs};
fn metadata() -> impl Iterator<Item = ClippyConfiguration> {
get_configuration_metadata()
.into_iter()
.filter(|config| config.deprecation_reason.is_none())
.filter(|config| !config.lints.is_empty())
}
#[test]
fn book() {
let path = "book/src/lint_configuration.md";
let current = fs::read_to_string(path).unwrap();
let configs = metadata().map(|conf| conf.to_markdown_paragraph()).join("\n");
let expected = format!(
r#"<!--
This file is generated by `cargo bless --test config-metadata`.
Please use that command to update the file and do not edit it by hand.
-->
# Lint Configuration Options
The following list shows each configuration option, along with a description, its default value, an example
and lints affected.
---
{}
"#,
configs.trim(),
);
if current != expected {
if env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
fs::write(path, expected).unwrap();
} else {
panic!("`{path}` is out of date, run `cargo bless --test config-metadata` to update it");
}
}
}
#[test]
fn changelog() {
let path = "CHANGELOG.md";
let current = fs::read_to_string(path).unwrap();
let configs = metadata().map(|conf| conf.to_markdown_link()).join("\n");
let re = Regex::new(
"(?s)\
(<!-- begin autogenerated links to configuration documentation -->)\
.*\
(<!-- end autogenerated links to configuration documentation -->)\
",
)
.unwrap();
let expected = re.replace(&current, format!("$1\n{configs}\n$2"));
assert!(
matches!(expected, Cow::Owned(_)),
"failed to find configuration section in `{path}`"
);
if current != expected {
if env::var_os("RUSTC_BLESS").is_some_and(|v| v != "0") {
fs::write(path, expected.as_bytes()).unwrap();
} else {
panic!("`{path}` is out of date, run `cargo bless --test config-metadata` to update it");
}
}
}

View File

@ -239,7 +239,7 @@ Otherwise, have a great day =^.^=
<!-- Applicability --> <!-- Applicability -->
<div class="lint-additional-info-item"> <div class="lint-additional-info-item">
<span> Applicability: </span> <span> Applicability: </span>
<span class="label label-default label-applicability">{{lint.applicability.applicability}}</span> <span class="label label-default label-applicability">{{lint.applicability}}</span>
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html#variants">(?)</a> <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html#variants">(?)</a>
</div> </div>
<!-- Clippy version --> <!-- Clippy version -->
@ -252,8 +252,8 @@ Otherwise, have a great day =^.^=
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a> <a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a>
</div> </div>
<!-- Jump to source --> <!-- Jump to source -->
<div class="lint-additional-info-item" ng-if="lint.id_span"> <div class="lint-additional-info-item" ng-if="lint.id_location">
<a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/clippy_lints/{{lint.id_span.path}}#L{{lint.id_span.line}}">View Source</a> <a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/{{lint.id_location}}">View Source</a>
</div> </div>
</div> </div>
</div> </div>

View File

@ -96,13 +96,13 @@
cargo: true, cargo: true,
complexity: true, complexity: true,
correctness: true, correctness: true,
deprecated: false,
nursery: true, nursery: true,
pedantic: true, pedantic: true,
perf: true, perf: true,
restriction: true, restriction: true,
style: true, style: true,
suspicious: true, suspicious: true,
deprecated: false,
} }
$scope.groups = { $scope.groups = {
@ -126,11 +126,10 @@
); );
const APPLICABILITIES_FILTER_DEFAULT = { const APPLICABILITIES_FILTER_DEFAULT = {
Unspecified: true,
Unresolved: true,
MachineApplicable: true, MachineApplicable: true,
MaybeIncorrect: true, MaybeIncorrect: true,
HasPlaceholders: true HasPlaceholders: true,
Unspecified: true,
}; };
$scope.applicabilities = { $scope.applicabilities = {
@ -425,7 +424,7 @@
} }
$scope.byApplicabilities = function (lint) { $scope.byApplicabilities = function (lint) {
return $scope.applicabilities[lint.applicability.applicability]; return $scope.applicabilities[lint.applicability];
}; };
// Show details for one lint // Show details for one lint