]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/copy_iterator.rs
Rollup merge of #88860 - nbdd0121:panic, r=m-ou-se
[rust.git] / clippy_lints / src / copy_iterator.rs
index f45d8eea5e1d5c47264952fc8fdc073f747b4f23..c2e9e8b3ab7f39d226f32df4449cfef0190cacbe 100644 (file)
@@ -1,63 +1,58 @@
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
+use clippy_utils::diagnostics::span_lint_and_note;
+use clippy_utils::ty::is_copy;
+use rustc_hir::{Impl, Item, ItemKind};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::sym;
 
-use crate::utils::{is_copy, match_path, paths, span_note_and_lint};
-use rustc::hir::{Item, ItemKind};
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use if_chain::if_chain;
 
-/// **What it does:** Checks for types that implement `Copy` as well as
-/// `Iterator`.
-///
-/// **Why is this bad?** Implicit copies can be confusing when working with
-/// iterator combinators.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// #[derive(Copy, Clone)]
-/// struct Countdown(u8);
-///
-/// impl Iterator for Countdown {
-///     // ...
-/// }
-///
-/// let a: Vec<_> = my_iterator.take(1).collect();
-/// let b: Vec<_> = my_iterator.collect();
-/// ```
 declare_clippy_lint! {
+    /// ### What it does
+    /// Checks for types that implement `Copy` as well as
+    /// `Iterator`.
+    ///
+    /// ### Why is this bad?
+    /// Implicit copies can be confusing when working with
+    /// iterator combinators.
+    ///
+    /// ### Example
+    /// ```rust,ignore
+    /// #[derive(Copy, Clone)]
+    /// struct Countdown(u8);
+    ///
+    /// impl Iterator for Countdown {
+    ///     // ...
+    /// }
+    ///
+    /// let a: Vec<_> = my_iterator.take(1).collect();
+    /// let b: Vec<_> = my_iterator.collect();
+    /// ```
     pub COPY_ITERATOR,
     pedantic,
     "implementing `Iterator` on a `Copy` type"
 }
 
-pub struct CopyIterator;
+declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
 
-impl LintPass for CopyIterator {
-    fn get_lints(&self) -> LintArray {
-        lint_array![COPY_ITERATOR]
-    }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
-    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
-        if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
-            let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.id));
-
-            if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
-                span_note_and_lint(
+impl<'tcx> LateLintPass<'tcx> for CopyIterator {
+    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
+        if_chain! {
+            if let ItemKind::Impl(Impl {
+                of_trait: Some(ref trait_ref),
+                ..
+            }) = item.kind;
+            let ty = cx.tcx.type_of(item.def_id);
+            if is_copy(cx, ty);
+            if let Some(trait_id) = trait_ref.trait_def_id();
+            if cx.tcx.is_diagnostic_item(sym::Iterator, trait_id);
+            then {
+                span_lint_and_note(
                     cx,
                     COPY_ITERATOR,
                     item.span,
                     "you are implementing `Iterator` on a `Copy` type",
-                    item.span,
+                    None,
                     "consider implementing `IntoIterator` instead",
                 );
             }