2018-11-27 14:14:15 -06:00
|
|
|
use crate::utils::{is_copy, match_path, paths, span_note_and_lint};
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc::declare_lint_pass;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::{Item, ItemKind};
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc_session::declare_tool_lint;
|
2018-07-17 12:22:55 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for types that implement `Copy` as well as
|
|
|
|
/// `Iterator`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Implicit copies can be confusing when working with
|
|
|
|
/// iterator combinators.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-08-03 11:42:05 -05:00
|
|
|
/// ```rust,ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// #[derive(Copy, Clone)]
|
|
|
|
/// struct Countdown(u8);
|
|
|
|
///
|
|
|
|
/// impl Iterator for Countdown {
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let a: Vec<_> = my_iterator.take(1).collect();
|
|
|
|
/// let b: Vec<_> = my_iterator.collect();
|
|
|
|
/// ```
|
2018-07-17 12:22:55 -05:00
|
|
|
pub COPY_ITERATOR,
|
|
|
|
pedantic,
|
|
|
|
"implementing `Iterator` on a `Copy` type"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
|
2018-07-17 12:22:55 -05:00
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator {
|
2019-12-22 08:42:41 -06:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.kind {
|
2019-07-05 22:52:51 -05:00
|
|
|
let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
|
2018-07-17 12:22:55 -05:00
|
|
|
|
2019-05-17 16:53:54 -05:00
|
|
|
if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
|
2018-07-17 12:22:55 -05:00
|
|
|
span_note_and_lint(
|
|
|
|
cx,
|
|
|
|
COPY_ITERATOR,
|
|
|
|
item.span,
|
|
|
|
"you are implementing `Iterator` on a `Copy` type",
|
|
|
|
item.span,
|
|
|
|
"consider implementing `IntoIterator` instead",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|