1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::utils::{is_copy, match_path, paths, span_note_and_lint};
11 use rustc::hir::{Item, ItemKind};
12 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use rustc::{declare_tool_lint, lint_array};
15 /// **What it does:** Checks for types that implement `Copy` as well as
18 /// **Why is this bad?** Implicit copies can be confusing when working with
19 /// iterator combinators.
21 /// **Known problems:** None.
25 /// #[derive(Copy, Clone)]
26 /// struct Countdown(u8);
28 /// impl Iterator for Countdown {
32 /// let a: Vec<_> = my_iterator.take(1).collect();
33 /// let b: Vec<_> = my_iterator.collect();
35 declare_clippy_lint! {
38 "implementing `Iterator` on a `Copy` type"
41 pub struct CopyIterator;
43 impl LintPass for CopyIterator {
44 fn get_lints(&self) -> LintArray {
45 lint_array![COPY_ITERATOR]
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
50 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
51 if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
52 let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.id));
54 if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
59 "you are implementing `Iterator` on a `Copy` type",
61 "consider implementing `IntoIterator` instead",