forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
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 rust-lang#6685 - magurotuna:filter_map_identity, r=phansch
Add new lint `filter_map_identity` <!-- Thank you for making Clippy better! We're collecting our changelog from pull request descriptions. If your PR only includes internal changes, you can just write `changelog: none`. Otherwise, please write a short comment explaining your change. If your PR fixes an issue, you can add "fixes #issue_number" into this PR description. This way the issue will be automatically closed when your PR is merged. 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 Note that you can skip the above if you are just opening a WIP PR in order to get feedback. Delete this line and everything above before opening your PR. --> This commit adds a new lint named filter_map_identity. This lint is the same as `flat_map_identity` except that it checks for the usage of `filter_map`. --- Closes rust-lang#6643 changelog: Added a new lint: `filter_map_identity`
- Loading branch information
Showing
8 changed files
with
144 additions
and
2 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
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,56 @@ | ||
use crate::utils::{match_qpath, match_trait_method, paths, span_lint_and_sugg}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Span; | ||
|
||
use super::FILTER_MAP_IDENTITY; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
expr: &hir::Expr<'_>, | ||
filter_map_args: &[hir::Expr<'_>], | ||
filter_map_span: Span, | ||
) { | ||
if match_trait_method(cx, expr, &paths::ITERATOR) { | ||
let arg_node = &filter_map_args[1].kind; | ||
|
||
let apply_lint = |message: &str| { | ||
span_lint_and_sugg( | ||
cx, | ||
FILTER_MAP_IDENTITY, | ||
filter_map_span.with_hi(expr.span.hi()), | ||
message, | ||
"try", | ||
"flatten()".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
}; | ||
|
||
if_chain! { | ||
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node; | ||
let body = cx.tcx.hir().body(*body_id); | ||
|
||
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind; | ||
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind; | ||
|
||
if path.segments.len() == 1; | ||
if path.segments[0].ident.name == binding_ident.name; | ||
|
||
then { | ||
apply_lint("called `filter_map(|x| x)` on an `Iterator`"); | ||
} | ||
} | ||
|
||
if_chain! { | ||
if let hir::ExprKind::Path(ref qpath) = arg_node; | ||
|
||
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY); | ||
|
||
then { | ||
apply_lint("called `filter_map(std::convert::identity)` on an `Iterator`"); | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![warn(clippy::filter_map_identity)] | ||
|
||
fn main() { | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.flatten(); | ||
|
||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.flatten(); | ||
|
||
use std::convert::identity; | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.flatten(); | ||
} |
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 @@ | ||
// run-rustfix | ||
|
||
#![allow(unused_imports)] | ||
#![warn(clippy::filter_map_identity)] | ||
|
||
fn main() { | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(|x| x); | ||
|
||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(std::convert::identity); | ||
|
||
use std::convert::identity; | ||
let iterator = vec![Some(1), None, Some(2)].into_iter(); | ||
let _ = iterator.filter_map(identity); | ||
} |
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 @@ | ||
error: called `filter_map(|x| x)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:8:22 | ||
| | ||
LL | let _ = iterator.filter_map(|x| x); | ||
| ^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
| | ||
= note: `-D clippy::filter-map-identity` implied by `-D warnings` | ||
|
||
error: called `filter_map(std::convert::identity)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:11:22 | ||
| | ||
LL | let _ = iterator.filter_map(std::convert::identity); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
|
||
error: called `filter_map(std::convert::identity)` on an `Iterator` | ||
--> $DIR/filter_map_identity.rs:15:22 | ||
| | ||
LL | let _ = iterator.filter_map(identity); | ||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `flatten()` | ||
|
||
error: aborting due to 3 previous errors | ||
|