-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2924 from niklasf/copy-iterator
Add copy_iterator lint (#1534)
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::utils::{is_copy, match_path, paths, span_note_and_lint}; | ||
use rustc::hir::{Item, ItemKind}; | ||
use rustc::lint::*; | ||
use rustc::{declare_lint, lint_array}; | ||
|
||
/// **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:** | ||
/// ```rust | ||
/// #[derive(Copy, Clone)] | ||
/// struct Countdown(u8); | ||
/// | ||
/// impl Iterator for Countdown { | ||
/// // ... | ||
/// } | ||
/// | ||
/// let a: Vec<_> = my_iterator.take(1).collect(); | ||
/// let b: Vec<_> = my_iterator.collect(); | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub COPY_ITERATOR, | ||
pedantic, | ||
"implementing `Iterator` on a `Copy` type" | ||
} | ||
|
||
pub struct CopyIterator; | ||
|
||
impl LintPass for CopyIterator { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array![COPY_ITERATOR] | ||
} | ||
} | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator { | ||
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { | ||
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node { | ||
let ty = cx.tcx.type_of(cx.tcx.hir.local_def_id(item.id)); | ||
|
||
if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) { | ||
span_note_and_lint( | ||
cx, | ||
COPY_ITERATOR, | ||
item.span, | ||
"you are implementing `Iterator` on a `Copy` type", | ||
item.span, | ||
"consider implementing `IntoIterator` instead", | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![warn(copy_iterator)] | ||
|
||
#[derive(Copy, Clone)] | ||
struct Countdown(u8); | ||
|
||
impl Iterator for Countdown { | ||
type Item = u8; | ||
|
||
fn next(&mut self) -> Option<u8> { | ||
self.0.checked_sub(1).map(|c| { | ||
self.0 = c; | ||
c | ||
}) | ||
} | ||
} | ||
|
||
fn main() { | ||
let my_iterator = Countdown(5); | ||
let a: Vec<_> = my_iterator.take(1).collect(); | ||
assert_eq!(a.len(), 1); | ||
let b: Vec<_> = my_iterator.collect(); | ||
assert_eq!(b.len(), 5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error: you are implementing `Iterator` on a `Copy` type | ||
--> $DIR/copy_iterator.rs:6:1 | ||
| | ||
6 | / impl Iterator for Countdown { | ||
7 | | type Item = u8; | ||
8 | | | ||
9 | | fn next(&mut self) -> Option<u8> { | ||
... | | ||
14 | | } | ||
15 | | } | ||
| |_^ | ||
| | ||
= note: `-D copy-iterator` implied by `-D warnings` | ||
= note: consider implementing `IntoIterator` instead | ||
|
||
error: aborting due to previous error | ||
|