[multiple_crate_versions]: add a configuration option for allowed duplicate dependencies

This commit is contained in:
y21 2024-01-21 03:21:54 +01:00
parent 64d08a8b1d
commit 95a084f2eb
10 changed files with 45 additions and 3 deletions

View File

@ -5815,6 +5815,7 @@ Released 2018-09-13
[`absolute-paths-max-segments`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-max-segments [`absolute-paths-max-segments`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-max-segments
[`absolute-paths-allowed-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-allowed-crates [`absolute-paths-allowed-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-allowed-crates
[`allowed-dotfiles`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-dotfiles [`allowed-dotfiles`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-dotfiles
[`allowed-duplicate-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-duplicate-crates
[`enforce-iter-loop-reborrow`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enforce-iter-loop-reborrow [`enforce-iter-loop-reborrow`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enforce-iter-loop-reborrow
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items [`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior [`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior

View File

@ -768,6 +768,16 @@ Additional dotfiles (files or directories starting with a dot) to allow
* [`path_ends_with_ext`](https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext) * [`path_ends_with_ext`](https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext)
## `allowed-duplicate-crates`
A list of crate names to allow duplicates of
**Default Value:** `[]`
---
**Affected lints:**
* [`multiple_crate_versions`](https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions)
## `enforce-iter-loop-reborrow` ## `enforce-iter-loop-reborrow`
Whether to recommend using implicit into iter for reborrowed values. Whether to recommend using implicit into iter for reborrowed values.

View File

@ -523,6 +523,10 @@ pub fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
/// ///
/// Additional dotfiles (files or directories starting with a dot) to allow /// Additional dotfiles (files or directories starting with a dot) to allow
(allowed_dotfiles: FxHashSet<String> = FxHashSet::default()), (allowed_dotfiles: FxHashSet<String> = FxHashSet::default()),
/// Lint: MULTIPLE_CRATE_VERSIONS.
///
/// A list of crate names to allow duplicates of
(allowed_duplicate_crates: FxHashSet<String> = FxHashSet::default()),
/// Lint: EXPLICIT_ITER_LOOP. /// Lint: EXPLICIT_ITER_LOOP.
/// ///
/// Whether to recommend using implicit into iter for reborrowed values. /// Whether to recommend using implicit into iter for reborrowed values.

View File

@ -6,6 +6,7 @@
use cargo_metadata::MetadataCommand; use cargo_metadata::MetadataCommand;
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_lint_allowed; use clippy_utils::is_lint_allowed;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::hir_id::CRATE_HIR_ID; use rustc_hir::hir_id::CRATE_HIR_ID;
use rustc_lint::{LateContext, LateLintPass, Lint}; use rustc_lint::{LateContext, LateLintPass, Lint};
use rustc_session::impl_lint_pass; use rustc_session::impl_lint_pass;
@ -128,6 +129,8 @@
/// ### Known problems /// ### Known problems
/// Because this can be caused purely by the dependencies /// Because this can be caused purely by the dependencies
/// themselves, it's not always possible to fix this issue. /// themselves, it's not always possible to fix this issue.
/// In those cases, you can allow that specific crate using
/// the `allowed_duplicate_crates` configuration option.
/// ///
/// ### Example /// ### Example
/// ```toml /// ```toml
@ -163,6 +166,7 @@
} }
pub struct Cargo { pub struct Cargo {
pub allowed_duplicate_crates: FxHashSet<String>,
pub ignore_publish: bool, pub ignore_publish: bool,
} }
@ -208,7 +212,7 @@ fn check_crate(&mut self, cx: &LateContext<'_>) {
{ {
match MetadataCommand::new().exec() { match MetadataCommand::new().exec() {
Ok(metadata) => { Ok(metadata) => {
multiple_crate_versions::check(cx, &metadata); multiple_crate_versions::check(cx, &metadata, &self.allowed_duplicate_crates);
}, },
Err(e) => { Err(e) => {
for lint in WITH_DEPS_LINTS { for lint in WITH_DEPS_LINTS {

View File

@ -3,13 +3,14 @@
use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId}; use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId};
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::span_lint;
use itertools::Itertools; use itertools::Itertools;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::LOCAL_CRATE; use rustc_hir::def_id::LOCAL_CRATE;
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_span::DUMMY_SP; use rustc_span::DUMMY_SP;
use super::MULTIPLE_CRATE_VERSIONS; use super::MULTIPLE_CRATE_VERSIONS;
pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) { pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, allowed_duplicate_crates: &FxHashSet<String>) {
let local_name = cx.tcx.crate_name(LOCAL_CRATE); let local_name = cx.tcx.crate_name(LOCAL_CRATE);
let mut packages = metadata.packages.clone(); let mut packages = metadata.packages.clone();
packages.sort_by(|a, b| a.name.cmp(&b.name)); packages.sort_by(|a, b| a.name.cmp(&b.name));
@ -31,7 +32,11 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) {
} }
}) })
{ {
for (name, group) in &packages.iter().group_by(|p| p.name.clone()) { for (name, group) in &packages
.iter()
.filter(|p| !allowed_duplicate_crates.contains(&p.name))
.group_by(|p| &p.name)
{
let group: Vec<&Package> = group.collect(); let group: Vec<&Package> = group.collect();
if group.len() <= 1 { if group.len() <= 1 {

View File

@ -574,6 +574,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
warn_on_all_wildcard_imports, warn_on_all_wildcard_imports,
check_private_items, check_private_items,
pub_underscore_fields_behavior, pub_underscore_fields_behavior,
ref allowed_duplicate_crates,
blacklisted_names: _, blacklisted_names: _,
cyclomatic_complexity_threshold: _, cyclomatic_complexity_threshold: _,
@ -947,6 +948,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(move |_| { store.register_late_pass(move |_| {
Box::new(cargo::Cargo { Box::new(cargo::Cargo {
ignore_publish: cargo_ignore_publish, ignore_publish: cargo_ignore_publish,
allowed_duplicate_crates: allowed_duplicate_crates.clone(),
}) })
}); });
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef)); store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));

View File

@ -0,0 +1,10 @@
[package]
name = "multiple_crate_versions"
version = "0.1.0"
publish = false
[workspace]
[dependencies]
winapi = "0.2"
ansi_term = "=0.11.0"

View File

@ -0,0 +1 @@
allowed-duplicate-crates = ["winapi"]

View File

@ -0,0 +1,3 @@
#![warn(clippy::multiple_crate_versions)]
fn main() {}

View File

@ -11,6 +11,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
allow-private-module-inception allow-private-module-inception
allow-unwrap-in-tests allow-unwrap-in-tests
allowed-dotfiles allowed-dotfiles
allowed-duplicate-crates
allowed-idents-below-min-chars allowed-idents-below-min-chars
allowed-scripts allowed-scripts
arithmetic-side-effects-allowed arithmetic-side-effects-allowed
@ -87,6 +88,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
allow-private-module-inception allow-private-module-inception
allow-unwrap-in-tests allow-unwrap-in-tests
allowed-dotfiles allowed-dotfiles
allowed-duplicate-crates
allowed-idents-below-min-chars allowed-idents-below-min-chars
allowed-scripts allowed-scripts
arithmetic-side-effects-allowed arithmetic-side-effects-allowed