Skip to content

Commit 925e820

Browse files
committed
Respond to review comments
Update README and CHANGELOG using the util scripts, refine the help message and fix the float_cmp error.
1 parent c962ddb commit 925e820

File tree

8 files changed

+57
-36
lines changed

8 files changed

+57
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ Released 2018-09-13
11381138
[`trivially_copy_pass_by_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref
11391139
[`try_err`]: https://rust-lang.github.io/rust-clippy/master/index.html#try_err
11401140
[`type_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
1141+
[`type_repetition_in_bounds`]: https://rust-lang.github.io/rust-clippy/master/index.html#type_repetition_in_bounds
11411142
[`unicode_not_nfc`]: https://rust-lang.github.io/rust-clippy/master/index.html#unicode_not_nfc
11421143
[`unimplemented`]: https://rust-lang.github.io/rust-clippy/master/index.html#unimplemented
11431144
[`unit_arg`]: https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 308 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 309 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ pub mod strings;
263263
pub mod suspicious_trait_impl;
264264
pub mod swap;
265265
pub mod temporary_assignment;
266+
pub mod trait_bounds;
266267
pub mod transmute;
267268
pub mod transmuting_null;
268-
pub mod trait_bounds;
269269
pub mod trivially_copy_pass_by_ref;
270270
pub mod try_err;
271271
pub mod types;
@@ -860,6 +860,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
860860
swap::ALMOST_SWAPPED,
861861
swap::MANUAL_SWAP,
862862
temporary_assignment::TEMPORARY_ASSIGNMENT,
863+
trait_bounds::TYPE_REPETITION_IN_BOUNDS,
863864
transmute::CROSSPOINTER_TRANSMUTE,
864865
transmute::TRANSMUTE_BYTES_TO_STR,
865866
transmute::TRANSMUTE_INT_TO_BOOL,
@@ -870,7 +871,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
870871
transmute::USELESS_TRANSMUTE,
871872
transmute::WRONG_TRANSMUTE,
872873
transmuting_null::TRANSMUTING_NULL,
873-
trait_bounds::TYPE_REPETITION_IN_BOUNDS,
874874
trivially_copy_pass_by_ref::TRIVIALLY_COPY_PASS_BY_REF,
875875
try_err::TRY_ERR,
876876
types::ABSURD_EXTREME_COMPARISONS,
@@ -1042,6 +1042,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
10421042
reference::REF_IN_DEREF,
10431043
swap::MANUAL_SWAP,
10441044
temporary_assignment::TEMPORARY_ASSIGNMENT,
1045+
trait_bounds::TYPE_REPETITION_IN_BOUNDS,
10451046
transmute::CROSSPOINTER_TRANSMUTE,
10461047
transmute::TRANSMUTE_BYTES_TO_STR,
10471048
transmute::TRANSMUTE_INT_TO_BOOL,

clippy_lints/src/trait_bounds.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use crate::utils::{in_macro, snippet, span_help_and_lint, SpanlessHash};
2+
use rustc::hir::*;
23
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
34
use rustc::{declare_tool_lint, impl_lint_pass};
45
use rustc_data_structures::fx::FxHashMap;
5-
use rustc::hir::*;
66

77
#[derive(Copy, Clone)]
88
pub struct TraitBounds;
99

1010
declare_clippy_lint! {
1111
/// **What it does:** This lint warns about unnecessary type repetitions in trait bounds
1212
///
13-
/// **Why is this bad?** Complexity
13+
/// **Why is this bad?** Repeating the type for every bound makes the code
14+
/// less readable than combining the bounds
1415
///
1516
/// **Example:**
1617
/// ```rust
17-
/// pub fn foo<T>(t: T) where T: Copy, T: Clone
18+
/// pub fn foo<T>(t: T) where T: Copy, T: Clone
1819
/// ```
1920
///
2021
/// Could be written as:
@@ -34,7 +35,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
3435
if in_macro(gen.span) {
3536
return;
3637
}
37-
let hash = | ty | -> u64 {
38+
let hash = |ty| -> u64 {
3839
let mut hasher = SpanlessHash::new(cx, cx.tables);
3940
hasher.hash_ty(ty);
4041
hasher.finish()
@@ -44,17 +45,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
4445
if let WherePredicate::BoundPredicate(ref p) = bound {
4546
let h = hash(&p.bounded_ty);
4647
if let Some(ref v) = map.insert(h, p.bounds.iter().collect::<Vec<_>>()) {
47-
let mut hint_string = format!("consider combining the bounds: `{}: ", snippet(cx, p.bounded_ty.span, "_"));
48+
let mut hint_string = format!(
49+
"consider combining the bounds: `{}:",
50+
snippet(cx, p.bounded_ty.span, "_")
51+
);
4852
for b in v.iter() {
4953
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
5054
let path = &poly_trait_ref.trait_ref.path;
51-
hint_string.push_str(&format!("{}, ", path));
55+
hint_string.push_str(&format!(" {} +", path));
5256
}
5357
}
5458
for b in p.bounds.iter() {
5559
if let GenericBound::Trait(ref poly_trait_ref, _) = b {
5660
let path = &poly_trait_ref.trait_ref.path;
57-
hint_string.push_str(&format!("{}, ", path));
61+
hint_string.push_str(&format!(" {} +", path));
5862
}
5963
}
6064
hint_string.truncate(hint_string.len() - 2);

clippy_lints/src/utils/hir_utils.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -639,23 +639,20 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
639639
for ty in ty_list {
640640
self.hash_ty(ty);
641641
}
642-
643642
},
644-
TyKind::Path(qpath) => {
645-
match qpath {
646-
QPath::Resolved(ref maybe_ty, ref path) => {
647-
if let Some(ref ty) = maybe_ty {
648-
self.hash_ty(ty);
649-
}
650-
for segment in &path.segments {
651-
segment.ident.name.hash(&mut self.s);
652-
}
653-
},
654-
QPath::TypeRelative(ref ty, ref segment) => {
643+
TyKind::Path(qpath) => match qpath {
644+
QPath::Resolved(ref maybe_ty, ref path) => {
645+
if let Some(ref ty) = maybe_ty {
655646
self.hash_ty(ty);
647+
}
648+
for segment in &path.segments {
656649
segment.ident.name.hash(&mut self.s);
657-
},
658-
}
650+
}
651+
},
652+
QPath::TypeRelative(ref ty, ref segment) => {
653+
self.hash_ty(ty);
654+
segment.ident.name.hash(&mut self.s);
655+
},
659656
},
660657
TyKind::Def(_, arg_list) => {
661658
for arg in arg_list {
@@ -670,14 +667,11 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
670667
},
671668
TyKind::TraitObject(_, lifetime) => {
672669
self.hash_lifetime(lifetime);
673-
674670
},
675671
TyKind::Typeof(anon_const) => {
676672
self.hash_expr(&self.cx.tcx.hir().body(anon_const.body).value);
677673
},
678-
TyKind::CVarArgs(lifetime) => {
679-
self.hash_lifetime(lifetime)
680-
},
674+
TyKind::CVarArgs(lifetime) => self.hash_lifetime(lifetime),
681675
TyKind::Err | TyKind::Infer | TyKind::Never => {},
682676
}
683677
}

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 308] = [
9+
pub const ALL_LINTS: [Lint; 309] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1848,6 +1848,13 @@ pub const ALL_LINTS: [Lint; 308] = [
18481848
deprecation: None,
18491849
module: "types",
18501850
},
1851+
Lint {
1852+
name: "type_repetition_in_bounds",
1853+
group: "complexity",
1854+
desc: "Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`",
1855+
deprecation: None,
1856+
module: "trait_bounds",
1857+
},
18511858
Lint {
18521859
name: "unicode_not_nfc",
18531860
group: "pedantic",

tests/ui/float_cmp.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
error: this type has already been used as a bound predicate
2+
--> $DIR/float_cmp.rs:12:5
3+
|
4+
LL | T: Copy,
5+
| ^^^^^^^
6+
|
7+
= note: `-D clippy::type-repetition-in-bounds` implied by `-D warnings`
8+
= help: consider combining the bounds: `T: Add<T, Output = T>, Copy`
9+
110
error: strict comparison of f32 or f64
211
--> $DIR/float_cmp.rs:60:5
312
|
@@ -35,5 +44,5 @@ note: std::f32::EPSILON and std::f64::EPSILON are available.
3544
LL | twice(x) != twice(ONE as f64);
3645
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3746

38-
error: aborting due to 3 previous errors
47+
error: aborting due to 4 previous errors
3948

tests/ui/type_repetition_in_bounds.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#[deny(clippy::type_repetition_in_bounds)]
22

3-
pub fn foo<T>(_t: T) where T: Copy, T: Clone {
3+
pub fn foo<T>(_t: T)
4+
where
5+
T: Copy,
6+
T: Clone,
7+
{
48
unimplemented!();
59
}
610

7-
pub fn bar<T, U>(_t: T, _u: U) where T: Copy, U: Clone {
11+
pub fn bar<T, U>(_t: T, _u: U)
12+
where
13+
T: Copy,
14+
U: Clone,
15+
{
816
unimplemented!();
917
}
1018

11-
12-
fn main() {
13-
14-
}
19+
fn main() {}

0 commit comments

Comments
 (0)