1 use crate::utils::{is_copy, match_path, paths, span_lint_and_note};
2 use rustc_hir::{Item, ItemKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{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 {
37 of_trait: Some(ref trait_ref),
41 let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
43 if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
48 "you are implementing `Iterator` on a `Copy` type",
50 "consider implementing `IntoIterator` instead",