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.
- Loading branch information
1 parent
f2f73f9
commit 91a1d16
Showing
7 changed files
with
168 additions
and
19 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,43 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::SpanRangeExt; | ||
use clippy_utils::{is_expr_identity_function, is_trait_method}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{Span, sym}; | ||
|
||
use super::MAP_ALL_ANY_IDENTITY; | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
recv: &Expr<'_>, | ||
map_call_span: Span, | ||
map_arg: &Expr<'_>, | ||
any_call_span: Span, | ||
any_arg: &Expr<'_>, | ||
method: &str, | ||
) { | ||
if is_trait_method(cx, expr, sym::Iterator) | ||
&& is_trait_method(cx, recv, sym::Iterator) | ||
&& is_expr_identity_function(cx, any_arg) | ||
&& let map_any_call_span = map_call_span.with_hi(any_call_span.hi()) | ||
&& let Some(map_arg) = map_arg.span.get_source_text(cx) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
MAP_ALL_ANY_IDENTITY, | ||
map_any_call_span, | ||
format!("usage of `.map(...).{method}(identity)`"), | ||
|diag| { | ||
diag.span_suggestion_verbose( | ||
map_any_call_span, | ||
format!("use `.{method}(...)` instead"), | ||
format!("{method}({map_arg})"), | ||
Applicability::MachineApplicable, | ||
); | ||
}, | ||
); | ||
} | ||
} |
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,21 @@ | ||
#![warn(clippy::map_all_any_identity)] | ||
|
||
fn main() { | ||
let _ = ["foo"].into_iter().any(|s| s == "foo"); | ||
//~^ map_all_any_identity | ||
let _ = ["foo"].into_iter().all(|s| s == "foo"); | ||
//~^ map_all_any_identity | ||
|
||
// | ||
// Do not lint | ||
// | ||
// Not identity | ||
let _ = ["foo"].into_iter().map(|s| s.len()).any(|n| n > 0); | ||
// Macro | ||
macro_rules! map { | ||
($x:expr) => { | ||
$x.into_iter().map(|s| s == "foo") | ||
}; | ||
} | ||
map!(["foo"]).any(|a| a); | ||
} |
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,21 @@ | ||
#![warn(clippy::map_all_any_identity)] | ||
|
||
fn main() { | ||
let _ = ["foo"].into_iter().map(|s| s == "foo").any(|a| a); | ||
//~^ map_all_any_identity | ||
let _ = ["foo"].into_iter().map(|s| s == "foo").all(std::convert::identity); | ||
//~^ map_all_any_identity | ||
|
||
// | ||
// Do not lint | ||
// | ||
// Not identity | ||
let _ = ["foo"].into_iter().map(|s| s.len()).any(|n| n > 0); | ||
// Macro | ||
macro_rules! map { | ||
($x:expr) => { | ||
$x.into_iter().map(|s| s == "foo") | ||
}; | ||
} | ||
map!(["foo"]).any(|a| a); | ||
} |
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,26 @@ | ||
error: usage of `.map(...).any(identity)` | ||
--> tests/ui/map_all_any_identity.rs:4:33 | ||
| | ||
LL | let _ = ["foo"].into_iter().map(|s| s == "foo").any(|a| a); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::map-all-any-identity` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::map_all_any_identity)]` | ||
help: use `.any(...)` instead | ||
| | ||
LL | let _ = ["foo"].into_iter().any(|s| s == "foo"); | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: usage of `.map(...).all(identity)` | ||
--> tests/ui/map_all_any_identity.rs:6:33 | ||
| | ||
LL | let _ = ["foo"].into_iter().map(|s| s == "foo").all(std::convert::identity); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: use `.all(...)` instead | ||
| | ||
LL | let _ = ["foo"].into_iter().all(|s| s == "foo"); | ||
| ~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|