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_lint_pass, declare_tool_lint};
7 /// **What it does:** Checks for types that implement `Copy` as well as
10 /// **Why is this bad?** Implicit copies can be confusing when working with
11 /// iterator combinators.
13 /// **Known problems:** None.
17 /// #[derive(Copy, Clone)]
18 /// struct Countdown(u8);
20 /// impl Iterator for Countdown {
24 /// let a: Vec<_> = my_iterator.take(1).collect();
25 /// let b: Vec<_> = my_iterator.collect();
29 "implementing `Iterator` on a `Copy` type"
32 declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
35 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
36 if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.kind {
37 let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
39 if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
44 "you are implementing `Iterator` on a `Copy` type",
46 "consider implementing `IntoIterator` instead",