Skip to content

Commit b233685

Browse files
committed
Auto merge of #4394 - jeremystucki:suspicious_map, r=flip1995
Implement "suspicious_map" lint Resolves #4010 changelog: New lint `suspicious_map`.
2 parents e92c489 + 9c39c02 commit b233685

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ Released 2018-09-13
11521152
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
11531153
[`suspicious_assignment_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_assignment_formatting
11541154
[`suspicious_else_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_else_formatting
1155+
[`suspicious_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_map
11551156
[`suspicious_op_assign_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_op_assign_impl
11561157
[`temporary_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_assignment
11571158
[`temporary_cstring_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_cstring_as_ptr

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 309 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 310 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
800800
methods::SHOULD_IMPLEMENT_TRAIT,
801801
methods::SINGLE_CHAR_PATTERN,
802802
methods::STRING_EXTEND_CHARS,
803+
methods::SUSPICIOUS_MAP,
803804
methods::TEMPORARY_CSTRING_AS_PTR,
804805
methods::UNNECESSARY_FILTER_MAP,
805806
methods::UNNECESSARY_FOLD,
@@ -1033,6 +1034,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
10331034
methods::FILTER_NEXT,
10341035
methods::FLAT_MAP_IDENTITY,
10351036
methods::SEARCH_IS_SOME,
1037+
methods::SUSPICIOUS_MAP,
10361038
methods::UNNECESSARY_FILTER_MAP,
10371039
methods::USELESS_ASREF,
10381040
misc::SHORT_CIRCUIT_STATEMENT,

clippy_lints/src/methods/mod.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use syntax::ast;
1818
use syntax::source_map::Span;
1919
use syntax::symbol::LocalInternedString;
2020

21-
use crate::utils::paths;
2221
use crate::utils::sugg;
2322
use crate::utils::usage::mutated_variables;
2423
use crate::utils::{
@@ -28,6 +27,7 @@ use crate::utils::{
2827
snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg,
2928
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
3029
};
30+
use crate::utils::{paths, span_help_and_lint};
3131

3232
declare_clippy_lint! {
3333
/// **What it does:** Checks for `.unwrap()` calls on `Option`s.
@@ -889,6 +889,24 @@ declare_clippy_lint! {
889889
"using `.into_iter()` on a reference"
890890
}
891891

892+
declare_clippy_lint! {
893+
/// **What it does:** Checks for calls to `map` followed by a `count`.
894+
///
895+
/// **Why is this bad?** It looks suspicious. Maybe `map` was confused with `filter`.
896+
/// If the `map` call is intentional, this should be rewritten.
897+
///
898+
/// **Known problems:** None
899+
///
900+
/// **Example:**
901+
///
902+
/// ```rust
903+
/// let _ = (0..3).map(|x| x + 2).count();
904+
/// ```
905+
pub SUSPICIOUS_MAP,
906+
complexity,
907+
"suspicious usage of map"
908+
}
909+
892910
declare_lint_pass!(Methods => [
893911
OPTION_UNWRAP_USED,
894912
RESULT_UNWRAP_USED,
@@ -927,6 +945,7 @@ declare_lint_pass!(Methods => [
927945
UNNECESSARY_FILTER_MAP,
928946
INTO_ITER_ON_ARRAY,
929947
INTO_ITER_ON_REF,
948+
SUSPICIOUS_MAP,
930949
]);
931950

932951
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
@@ -972,6 +991,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
972991
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
973992
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
974993
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
994+
["count", "map"] => lint_suspicious_map(cx, expr),
975995
_ => {},
976996
}
977997

@@ -2519,6 +2539,16 @@ fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: Ty<'_
25192539
}
25202540
}
25212541

2542+
fn lint_suspicious_map(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
2543+
span_help_and_lint(
2544+
cx,
2545+
SUSPICIOUS_MAP,
2546+
expr.span,
2547+
"this call to `map()` won't have an effect on the call to `count()`",
2548+
"make sure you did not confuse `map` with `filter`",
2549+
);
2550+
}
2551+
25222552
/// Given a `Result<T, E>` type, return its error type (`E`).
25232553
fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
25242554
if let ty::Adt(_, substs) = ty.sty {

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 309] = [
9+
pub const ALL_LINTS: [Lint; 310] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -1736,6 +1736,13 @@ pub const ALL_LINTS: [Lint; 309] = [
17361736
deprecation: None,
17371737
module: "formatting",
17381738
},
1739+
Lint {
1740+
name: "suspicious_map",
1741+
group: "complexity",
1742+
desc: "suspicious usage of map",
1743+
deprecation: None,
1744+
module: "methods",
1745+
},
17391746
Lint {
17401747
name: "suspicious_op_assign_impl",
17411748
group: "correctness",

tests/ui/suspicious_map.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![warn(clippy::suspicious_map)]
2+
3+
fn main() {
4+
let _ = (0..3).map(|x| x + 2).count();
5+
}

tests/ui/suspicious_map.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: this call to `map()` won't have an effect on the call to `count()`
2+
--> $DIR/suspicious_map.rs:4:13
3+
|
4+
LL | let _ = (0..3).map(|x| x + 2).count();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::suspicious-map` implied by `-D warnings`
8+
= help: make sure you did not confuse `map` with `filter`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)