Skip to content

Commit 76a75bf

Browse files
committed
Auto merge of rust-lang#12179 - y21:issue12176, r=dswij
[`multiple_crate_versions`]: add a configuration option for allowed duplicate crates Closes rust-lang#12176 changelog: [`multiple_crate_versions`]: add a configuration option for allowed duplicate crates
2 parents 0b6e7e2 + 95a084f commit 76a75bf

File tree

10 files changed

+45
-3
lines changed

10 files changed

+45
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5815,6 +5815,7 @@ Released 2018-09-13
58155815
[`absolute-paths-max-segments`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-max-segments
58165816
[`absolute-paths-allowed-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#absolute-paths-allowed-crates
58175817
[`allowed-dotfiles`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-dotfiles
5818+
[`allowed-duplicate-crates`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allowed-duplicate-crates
58185819
[`enforce-iter-loop-reborrow`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enforce-iter-loop-reborrow
58195820
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items
58205821
[`pub-underscore-fields-behavior`]: https://doc.rust-lang.org/clippy/lint_configuration.html#pub-underscore-fields-behavior

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,16 @@ Additional dotfiles (files or directories starting with a dot) to allow
768768
* [`path_ends_with_ext`](https://rust-lang.github.io/rust-clippy/master/index.html#path_ends_with_ext)
769769

770770

771+
## `allowed-duplicate-crates`
772+
A list of crate names to allow duplicates of
773+
774+
**Default Value:** `[]`
775+
776+
---
777+
**Affected lints:**
778+
* [`multiple_crate_versions`](https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions)
779+
780+
771781
## `enforce-iter-loop-reborrow`
772782
Whether to recommend using implicit into iter for reborrowed values.
773783

clippy_config/src/conf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ define_Conf! {
523523
///
524524
/// Additional dotfiles (files or directories starting with a dot) to allow
525525
(allowed_dotfiles: FxHashSet<String> = FxHashSet::default()),
526+
/// Lint: MULTIPLE_CRATE_VERSIONS.
527+
///
528+
/// A list of crate names to allow duplicates of
529+
(allowed_duplicate_crates: FxHashSet<String> = FxHashSet::default()),
526530
/// Lint: EXPLICIT_ITER_LOOP.
527531
///
528532
/// Whether to recommend using implicit into iter for reborrowed values.

clippy_lints/src/cargo/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod wildcard_dependencies;
66
use cargo_metadata::MetadataCommand;
77
use clippy_utils::diagnostics::span_lint;
88
use clippy_utils::is_lint_allowed;
9+
use rustc_data_structures::fx::FxHashSet;
910
use rustc_hir::hir_id::CRATE_HIR_ID;
1011
use rustc_lint::{LateContext, LateLintPass, Lint};
1112
use rustc_session::impl_lint_pass;
@@ -128,6 +129,8 @@ declare_clippy_lint! {
128129
/// ### Known problems
129130
/// Because this can be caused purely by the dependencies
130131
/// themselves, it's not always possible to fix this issue.
132+
/// In those cases, you can allow that specific crate using
133+
/// the `allowed_duplicate_crates` configuration option.
131134
///
132135
/// ### Example
133136
/// ```toml
@@ -163,6 +166,7 @@ declare_clippy_lint! {
163166
}
164167

165168
pub struct Cargo {
169+
pub allowed_duplicate_crates: FxHashSet<String>,
166170
pub ignore_publish: bool,
167171
}
168172

@@ -208,7 +212,7 @@ impl LateLintPass<'_> for Cargo {
208212
{
209213
match MetadataCommand::new().exec() {
210214
Ok(metadata) => {
211-
multiple_crate_versions::check(cx, &metadata);
215+
multiple_crate_versions::check(cx, &metadata, &self.allowed_duplicate_crates);
212216
},
213217
Err(e) => {
214218
for lint in WITH_DEPS_LINTS {

clippy_lints/src/cargo/multiple_crate_versions.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId};
44
use clippy_utils::diagnostics::span_lint;
55
use itertools::Itertools;
6+
use rustc_data_structures::fx::FxHashSet;
67
use rustc_hir::def_id::LOCAL_CRATE;
78
use rustc_lint::LateContext;
89
use rustc_span::DUMMY_SP;
910

1011
use super::MULTIPLE_CRATE_VERSIONS;
1112

12-
pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) {
13+
pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, allowed_duplicate_crates: &FxHashSet<String>) {
1314
let local_name = cx.tcx.crate_name(LOCAL_CRATE);
1415
let mut packages = metadata.packages.clone();
1516
packages.sort_by(|a, b| a.name.cmp(&b.name));
@@ -31,7 +32,11 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) {
3132
}
3233
})
3334
{
34-
for (name, group) in &packages.iter().group_by(|p| p.name.clone()) {
35+
for (name, group) in &packages
36+
.iter()
37+
.filter(|p| !allowed_duplicate_crates.contains(&p.name))
38+
.group_by(|p| &p.name)
39+
{
3540
let group: Vec<&Package> = group.collect();
3641

3742
if group.len() <= 1 {

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
574574
warn_on_all_wildcard_imports,
575575
check_private_items,
576576
pub_underscore_fields_behavior,
577+
ref allowed_duplicate_crates,
577578

578579
blacklisted_names: _,
579580
cyclomatic_complexity_threshold: _,
@@ -947,6 +948,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
947948
store.register_late_pass(move |_| {
948949
Box::new(cargo::Cargo {
949950
ignore_publish: cargo_ignore_publish,
951+
allowed_duplicate_crates: allowed_duplicate_crates.clone(),
950952
})
951953
});
952954
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "multiple_crate_versions"
3+
version = "0.1.0"
4+
publish = false
5+
6+
[workspace]
7+
8+
[dependencies]
9+
winapi = "0.2"
10+
ansi_term = "=0.11.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allowed-duplicate-crates = ["winapi"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![warn(clippy::multiple_crate_versions)]
2+
3+
fn main() {}

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect
1111
allow-private-module-inception
1212
allow-unwrap-in-tests
1313
allowed-dotfiles
14+
allowed-duplicate-crates
1415
allowed-idents-below-min-chars
1516
allowed-scripts
1617
arithmetic-side-effects-allowed
@@ -87,6 +88,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect
8788
allow-private-module-inception
8889
allow-unwrap-in-tests
8990
allowed-dotfiles
91+
allowed-duplicate-crates
9092
allowed-idents-below-min-chars
9193
allowed-scripts
9294
arithmetic-side-effects-allowed

0 commit comments

Comments
 (0)