]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/copy_iterator.rs
Auto merge of #4934 - illicitonion:exhaustive_match, r=flip1995
[rust.git] / clippy_lints / src / copy_iterator.rs
1 use crate::utils::{is_copy, match_path, paths, span_note_and_lint};
2 use rustc::declare_lint_pass;
3 use rustc::hir::{Item, ItemKind};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_session::declare_tool_lint;
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for types that implement `Copy` as well as
9     /// `Iterator`.
10     ///
11     /// **Why is this bad?** Implicit copies can be confusing when working with
12     /// iterator combinators.
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     /// ```rust,ignore
18     /// #[derive(Copy, Clone)]
19     /// struct Countdown(u8);
20     ///
21     /// impl Iterator for Countdown {
22     ///     // ...
23     /// }
24     ///
25     /// let a: Vec<_> = my_iterator.take(1).collect();
26     /// let b: Vec<_> = my_iterator.collect();
27     /// ```
28     pub COPY_ITERATOR,
29     pedantic,
30     "implementing `Iterator` on a `Copy` type"
31 }
32
33 declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
36     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
37         if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.kind {
38             let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
39
40             if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
41                 span_note_and_lint(
42                     cx,
43                     COPY_ITERATOR,
44                     item.span,
45                     "you are implementing `Iterator` on a `Copy` type",
46                     item.span,
47                     "consider implementing `IntoIterator` instead",
48                 );
49             }
50         }
51     }
52 }