1 use crate::utils::{is_copy, match_path, paths, span_note_and_lint};
2 use rustc::hir::{Item, ItemKind};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
6 /// **What it does:** Checks for types that implement `Copy` as well as
9 /// **Why is this bad?** Implicit copies can be confusing when working with
10 /// iterator combinators.
12 /// **Known problems:** None.
16 /// #[derive(Copy, Clone)]
17 /// struct Countdown(u8);
19 /// impl Iterator for Countdown {
23 /// let a: Vec<_> = my_iterator.take(1).collect();
24 /// let b: Vec<_> = my_iterator.collect();
26 declare_clippy_lint! {
29 "implementing `Iterator` on a `Copy` type"
32 pub struct CopyIterator;
34 impl LintPass for CopyIterator {
35 fn get_lints(&self) -> LintArray {
36 lint_array![COPY_ITERATOR]
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
41 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
42 if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
43 let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.id));
45 if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
50 "you are implementing `Iterator` on a `Copy` type",
52 "consider implementing `IntoIterator` instead",