Skip to content

Commit 1a52478

Browse files
committed
Auto merge of #7610 - Labelray:master, r=camsteffen
Add new lint `iter_not_returning_iterator` Add new lint [`iter_not_returning_iterator`] to detect method `iter()` or `iter_mut()` returning a type not implementing `Iterator` changelog: Add new lint [`iter_not_returning_iterator`]
2 parents 36e6469 + 8f88acd commit 1a52478

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,7 @@ Released 2018-09-13
27222722
[`iter_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_count
27232723
[`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
27242724
[`iter_next_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_slice
2725+
[`iter_not_returning_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_not_returning_iterator
27252726
[`iter_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth
27262727
[`iter_nth_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth_zero
27272728
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use clippy_utils::{diagnostics::span_lint, return_ty, ty::implements_trait};
2+
use rustc_hir::{ImplItem, ImplItemKind};
3+
use rustc_lint::{LateContext, LateLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_span::symbol::kw;
6+
use rustc_span::symbol::sym;
7+
8+
declare_clippy_lint! {
9+
/// ### What it does
10+
/// Detects methods named `iter` or `iter_mut` that do not have a return type that implements `Iterator`.
11+
///
12+
/// ### Why is this bad?
13+
/// Methods named `iter` or `iter_mut` conventionally return an `Iterator`.
14+
///
15+
/// ### Example
16+
/// ```rust
17+
/// // `String` does not implement `Iterator`
18+
/// struct Data {}
19+
/// impl Data {
20+
/// fn iter(&self) -> String {
21+
/// todo!()
22+
/// }
23+
/// }
24+
/// ```
25+
/// Use instead:
26+
/// ```rust
27+
/// use std::str::Chars;
28+
/// struct Data {}
29+
/// impl Data {
30+
/// fn iter(&self) -> Chars<'static> {
31+
/// todo!()
32+
/// }
33+
/// }
34+
/// ```
35+
pub ITER_NOT_RETURNING_ITERATOR,
36+
pedantic,
37+
"methods named `iter` or `iter_mut` that do not return an `Iterator`"
38+
}
39+
40+
declare_lint_pass!(IterNotReturningIterator => [ITER_NOT_RETURNING_ITERATOR]);
41+
42+
impl LateLintPass<'_> for IterNotReturningIterator {
43+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'tcx>) {
44+
let name: &str = &impl_item.ident.name.as_str();
45+
if_chain! {
46+
if let ImplItemKind::Fn(fn_sig, _) = &impl_item.kind;
47+
let ret_ty = return_ty(cx, impl_item.hir_id());
48+
if matches!(name, "iter" | "iter_mut");
49+
if let [param] = cx.tcx.fn_arg_names(impl_item.def_id);
50+
if param.name == kw::SelfLower;
51+
if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
52+
if !implements_trait(cx, ret_ty, iter_trait_id, &[]);
53+
54+
then {
55+
span_lint(
56+
cx,
57+
ITER_NOT_RETURNING_ITERATOR,
58+
fn_sig.span,
59+
&format!("this method is named `{}` but its return type does not implement `Iterator`", name),
60+
);
61+
}
62+
}
63+
}
64+
}

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ mod int_plus_one;
241241
mod integer_division;
242242
mod invalid_upcast_comparisons;
243243
mod items_after_statements;
244+
mod iter_not_returning_iterator;
244245
mod large_const_arrays;
245246
mod large_enum_variant;
246247
mod large_stack_arrays;
@@ -674,6 +675,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
674675
integer_division::INTEGER_DIVISION,
675676
invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS,
676677
items_after_statements::ITEMS_AFTER_STATEMENTS,
678+
iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR,
677679
large_const_arrays::LARGE_CONST_ARRAYS,
678680
large_enum_variant::LARGE_ENUM_VARIANT,
679681
large_stack_arrays::LARGE_STACK_ARRAYS,
@@ -1104,6 +1106,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11041106
LintId::of(infinite_iter::MAYBE_INFINITE_ITER),
11051107
LintId::of(invalid_upcast_comparisons::INVALID_UPCAST_COMPARISONS),
11061108
LintId::of(items_after_statements::ITEMS_AFTER_STATEMENTS),
1109+
LintId::of(iter_not_returning_iterator::ITER_NOT_RETURNING_ITERATOR),
11071110
LintId::of(large_stack_arrays::LARGE_STACK_ARRAYS),
11081111
LintId::of(let_underscore::LET_UNDERSCORE_DROP),
11091112
LintId::of(literal_representation::LARGE_DIGIT_GROUPS),
@@ -2131,6 +2134,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
21312134
store.register_late_pass(|| Box::new(strlen_on_c_strings::StrlenOnCStrings));
21322135
store.register_late_pass(move || Box::new(self_named_constructors::SelfNamedConstructors));
21332136
store.register_late_pass(move || Box::new(feature_name::FeatureName));
2137+
store.register_late_pass(move || Box::new(iter_not_returning_iterator::IterNotReturningIterator));
21342138
}
21352139

21362140
#[rustfmt::skip]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![warn(clippy::iter_not_returning_iterator)]
2+
3+
struct Data {
4+
begin: u32,
5+
}
6+
7+
struct Counter {
8+
count: u32,
9+
}
10+
11+
impl Data {
12+
fn iter(&self) -> Counter {
13+
todo!()
14+
}
15+
16+
fn iter_mut(&self) -> Counter {
17+
todo!()
18+
}
19+
}
20+
21+
struct Data2 {
22+
begin: u32,
23+
}
24+
25+
struct Counter2 {
26+
count: u32,
27+
}
28+
29+
impl Data2 {
30+
fn iter(&self) -> Counter2 {
31+
todo!()
32+
}
33+
34+
fn iter_mut(&self) -> Counter2 {
35+
todo!()
36+
}
37+
}
38+
39+
impl Iterator for Counter {
40+
type Item = u32;
41+
42+
fn next(&mut self) -> Option<Self::Item> {
43+
todo!()
44+
}
45+
}
46+
47+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this method is named `iter` but its return type does not implement `Iterator`
2+
--> $DIR/iter_not_returning_iterator.rs:30:5
3+
|
4+
LL | fn iter(&self) -> Counter2 {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::iter-not-returning-iterator` implied by `-D warnings`
8+
9+
error: this method is named `iter_mut` but its return type does not implement `Iterator`
10+
--> $DIR/iter_not_returning_iterator.rs:34:5
11+
|
12+
LL | fn iter_mut(&self) -> Counter2 {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)