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;
8 /// **What it does:** Checks for types that implement `Copy` as well as
11 /// **Why is this bad?** Implicit copies can be confusing when working with
12 /// iterator combinators.
14 /// **Known problems:** None.
18 /// #[derive(Copy, Clone)]
19 /// struct Countdown(u8);
21 /// impl Iterator for Countdown {
25 /// let a: Vec<_> = my_iterator.take(1).collect();
26 /// let b: Vec<_> = my_iterator.collect();
30 "implementing `Iterator` on a `Copy` type"
33 declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
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));
40 if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
45 "you are implementing `Iterator` on a `Copy` type",
47 "consider implementing `IntoIterator` instead",