@@ -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+
892909declare_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
932950impl < ' 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`).
25232551fn get_error_type < ' a > ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' a > ) -> Option < Ty < ' a > > {
25242552 if let ty:: Adt ( _, substs) = ty. sty {
0 commit comments