]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/trait_bounds.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / trait_bounds.rs
index 1f4c68507604595ea688ebaffe6b4fb8386904b6..858b505712d31490c34d99ce42ac163d4afb2a2e 100644 (file)
@@ -1,55 +1,69 @@
-use crate::utils::{in_macro, span_help_and_lint, SpanlessHash};
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use crate::utils::{in_macro, snippet, span_lint_and_help, SpanlessHash};
 use rustc_data_structures::fx::FxHashMap;
-use rustc::hir::*;
-
-declare_clippy_lint! {
-    pub TYPE_REPETITION_IN_BOUNDS,
-    complexity,
-    "Types are repeated unnecessary in trait bounds use `+` instead of using `T: _, T: _`"
-}
+use rustc_hir::*;
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_tool_lint, impl_lint_pass};
 
 #[derive(Copy, Clone)]
 pub struct TraitBounds;
 
-impl LintPass for TraitBounds {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(TYPE_REPETITION_IN_BOUNDS)
-    }
-
-    fn name(&self) -> &'static str {
-        "TypeRepetitionInBounds"
-    }
+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,
+    pedantic,
+    "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) {
+    fn check_generics(&mut self, cx: &LateContext<'a, 'tcx>, gen: &'tcx Generics<'_>) {
         if in_macro(gen.span) {
             return;
         }
-        let hash = | ty | -> u64 {
+        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 {
+        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) {
-                    let mut hint_string = format!("consider combining the bounds: `{:?}: ", p.bounded_ty);
-                    for &b in v.iter() {
-                        hint_string.push_str(&format!("{:?}, ", b));
+                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() {
-                        hint_string.push_str(&format!("{:?}, ", b));
+                    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(
+                    span_lint_and_help(
                         cx,
                         TYPE_REPETITION_IN_BOUNDS,
                         p.span,