-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(multiple_inherent_impl): Add config option to target specific scope #15843
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
paulmialane
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
paulmialane:feat/multiple_inherent_impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b2b0a44
feat(multiple_inherent_impl): Add config option to target specific scope
paulmialane fa88945
fix(multiple_inherent_impl): add lowercase config option + fix typo
paulmialane f287b42
fix(multiple_inherent_impl): Criterion not needed in the value
paulmialane 21e0f33
WIP: Error using `cargo uibless`
paulmialane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,17 +1,23 @@ | ||||||||||||||||||||||||||||||
use clippy_config::Conf; | ||||||||||||||||||||||||||||||
use clippy_config::types::InherentImplLintScope; | ||||||||||||||||||||||||||||||
use clippy_utils::diagnostics::span_lint_and_then; | ||||||||||||||||||||||||||||||
use clippy_utils::is_lint_allowed; | ||||||||||||||||||||||||||||||
use rustc_data_structures::fx::FxHashMap; | ||||||||||||||||||||||||||||||
use rustc_hir::def_id::LocalDefId; | ||||||||||||||||||||||||||||||
use rustc_hir::def_id::{LocalDefId, LocalModDefId}; | ||||||||||||||||||||||||||||||
use rustc_hir::{Item, ItemKind, Node}; | ||||||||||||||||||||||||||||||
use rustc_lint::{LateContext, LateLintPass}; | ||||||||||||||||||||||||||||||
use rustc_session::declare_lint_pass; | ||||||||||||||||||||||||||||||
use rustc_span::Span; | ||||||||||||||||||||||||||||||
use rustc_session::impl_lint_pass; | ||||||||||||||||||||||||||||||
use rustc_span::{FileName, Span}; | ||||||||||||||||||||||||||||||
use std::collections::hash_map::Entry; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
declare_clippy_lint! { | ||||||||||||||||||||||||||||||
/// ### What it does | ||||||||||||||||||||||||||||||
/// Checks for multiple inherent implementations of a struct | ||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||
/// The config option controls the scope in which multiple inherent `impl` blocks for the same | ||||||||||||||||||||||||||||||
/// struct are linted, allowing values of `module` (only within the same module), `file` | ||||||||||||||||||||||||||||||
/// (within the same file), or `crate` (anywhere in the crate, default). | ||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||
/// ### Why restrict this? | ||||||||||||||||||||||||||||||
/// Splitting the implementation of a type makes the code harder to navigate. | ||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||
|
@@ -41,7 +47,26 @@ declare_clippy_lint! { | |||||||||||||||||||||||||||||
"Multiple inherent impl that could be grouped" | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
declare_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]); | ||||||||||||||||||||||||||||||
impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
pub struct MultipleInherentImpl { | ||||||||||||||||||||||||||||||
scope: InherentImplLintScope, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
impl MultipleInherentImpl { | ||||||||||||||||||||||||||||||
pub fn new(conf: &'static Conf) -> Self { | ||||||||||||||||||||||||||||||
Self { | ||||||||||||||||||||||||||||||
scope: conf.inherent_impl_lint_scope, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[derive(Hash, Eq, PartialEq, Clone)] | ||||||||||||||||||||||||||||||
enum Criterion { | ||||||||||||||||||||||||||||||
Module(LocalModDefId), | ||||||||||||||||||||||||||||||
File(FileName), | ||||||||||||||||||||||||||||||
Crate, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl { | ||||||||||||||||||||||||||||||
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) { | ||||||||||||||||||||||||||||||
|
@@ -66,7 +91,26 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl { | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
for impl_id in impl_ids.iter().map(|id| id.expect_local()) { | ||||||||||||||||||||||||||||||
let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity(); | ||||||||||||||||||||||||||||||
match type_map.entry(impl_ty) { | ||||||||||||||||||||||||||||||
let hir_id = cx.tcx.local_def_id_to_hir_id(impl_id); | ||||||||||||||||||||||||||||||
let criterion = match self.scope { | ||||||||||||||||||||||||||||||
InherentImplLintScope::Module => Criterion::Module(cx.tcx.parent_module(hir_id)), | ||||||||||||||||||||||||||||||
InherentImplLintScope::File => { | ||||||||||||||||||||||||||||||
if let Node::Item(&Item { | ||||||||||||||||||||||||||||||
kind: ItemKind::Impl(_impl_id), | ||||||||||||||||||||||||||||||
span, | ||||||||||||||||||||||||||||||
.. | ||||||||||||||||||||||||||||||
}) = cx.tcx.hir_node(hir_id) | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
Criterion::File(cx.tcx.sess.source_map().lookup_source_file(span.lo()).name.clone()) | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
// We know we are working on an impl, so the pattern matching can | ||||||||||||||||||||||||||||||
// not fail | ||||||||||||||||||||||||||||||
unreachable!() | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
if let Node::Item(&Item { | |
kind: ItemKind::Impl(_impl_id), | |
span, | |
.. | |
}) = cx.tcx.hir_node(hir_id) | |
{ | |
Criterion::File(cx.tcx.sess.source_map().lookup_source_file(span.lo()).name.clone()) | |
} else { | |
// We know we are working on an impl, so the pattern matching can | |
// not fail | |
unreachable!() | |
} | |
let span = cx.tcx.hir_span(hir_id); | |
Criterion::File(cx.tcx.sess.source_map().lookup_source_file(span.lo()).name.clone()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tests/ui-cargo/multiple_inherent_impl/config_fail/Cargo.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
error: error reading Clippy's configuration file: unknown variant `FooBar`, expected one of `crate`, `file`, `module` | ||
--> $DIR/tests/ui-cargo/multiple_inherent_impl/config_fail/clippy.toml:1:28 | ||
| | ||
1 | inherent-impl-lint-scope = "FooBar" | ||
| ^^^^^^^^ | ||
|
||
error: could not compile `config_fail` (bin "config_fail") due to 1 previous error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "config_fail" | ||
version = "0.1.0" | ||
edition = "2024" | ||
publish = false | ||
|
||
[dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
inherent-impl-lint-scope = "FooBar" |
3 changes: 3 additions & 0 deletions
3
tests/ui-cargo/multiple_inherent_impl/config_fail/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#![allow(dead_code)] | ||
#![deny(clippy::multiple_inherent_impl)] | ||
fn main() {} |
57 changes: 57 additions & 0 deletions
57
tests/ui-cargo/multiple_inherent_impl/crate_fail/Cargo.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
error: multiple implementations of this structure | ||
--> src/main.rs:11:1 | ||
| | ||
11 | / impl S { | ||
12 | | //^ Must trigger | ||
13 | | fn second() {} | ||
14 | | } | ||
| |_^ | ||
| | ||
note: first implementation here | ||
--> src/main.rs:7:1 | ||
| | ||
7 | / impl S { | ||
8 | | fn first() {} | ||
9 | | } | ||
| |_^ | ||
note: the lint level is defined here | ||
--> src/main.rs:2:9 | ||
| | ||
2 | #![deny(clippy::multiple_inherent_impl)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: multiple implementations of this structure | ||
--> src/main.rs:22:5 | ||
| | ||
22 | / impl T { | ||
23 | | //^ Must trigger | ||
24 | | fn second() {} | ||
25 | | } | ||
| |_____^ | ||
| | ||
note: first implementation here | ||
--> src/main.rs:16:1 | ||
| | ||
16 | / impl T { | ||
17 | | fn first() {} | ||
18 | | } | ||
| |_^ | ||
|
||
error: multiple implementations of this structure | ||
--> src/main.rs:36:1 | ||
| | ||
36 | / impl b::T { | ||
37 | | //^ Must trigger | ||
38 | | fn second() {} | ||
39 | | } | ||
| |_^ | ||
| | ||
note: first implementation here | ||
--> src/b.rs:4:1 | ||
| | ||
4 | / impl T { | ||
5 | | fn first() {} | ||
6 | | } | ||
| |_^ | ||
|
||
error: could not compile `crate_fail` (bin "crate_fail") due to 3 previous errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "crate_fail" | ||
version = "0.1.0" | ||
edition = "2024" | ||
publish = false | ||
|
||
[dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
inherent-impl-lint-scope = "crate" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub struct S; | ||
pub struct T; | ||
|
||
impl T { | ||
fn first() {} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/ui-cargo/multiple_inherent_impl/crate_fail/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#![allow(dead_code)] | ||
#![deny(clippy::multiple_inherent_impl)] | ||
|
||
struct S; | ||
struct T; | ||
|
||
impl S { | ||
fn first() {} | ||
} | ||
|
||
impl S { | ||
//^ Must trigger | ||
fn second() {} | ||
} | ||
|
||
impl T { | ||
fn first() {} | ||
} | ||
|
||
mod a { | ||
use super::T; | ||
impl T { | ||
//^ Must trigger | ||
fn second() {} | ||
} | ||
} | ||
|
||
mod b; | ||
|
||
impl b::S { | ||
//^ Must NOT trigger | ||
fn first() {} | ||
fn second() {} | ||
} | ||
|
||
impl b::T { | ||
//^ Must trigger | ||
fn second() {} | ||
} | ||
|
||
fn main() {} |
58 changes: 58 additions & 0 deletions
58
tests/ui-cargo/multiple_inherent_impl/file_fail/Cargo.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
error: multiple implementations of this structure | ||
--> src/main.rs:13:5 | ||
| | ||
13 | / impl S { | ||
14 | | //^ Must trigger | ||
15 | | fn second() {} | ||
16 | | } | ||
| |_____^ | ||
| | ||
note: first implementation here | ||
--> src/main.rs:6:1 | ||
| | ||
6 | / impl S { | ||
7 | | fn first() {} | ||
8 | | } | ||
| |_^ | ||
note: the lint level is defined here | ||
--> src/main.rs:2:9 | ||
| | ||
2 | #![deny(clippy::multiple_inherent_impl)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: multiple implementations of this structure | ||
--> src/main.rs:26:5 | ||
| | ||
26 | / impl S { | ||
27 | | //^ Must trigger | ||
28 | | | ||
29 | | fn second() {} | ||
30 | | } | ||
| |_____^ | ||
| | ||
note: first implementation here | ||
--> src/main.rs:22:5 | ||
| | ||
22 | / impl S { | ||
23 | | fn first() {} | ||
24 | | } | ||
| |_____^ | ||
|
||
error: multiple implementations of this structure | ||
--> src/c.rs:17:5 | ||
| | ||
17 | / impl T { | ||
18 | | //^ Must trigger | ||
19 | | fn second() {} | ||
20 | | } | ||
| |_____^ | ||
| | ||
note: first implementation here | ||
--> src/c.rs:10:5 | ||
| | ||
10 | / impl T { | ||
11 | | fn first() {} | ||
12 | | } | ||
| |_____^ | ||
|
||
error: could not compile `file_fail` (bin "file_fail") due to 3 previous errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "file_fail" | ||
version = "0.1.0" | ||
edition = "2024" | ||
publish = false | ||
|
||
[dependencies] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
inherent-impl-lint-scope = "file" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pub struct S; | ||
struct T; | ||
|
||
impl S { | ||
fn first() {} | ||
} | ||
|
||
mod d { | ||
use super::T; | ||
impl T { | ||
fn first() {} | ||
} | ||
} | ||
|
||
mod e { | ||
use super::T; | ||
impl T { | ||
//^ Must trigger | ||
fn second() {} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.