-
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.
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`]
- Loading branch information
Showing
5 changed files
with
132 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
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,64 @@ | ||
use clippy_utils::{diagnostics::span_lint, return_ty, ty::implements_trait}; | ||
use rustc_hir::{ImplItem, ImplItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::symbol::kw; | ||
use rustc_span::symbol::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects methods named `iter` or `iter_mut` that do not have a return type that implements `Iterator`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Methods named `iter` or `iter_mut` conventionally return an `Iterator`. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// // `String` does not implement `Iterator` | ||
/// struct Data {} | ||
/// impl Data { | ||
/// fn iter(&self) -> String { | ||
/// todo!() | ||
/// } | ||
/// } | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// use std::str::Chars; | ||
/// struct Data {} | ||
/// impl Data { | ||
/// fn iter(&self) -> Chars<'static> { | ||
/// todo!() | ||
/// } | ||
/// } | ||
/// ``` | ||
pub ITER_NOT_RETURNING_ITERATOR, | ||
pedantic, | ||
"methods named `iter` or `iter_mut` that do not return an `Iterator`" | ||
} | ||
|
||
declare_lint_pass!(IterNotReturningIterator => [ITER_NOT_RETURNING_ITERATOR]); | ||
|
||
impl LateLintPass<'_> for IterNotReturningIterator { | ||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'tcx>) { | ||
let name: &str = &impl_item.ident.name.as_str(); | ||
if_chain! { | ||
if let ImplItemKind::Fn(fn_sig, _) = &impl_item.kind; | ||
let ret_ty = return_ty(cx, impl_item.hir_id()); | ||
if matches!(name, "iter" | "iter_mut"); | ||
if let [param] = cx.tcx.fn_arg_names(impl_item.def_id); | ||
if param.name == kw::SelfLower; | ||
if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator); | ||
if !implements_trait(cx, ret_ty, iter_trait_id, &[]); | ||
|
||
then { | ||
span_lint( | ||
cx, | ||
ITER_NOT_RETURNING_ITERATOR, | ||
fn_sig.span, | ||
&format!("this method is named `{}` but its return type does not implement `Iterator`", name), | ||
); | ||
} | ||
} | ||
} | ||
} |
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,47 @@ | ||
#![warn(clippy::iter_not_returning_iterator)] | ||
|
||
struct Data { | ||
begin: u32, | ||
} | ||
|
||
struct Counter { | ||
count: u32, | ||
} | ||
|
||
impl Data { | ||
fn iter(&self) -> Counter { | ||
todo!() | ||
} | ||
|
||
fn iter_mut(&self) -> Counter { | ||
todo!() | ||
} | ||
} | ||
|
||
struct Data2 { | ||
begin: u32, | ||
} | ||
|
||
struct Counter2 { | ||
count: u32, | ||
} | ||
|
||
impl Data2 { | ||
fn iter(&self) -> Counter2 { | ||
todo!() | ||
} | ||
|
||
fn iter_mut(&self) -> Counter2 { | ||
todo!() | ||
} | ||
} | ||
|
||
impl Iterator for Counter { | ||
type Item = u32; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn main() {} |
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,16 @@ | ||
error: this method is named `iter` but its return type does not implement `Iterator` | ||
--> $DIR/iter_not_returning_iterator.rs:30:5 | ||
| | ||
LL | fn iter(&self) -> Counter2 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::iter-not-returning-iterator` implied by `-D warnings` | ||
|
||
error: this method is named `iter_mut` but its return type does not implement `Iterator` | ||
--> $DIR/iter_not_returning_iterator.rs:34:5 | ||
| | ||
LL | fn iter_mut(&self) -> Counter2 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|