Skip to content

Commit f4f31a4

Browse files
committed
Implement lint 'suspicious_map'
1 parent f95c87e commit f4f31a4

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
657657
methods::OPTION_MAP_UNWRAP_OR,
658658
methods::OPTION_MAP_UNWRAP_OR_ELSE,
659659
methods::RESULT_MAP_UNWRAP_OR_ELSE,
660+
methods::SUSPICIOUS_MAP,
660661
misc::USED_UNDERSCORE_BINDING,
661662
misc_early::UNSEPARATED_LITERAL_SUFFIX,
662663
mut_mut::MUT_MUT,

clippy_lints/src/methods/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,23 @@ 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+
///
897+
/// **Known problems:** None
898+
///
899+
/// **Example:**
900+
///
901+
/// ```rust
902+
/// let _ = (0..3).map(|x| x + 2).count();
903+
/// ```
904+
pub SUSPICIOUS_MAP,
905+
pedantic,
906+
"suspicious usage of map"
907+
}
908+
892909
declare_lint_pass!(Methods => [
893910
OPTION_UNWRAP_USED,
894911
RESULT_UNWRAP_USED,
@@ -927,6 +944,7 @@ declare_lint_pass!(Methods => [
927944
UNNECESSARY_FILTER_MAP,
928945
INTO_ITER_ON_ARRAY,
929946
INTO_ITER_ON_REF,
947+
SUSPICIOUS_MAP,
930948
]);
931949

932950
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
@@ -972,6 +990,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
972990
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
973991
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
974992
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
993+
["count", "map"] => lint_suspicious_map(cx, expr),
975994
_ => {},
976995
}
977996

@@ -2519,6 +2538,15 @@ fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: Ty<'_
25192538
}
25202539
}
25212540

2541+
fn lint_suspicious_map(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
2542+
span_lint(
2543+
cx,
2544+
SUSPICIOUS_MAP,
2545+
expr.span,
2546+
"Make sure you did not confuse `map` with `filter`.",
2547+
);
2548+
}
2549+
25222550
/// Given a `Result<T, E>` type, return its error type (`E`).
25232551
fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
25242552
if let ty::Adt(_, substs) = ty.sty {

src/lintlist/mod.rs

Lines changed: 8 additions & 1 deletion
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: "pedantic",
1742+
desc: "suspicious usage of map",
1743+
deprecation: None,
1744+
module: "methods",
1745+
},
17391746
Lint {
17401747
name: "suspicious_op_assign_impl",
17411748
group: "correctness",

0 commit comments

Comments
 (0)