-
Notifications
You must be signed in to change notification settings - Fork 1.7k
trait bounds lint - repeated types #3766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f71d59e
Lint for type repetition in trait bounds.
xd009642 f3e4467
Changed Ty to ty, added lifetime 'tcx
xd009642 7921531
Fix some of the compile errors
xd009642 c025917
Fixed more compile errors
xd009642 7853dac
Responded to comments and fixed compile bug
xd009642 cac69ec
Respond to comments and improve printout
xd009642 5e9906b
Added doc comment fixed type printout
xd009642 c962ddb
Updated test stderr
xd009642 925e820
Respond to review comments
xd009642 bba2c7f
Updated tests.
xd009642 ad63719
Hash discriminant of Lifetime::Name
xd009642 03c5435
Hash discriminant of lifetime.name
xd009642 78ebcaa
Fix dogfood test
xd009642 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::utils::{in_macro, snippet, span_help_and_lint, SpanlessHash}; | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_data_structures::fx::FxHashMap; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct TraitBounds; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** This lint warns about unnecessary type repetitions in trait bounds | ||
/// | ||
/// **Why is this bad?** Repeating the type for every bound makes the code | ||
/// less readable than combining the bounds | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// pub fn foo<T>(t: T) where T: Copy, T: Clone | ||
/// ``` | ||
/// | ||
/// Could be written as: | ||
/// | ||
/// ```rust | ||
/// pub fn foo<T>(t: T) where T: Copy + Clone | ||
/// ``` | ||
pub TYPE_REPETITION_IN_BOUNDS, | ||
complexity, | ||
"Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`" | ||
} | ||
|
||
impl_lint_pass!(TraitBounds => [TYPE_REPETITION_IN_BOUNDS]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds { | ||
fn check_generics(&mut self, cx: &LateContext<'a, 'tcx>, gen: &'tcx Generics) { | ||
if in_macro(gen.span) { | ||
return; | ||
} | ||
let hash = |ty| -> u64 { | ||
let mut hasher = SpanlessHash::new(cx, cx.tables); | ||
hasher.hash_ty(ty); | ||
hasher.finish() | ||
}; | ||
let mut map = FxHashMap::default(); | ||
for bound in &gen.where_clause.predicates { | ||
if let WherePredicate::BoundPredicate(ref p) = bound { | ||
let h = hash(&p.bounded_ty); | ||
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>()) { | ||
let mut hint_string = format!( | ||
"consider combining the bounds: `{}:", | ||
snippet(cx, p.bounded_ty.span, "_") | ||
); | ||
for b in v.iter() { | ||
if let GenericBound::Trait(ref poly_trait_ref, _) = b { | ||
let path = &poly_trait_ref.trait_ref.path; | ||
hint_string.push_str(&format!(" {} +", path)); | ||
} | ||
} | ||
for b in p.bounds.iter() { | ||
if let GenericBound::Trait(ref poly_trait_ref, _) = b { | ||
let path = &poly_trait_ref.trait_ref.path; | ||
hint_string.push_str(&format!(" {} +", path)); | ||
} | ||
} | ||
hint_string.truncate(hint_string.len() - 2); | ||
hint_string.push('`'); | ||
span_help_and_lint( | ||
cx, | ||
TYPE_REPETITION_IN_BOUNDS, | ||
p.span, | ||
"this type has already been used as a bound predicate", | ||
&hint_string, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[deny(clippy::type_repetition_in_bounds)] | ||
|
||
pub fn foo<T>(_t: T) | ||
where | ||
T: Copy, | ||
T: Clone, | ||
{ | ||
unimplemented!(); | ||
} | ||
|
||
pub fn bar<T, U>(_t: T, _u: U) | ||
where | ||
T: Copy, | ||
U: Clone, | ||
{ | ||
unimplemented!(); | ||
} | ||
|
||
fn main() {} |
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,15 @@ | ||
error: this type has already been used as a bound predicate | ||
--> $DIR/type_repetition_in_bounds.rs:6:5 | ||
| | ||
LL | T: Clone, | ||
| ^^^^^^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/type_repetition_in_bounds.rs:1:8 | ||
| | ||
LL | #[deny(clippy::type_repetition_in_bounds)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= help: consider combining the bounds: `T: Copy + Clone` | ||
|
||
error: aborting due to previous error | ||
|
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.