-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add lint_duplicate_trait for duplicate specified traits in a trait object
#110991
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use crate::hir; | ||
|
|
||
| use crate::{lints::DuplicateTraitDiag, LateContext, LateLintPass}; | ||
|
|
||
| declare_lint! { | ||
| /// The `lint_duplicate_trait` lints repetition of traits. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,compile_fail | ||
| /// fn foo(_: &(dyn MyTrait + Send + Send>) {} | ||
| /// ``` | ||
| /// | ||
| /// {{produces}} | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// Duplicate trait `Send` in trait object. | ||
| pub DUPLICATE_TRAIT, | ||
| Warn, | ||
| "duplicate trait constraint in trait object" | ||
| } | ||
|
|
||
| declare_lint_pass!(DuplicateTrait => [DUPLICATE_TRAIT]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for DuplicateTrait { | ||
| fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'tcx>) { | ||
| let hir::TyKind::Ref( | ||
| .., | ||
| hir::MutTy { | ||
| ty: hir::Ty { | ||
| kind: hir::TyKind::TraitObject(bounds, ..), | ||
| .. | ||
| }, | ||
| .. | ||
| } | ||
| ) = ty.kind else { return; }; | ||
|
|
||
| let mut bounds = (*bounds).to_owned(); | ||
|
|
||
| bounds.sort_unstable_by_key(|b| b.trait_ref.trait_def_id()); | ||
|
|
||
| if bounds.len() < 2 { | ||
| return; | ||
| } | ||
|
|
||
| let mut last_bound = &bounds[0]; | ||
| for bound in bounds.iter().skip(1) { | ||
| if last_bound.trait_ref.trait_def_id() == bound.trait_ref.trait_def_id() | ||
| && let Some(def_id) = bound.trait_ref.trait_def_id() { | ||
| cx.tcx.emit_spanned_lint( | ||
| DUPLICATE_TRAIT, | ||
| bound.trait_ref.hir_ref_id, // is this correct? | ||
| bound.span, | ||
| DuplicateTraitDiag { | ||
| trait_name: cx.tcx.item_name(def_id), | ||
| suggestion: bound.span | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| last_bound = bound; | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // check-fail | ||
| #![warn(duplicate_trait)] | ||
|
|
||
| use std::any::Any; | ||
|
|
||
| fn main() {} | ||
|
|
||
| fn fine(_a: &(dyn Any + Send)) {} | ||
|
|
||
| fn duplicate_once(_a: &(dyn Any + Send + Send)) {} //~WARNING duplicate trait | ||
|
|
||
| fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} //~WARNING duplicate trait | ||
|
|
||
| fn duplicate_out_of_order(_a: &(dyn Any + Send + Sync + Send)) {} //~WARNING duplicate trait | ||
|
|
||
| fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} //~WARNING duplicate trait |
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,43 @@ | ||
| warning: trait `Send` was already specified | ||
| --> $DIR/duplicate-trait.rs:9:42 | ||
| | | ||
| LL | fn duplicate_once(_a: &(dyn Any + Send + Send)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
| | | ||
| note: the lint level is defined here | ||
| --> $DIR/duplicate-trait.rs:1:9 | ||
| | | ||
| LL | #![warn(duplicate_trait)] | ||
| | ^^^^^^^^^^^^^^^ | ||
|
|
||
| warning: trait `Send` was already specified | ||
| --> $DIR/duplicate-trait.rs:11:43 | ||
| | | ||
| LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
|
|
||
| warning: trait `Send` was already specified | ||
| --> $DIR/duplicate-trait.rs:11:50 | ||
| | | ||
| LL | fn duplicate_twice(_a: &(dyn Any + Send + Send + Send)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
|
|
||
| warning: trait `Send` was already specified | ||
| --> $DIR/duplicate-trait.rs:13:57 | ||
| | | ||
| LL | fn duplicate_out_of_order(_a: &(dyn Any + Send + Sync + Send)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
|
|
||
| warning: trait `Send` was already specified | ||
| --> $DIR/duplicate-trait.rs:15:53 | ||
| | | ||
| LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
|
|
||
| warning: trait `Sync` was already specified | ||
| --> $DIR/duplicate-trait.rs:15:60 | ||
| | | ||
| LL | fn duplicate_multiple(_a: &(dyn Any + Send + Sync + Send + Sync)) {} | ||
| | ^^^^ help: remove duplicate trait | ||
|
|
||
| warning: 6 warnings emitted |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DefIds are not stable across compilations and should not be used for sorting like here. Instead of what you're doing right now, you should use a
FxHashSetand iterate through the bounds (in source order) and insert their def ids into the set. If a def id is already in the set, emit the lint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will make that change. Out of curiosity, why does the fact they're unstable across compilations matter? The only aim of the sort is to group like
DefIds together, which should still be valid given it is within one compilation right?