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

Replace the metadata collector with tests #13221

Merged
merged 2 commits into from
Aug 13, 2024
Merged
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
6 changes: 3 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[alias]
bless = "test --config env.RUSTC_BLESS='1'"
uitest = "test --test compile-test"
uibless = "test --test compile-test -- -- --bless"
bless = "test -- -- --bless"
uibless = "bless --test compile-test"
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 -- "
collect-metadata = "test --test dogfood --features internal -- collect_metadata"
collect-metadata = "test --test compile-test --config env.COLLECT_METADATA='1'"

[build]
# -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/clippy_bors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ jobs:
- name: Test metadata collection
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:
needs: changelog
runs-on: ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ color-print = "0.3.4"
anstream = "0.6.0"

[dev-dependencies]
cargo_metadata = "0.18.1"
ui_test = "0.25"
regex = "1.5.5"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.122"
toml = "0.7.3"
walkdir = "2.3"
filetime = "0.2.9"
Expand All @@ -41,7 +44,6 @@ itertools = "0.12"
clippy_utils = { path = "clippy_utils" }
if_chain = "1.0"
quote = "1.0.25"
serde = { version = "1.0.145", features = ["derive"] }
syn = { version = "2.0", features = ["full"] }
futures = "0.3"
parking_lot = "0.12"
Expand Down
2 changes: 1 addition & 1 deletion book/src/development/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ for some users. Adding a configuration is done in the following steps:

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_lints` lib file]: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/lib.rs
Expand Down
8 changes: 3 additions & 5 deletions book/src/lint_configuration.md
Original file line number Diff line number Diff line change
@@ -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.
-->

Expand Down Expand Up @@ -199,7 +199,7 @@ Allowed names below the minimum allowed characters. The value `".."` can be used
the list to indicate, that the configured values should be appended to the default
configuration of Clippy. By default, any configuration will replace the default value.

**Default Value:** `["j", "z", "i", "y", "n", "x", "w"]`
**Default Value:** `["i", "j", "x", "y", "z", "w", "n"]`

---
**Affected lints:**
Expand Down Expand Up @@ -455,7 +455,7 @@ default configuration of Clippy. By default, any configuration will replace the
* `doc-valid-idents = ["ClipPy"]` would replace the default list with `["ClipPy"]`.
* `doc-valid-idents = ["ClipPy", ".."]` would append `ClipPy` to the default list.

**Default Value:** `["TiB", "CoreGraphics", "CoffeeScript", "TeX", "Direct2D", "PiB", "DirectX", "NetBSD", "OAuth", "NaN", "OpenType", "WebGL2", "WebTransport", "JavaScript", "OpenSSL", "OpenSSH", "EiB", "PureScript", "OpenAL", "MiB", "WebAssembly", "MinGW", "CoreFoundation", "WebGPU", "ClojureScript", "CamelCase", "OpenDNS", "NaNs", "OpenMP", "GitLab", "KiB", "sRGB", "CoreText", "macOS", "TypeScript", "GiB", "OpenExr", "YCbCr", "OpenTelemetry", "OpenBSD", "FreeBSD", "GPLv2", "PostScript", "WebP", "LaTeX", "TensorFlow", "AccessKit", "TrueType", "OpenStreetMap", "OpenGL", "DevOps", "OCaml", "WebRTC", "WebGL", "BibLaTeX", "GitHub", "GraphQL", "iOS", "Direct3D", "BibTeX", "DirectWrite", "GPLv3", "IPv6", "WebSocket", "IPv4", "ECMAScript"]`
**Default Value:** `["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "AccessKit", "CoreFoundation", "CoreGraphics", "CoreText", "DevOps", "Direct2D", "Direct3D", "DirectWrite", "DirectX", "ECMAScript", "GPLv2", "GPLv3", "GitHub", "GitLab", "IPv4", "IPv6", "ClojureScript", "CoffeeScript", "JavaScript", "PostScript", "PureScript", "TypeScript", "WebAssembly", "NaN", "NaNs", "OAuth", "GraphQL", "OCaml", "OpenAL", "OpenDNS", "OpenGL", "OpenMP", "OpenSSH", "OpenSSL", "OpenStreetMap", "OpenTelemetry", "OpenType", "WebGL", "WebGL2", "WebGPU", "WebRTC", "WebSocket", "WebTransport", "WebP", "OpenExr", "YCbCr", "sRGB", "TensorFlow", "TrueType", "iOS", "macOS", "FreeBSD", "NetBSD", "OpenBSD", "TeX", "LaTeX", "BibTeX", "BibLaTeX", "MinGW", "CamelCase"]`

---
**Affected lints:**
Expand Down Expand Up @@ -949,5 +949,3 @@ Whether to also emit warnings for unsafe blocks with metavariable expansions in
---
**Affected lints:**
* [`macro_metavars_in_unsafe`](https://rust-lang.github.io/rust-clippy/master/index.html#macro_metavars_in_unsafe)


23 changes: 11 additions & 12 deletions clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::msrvs::Msrv;
use crate::types::{DisallowedPath, MacroMatcher, MatchLintBehaviour, PubUnderscoreFieldsBehaviour, Rename};
use crate::ClippyConfiguration;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_session::Session;
use rustc_span::edit_distance::edit_distance;
Expand Down Expand Up @@ -218,7 +217,7 @@ macro_rules! define_Conf {
define_Conf! {
/// Which crates to allow absolute paths from
#[lints(absolute_paths)]
absolute_paths_allowed_crates: FxHashSet<String> = FxHashSet::default(),
absolute_paths_allowed_crates: Vec<String> = Vec::new(),
/// The maximum number of segments a path can have before being linted, anything above this will
/// be linted.
#[lints(absolute_paths)]
Expand Down Expand Up @@ -280,12 +279,12 @@ define_Conf! {
allowed_dotfiles: Vec<String> = Vec::default(),
/// A list of crate names to allow duplicates of
#[lints(multiple_crate_versions)]
allowed_duplicate_crates: FxHashSet<String> = FxHashSet::default(),
allowed_duplicate_crates: Vec<String> = Vec::new(),
/// Allowed names below the minimum allowed characters. The value `".."` can be used as part of
/// the list to indicate, that the configured values should be appended to the default
/// configuration of Clippy. By default, any configuration will replace the default value.
#[lints(min_ident_chars)]
allowed_idents_below_min_chars: FxHashSet<String> =
allowed_idents_below_min_chars: Vec<String> =
DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string).collect(),
/// List of prefixes to allow when determining whether an item's name ends with the module's name.
/// If the rest of an item's name is an allowed prefix (e.g. item `ToFoo` or `to_foo` in module `foo`),
Expand Down Expand Up @@ -323,7 +322,7 @@ define_Conf! {
/// 2. Paths with any segment that containing the word 'prelude'
/// are already allowed by default.
#[lints(wildcard_imports)]
allowed_wildcard_imports: FxHashSet<String> = FxHashSet::default(),
allowed_wildcard_imports: Vec<String> = Vec::new(),
/// Suppress checking of the passed type names in all types of operations.
///
/// If a specific operation is desired, consider using `arithmetic_side_effects_allowed_binary` or `arithmetic_side_effects_allowed_unary` instead.
Expand Down Expand Up @@ -355,7 +354,7 @@ define_Conf! {
/// arithmetic-side-effects-allowed-binary = [["SomeType" , "f32"], ["AnotherType", "*"]]
/// ```
#[lints(arithmetic_side_effects)]
arithmetic_side_effects_allowed_binary: Vec<[String; 2]> = <_>::default(),
arithmetic_side_effects_allowed_binary: Vec<(String, String)> = <_>::default(),
/// Suppress checking of the passed type names in unary operations like "negation" (`-`).
///
/// #### Example
Expand Down Expand Up @@ -431,7 +430,7 @@ define_Conf! {
/// * `doc-valid-idents = ["ClipPy"]` would replace the default list with `["ClipPy"]`.
/// * `doc-valid-idents = ["ClipPy", ".."]` would append `ClipPy` to the default list.
#[lints(doc_markdown)]
doc_valid_idents: FxHashSet<String> = DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string).collect(),
doc_valid_idents: Vec<String> = DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string).collect(),
/// Whether to apply the raw pointer heuristic to determine if a type is `Send`.
#[lints(non_send_fields_in_send_ty)]
enable_raw_pointer_heuristic_for_send: bool = true,
Expand Down Expand Up @@ -706,12 +705,12 @@ fn deserialize(file: &SourceFile) -> TryConf {
DEFAULT_ALLOWED_TRAITS_WITH_RENAMED_PARAMS,
);
// TODO: THIS SHOULD BE TESTED, this comment will be gone soon
if conf.conf.allowed_idents_below_min_chars.contains("..") {
if conf.conf.allowed_idents_below_min_chars.iter().any(|e| e == "..") {
conf.conf
.allowed_idents_below_min_chars
.extend(DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string));
}
if conf.conf.doc_valid_idents.contains("..") {
if conf.conf.doc_valid_idents.iter().any(|e| e == "..") {
conf.conf
.doc_valid_idents
.extend(DEFAULT_DOC_VALID_IDENTS.iter().map(ToString::to_string));
Expand Down Expand Up @@ -890,14 +889,14 @@ fn calculate_dimensions(fields: &[&str]) -> (usize, Vec<usize>) {

#[cfg(test)]
mod tests {
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use serde::de::IgnoredAny;
use std::collections::{HashMap, HashSet};
use std::fs;
use walkdir::WalkDir;

#[test]
fn configs_are_tested() {
let mut names: FxHashSet<String> = crate::get_configuration_metadata()
let mut names: HashSet<String> = crate::get_configuration_metadata()
.into_iter()
.map(|meta| meta.name.replace('_', "-"))
.collect();
Expand All @@ -910,7 +909,7 @@ mod tests {
for entry in toml_files {
let file = fs::read_to_string(entry.path()).unwrap();
#[allow(clippy::zero_sized_map_values)]
if let Ok(map) = toml::from_str::<FxHashMap<String, IgnoredAny>>(&file) {
if let Ok(map) = toml::from_str::<HashMap<String, IgnoredAny>>(&file) {
for name in map.keys() {
names.remove(name.as_str());
}
Expand Down
1 change: 0 additions & 1 deletion clippy_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

extern crate rustc_ast;
extern crate rustc_attr;
extern crate rustc_data_structures;
#[allow(unused_extern_crates)]
extern crate rustc_driver;
extern crate rustc_errors;
Expand Down
2 changes: 1 addition & 1 deletion clippy_dev/src/update_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ fn gen_declared_lints<'a>(
details.sort_unstable();

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 {
if !is_public {
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ declare_clippy_lint! {
}

pub struct Cargo {
allowed_duplicate_crates: &'static FxHashSet<String>,
allowed_duplicate_crates: FxHashSet<String>,
ignore_publish: bool,
}

Expand All @@ -221,7 +221,7 @@ impl_lint_pass!(Cargo => [
impl Cargo {
pub fn new(conf: &'static Conf) -> Self {
Self {
allowed_duplicate_crates: &conf.allowed_duplicate_crates,
allowed_duplicate_crates: conf.allowed_duplicate_crates.iter().cloned().collect(),
ignore_publish: conf.cargo_ignore_publish,
}
}
Expand Down Expand Up @@ -263,7 +263,7 @@ impl LateLintPass<'_> for Cargo {
{
match MetadataCommand::new().exec() {
Ok(metadata) => {
multiple_crate_versions::check(cx, &metadata, self.allowed_duplicate_crates);
multiple_crate_versions::check(cx, &metadata, &self.allowed_duplicate_crates);
},
Err(e) => {
for lint in WITH_DEPS_LINTS {
Expand Down
4 changes: 1 addition & 3 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use that command to update this file and do not edit by hand.
// Manual edits will be overwritten.

pub(crate) static LINTS: &[&crate::LintInfo] = &[
pub static LINTS: &[&crate::LintInfo] = &[
#[cfg(feature = "internal")]
crate::utils::internal_lints::almost_standard_lint_formulation::ALMOST_STANDARD_LINT_FORMULATION_INFO,
#[cfg(feature = "internal")]
Expand All @@ -22,8 +22,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
#[cfg(feature = "internal")]
crate::utils::internal_lints::lint_without_lint_pass::MISSING_CLIPPY_VERSION_ATTRIBUTE_INFO,
#[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,
#[cfg(feature = "internal")]
crate::utils::internal_lints::outer_expn_data_pass::OUTER_EXPN_EXPN_DATA_INFO,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,14 @@ declare_clippy_lint! {
}

pub struct Documentation {
valid_idents: &'static FxHashSet<String>,
valid_idents: FxHashSet<String>,
check_private_items: bool,
}

impl Documentation {
pub fn new(conf: &'static Conf) -> Self {
Self {
valid_idents: &conf.doc_valid_idents,
valid_idents: conf.doc_valid_idents.iter().cloned().collect(),
check_private_items: conf.check_private_items,
}
}
Expand All @@ -452,7 +452,7 @@ impl_lint_pass!(Documentation => [

impl<'tcx> LateLintPass<'tcx> for Documentation {
fn check_attributes(&mut self, cx: &LateContext<'tcx>, attrs: &'tcx [Attribute]) {
let Some(headers) = check_attrs(cx, self.valid_idents, attrs) else {
let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else {
return;
};

Expand Down
48 changes: 34 additions & 14 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ extern crate declare_clippy_lint;
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
mod utils;

mod declared_lints;
mod deprecated_lints;
pub mod declared_lints;
pub mod deprecated_lints;

// begin lints modules, do not remove this comment, it’s used in `update_lints`
mod absolute_paths;
Expand Down Expand Up @@ -440,7 +440,7 @@ impl RegistrationGroups {
}
}

#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub(crate) enum LintCategory {
Cargo,
Complexity,
Expand Down Expand Up @@ -479,11 +479,39 @@ impl LintCategory {
}
}

pub(crate) struct LintInfo {
pub struct LintInfo {
/// Double reference to maintain pointer equality
lint: &'static &'static Lint,
pub lint: &'static &'static Lint,
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 {
Expand Down Expand Up @@ -538,14 +566,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
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 = format_args_storage.clone();
store.register_early_pass(move || {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/min_ident_chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ declare_clippy_lint! {
impl_lint_pass!(MinIdentChars => [MIN_IDENT_CHARS]);

pub struct MinIdentChars {
allowed_idents_below_min_chars: &'static FxHashSet<String>,
allowed_idents_below_min_chars: FxHashSet<String>,
min_ident_chars_threshold: u64,
}

impl MinIdentChars {
pub fn new(conf: &'static Conf) -> Self {
Self {
allowed_idents_below_min_chars: &conf.allowed_idents_below_min_chars,
allowed_idents_below_min_chars: conf.allowed_idents_below_min_chars.iter().cloned().collect(),
min_ident_chars_threshold: conf.min_ident_chars_threshold,
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/operators/arithmetic_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ArithmeticSideEffects {
("f64", FxHashSet::from_iter(["f64"])),
("std::string::String", FxHashSet::from_iter(["str"])),
]);
for [lhs, rhs] in &conf.arithmetic_side_effects_allowed_binary {
for (lhs, rhs) in &conf.arithmetic_side_effects_allowed_binary {
allowed_binary.entry(lhs).or_default().insert(rhs);
}
for s in &conf.arithmetic_side_effects_allowed {
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod collapsible_calls;
pub mod interning_defined_symbol;
pub mod invalid_paths;
pub mod lint_without_lint_pass;
pub mod metadata_collector;
pub mod msrv_attr_impl;
pub mod outer_expn_data_pass;
pub mod produce_ice;
Expand Down
Loading
Loading