Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "suspicious_map" lint #4394

Merged
merged 4 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ Released 2018-09-13
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
[`suspicious_assignment_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_assignment_formatting
[`suspicious_else_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_else_formatting
[`suspicious_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_map
[`suspicious_op_assign_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_op_assign_impl
[`temporary_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_assignment
[`temporary_cstring_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_cstring_as_ptr
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

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

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

Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
methods::OPTION_MAP_UNWRAP_OR,
methods::OPTION_MAP_UNWRAP_OR_ELSE,
methods::RESULT_MAP_UNWRAP_OR_ELSE,
methods::SUSPICIOUS_MAP,
misc::USED_UNDERSCORE_BINDING,
misc_early::UNSEPARATED_LITERAL_SUFFIX,
mut_mut::MUT_MUT,
Expand Down
28 changes: 28 additions & 0 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,23 @@ declare_clippy_lint! {
"using `.into_iter()` on a reference"
}

declare_clippy_lint! {
/// **What it does:** Checks for calls to `map` followed by a `count`.
///
/// **Why is this bad?** It looks suspicious. Maybe `map` was confused with `filter`.
///
/// **Known problems:** None
///
/// **Example:**
///
/// ```rust
/// let _ = (0..3).map(|x| x + 2).count();
/// ```
pub SUSPICIOUS_MAP,
pedantic,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could even be a style (Using map for side effects should be rewritten) or complexity (Using map for side effects might be confusing and can be written less complex) lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I just thought it was too invasive at first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it. If people complain, we can still move it to pedantic.

"suspicious usage of map"
}

declare_lint_pass!(Methods => [
OPTION_UNWRAP_USED,
RESULT_UNWRAP_USED,
Expand Down Expand Up @@ -927,6 +944,7 @@ declare_lint_pass!(Methods => [
UNNECESSARY_FILTER_MAP,
INTO_ITER_ON_ARRAY,
INTO_ITER_ON_REF,
SUSPICIOUS_MAP,
]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
Expand Down Expand Up @@ -972,6 +990,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0]),
["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
["count", "map"] => lint_suspicious_map(cx, expr),
_ => {},
}

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

fn lint_suspicious_map(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
span_lint(
cx,
SUSPICIOUS_MAP,
expr.span,
"Make sure you did not confuse `map` with `filter`.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone should document the rules of rust error messages and suggestions somewhere somtimes. 😄

Suggested change
"Make sure you did not confuse `map` with `filter`.",
"make sure you did not confuse `map` with `filter`",

IMO this should be a span_help_and_lint:

  • message: "this call to map() won't have an effect on the call to count()"
  • help: "make sure you did not confuse map with fliter"

);
}

/// Given a `Result<T, E>` type, return its error type (`E`).
fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
if let ty::Adt(_, substs) = ty.sty {
Expand Down
9 changes: 8 additions & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use lint::Lint;
pub use lint::LINT_LEVELS;

// begin lint list, do not remove this comment, it’s used in `update_lints`
pub const ALL_LINTS: [Lint; 309] = [
pub const ALL_LINTS: [Lint; 310] = [
Lint {
name: "absurd_extreme_comparisons",
group: "correctness",
Expand Down Expand Up @@ -1736,6 +1736,13 @@ pub const ALL_LINTS: [Lint; 309] = [
deprecation: None,
module: "formatting",
},
Lint {
name: "suspicious_map",
group: "pedantic",
desc: "suspicious usage of map",
deprecation: None,
module: "methods",
},
Lint {
name: "suspicious_op_assign_impl",
group: "correctness",
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/suspicious_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(clippy::suspicious_map)]

fn main() {
let _ = (0..3).map(|x| x + 2).count();
}
10 changes: 10 additions & 0 deletions tests/ui/suspicious_map.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: Make sure you did not confuse `map` with `filter`.
--> $DIR/suspicious_map.rs:4:13
|
LL | let _ = (0..3).map(|x| x + 2).count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::suspicious-map` implied by `-D warnings`

error: aborting due to previous error