-
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 #6577 - nahuakang:inspect_then_for_each, r=flip1995
New Lint: inspect_then_for_each **Work In Progress** This PR addresses [Issue 5209](#5209) and adds a new lint called `inspect_then_for_each`. Current seek some guidance. If you added a new lint, here's a checklist for things that will be checked during review or continuous integration. - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints --- changelog: Add [`inspect_for_each`] lint for the use of `inspect().for_each()` on `Iterators`.
- Loading branch information
Showing
6 changed files
with
98 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
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 @@ | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Span; | ||
|
||
use crate::utils::{match_trait_method, paths, span_lint_and_help}; | ||
|
||
use super::INSPECT_FOR_EACH; | ||
|
||
/// lint use of `inspect().for_each()` for `Iterators` | ||
pub(super) fn lint<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, inspect_span: Span) { | ||
if match_trait_method(cx, expr, &paths::ITERATOR) { | ||
let msg = "called `inspect(..).for_each(..)` on an `Iterator`"; | ||
let hint = "move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)`"; | ||
span_lint_and_help( | ||
cx, | ||
INSPECT_FOR_EACH, | ||
inspect_span.with_hi(expr.span.hi()), | ||
msg, | ||
None, | ||
hint, | ||
); | ||
} | ||
} |
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,22 @@ | ||
#![warn(clippy::inspect_for_each)] | ||
|
||
fn main() { | ||
let a: Vec<usize> = vec![1, 2, 3, 4, 5]; | ||
|
||
let mut b: Vec<usize> = Vec::new(); | ||
a.into_iter().inspect(|x| assert!(*x > 0)).for_each(|x| { | ||
let y = do_some(x); | ||
let z = do_more(y); | ||
b.push(z); | ||
}); | ||
|
||
assert_eq!(b, vec![4, 5, 6, 7, 8]); | ||
} | ||
|
||
fn do_some(a: usize) -> usize { | ||
a + 1 | ||
} | ||
|
||
fn do_more(a: usize) -> usize { | ||
a + 2 | ||
} |
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: called `inspect(..).for_each(..)` on an `Iterator` | ||
--> $DIR/inspect_for_each.rs:7:19 | ||
| | ||
LL | a.into_iter().inspect(|x| assert!(*x > 0)).for_each(|x| { | ||
| ___________________^ | ||
LL | | let y = do_some(x); | ||
LL | | let z = do_more(y); | ||
LL | | b.push(z); | ||
LL | | }); | ||
| |______^ | ||
| | ||
= note: `-D clippy::inspect-for-each` implied by `-D warnings` | ||
= help: move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)` | ||
|
||
error: aborting due to previous error | ||
|