Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::suspicious_map: this call to `map()` won't have an effect on the call to `count()`
What it meansChecks for calls to map followed by a count. It is in the suspicious group and warns by default.
What you see
warning: this call to `map()` won't have an effect on the call to `count()` --> main.rs:3:9 | 3 | let _ = (0..3).map(|x| x + 2).count(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: make sure you did not confuse `map` with `filter`, `for_each` or `inspect` = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#suspicious_map = note: `#[warn(clippy::suspicious_map)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
It looks suspicious. Maybe map was confused with filter. If the map call is intentional, this should be rewritten using inspect. Or, if you intend to drive the iterator to completion, you can just use for_each instead.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let _ = (0..3).map(|x| x + 2).count();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::suspicious_map)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
suspicious_map = "allow"One run
cargo clippy -- -A clippy::suspicious-mapThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
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.
If the map call is intentional, this should be rewritten
using inspect. Or, if you intend to drive the iterator to
completion, you can just use for_each instead.
Example
let _ = (0..3).map(|x| x + 2).count();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- suspicious_command_arg_spacesingle argument that looks like it should be multiple arguments
- suspicious_doc_commentsthis is an outer doc comment and does not apply to the parent module or crate
- suspicious_else_formattingthis is an `else {..}` but the formatting might hide it
- suspicious_op_assign_implLints for suspicious operations in impls of OpAssign, e.g. subtracting elements in an AddAssign impl
- suspicious_open_optionsChecks for the suspicious use of OpenOptions::create() without an explicit OpenOptions::truncate()
- suspicious_to_ownedChecks for the usage of _.to_owned(), on a Cow<'_, _>
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::suspicious_command_arg_spacesingle argument that looks like it should be multiple argumentsRead it next →Did this answer your question?