Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::map_with_unused_argument_over_ranges: map of a closure that does not depend on its parameter over a range
What it meansChecks for Iterator::map over ranges without using the parameter which could be more clearly expressed using std::iter::repeat(...).take(...) or std::iter::repeat_n. It is in the restriction group and off by default.
What you see
warning: map of a closure that does not depend on its parameter over a range --> main.rs:3:31 | 3 | let random_numbers : Vec<_> = (0..10).map(|_| { 3 + 1 }).collect(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#map_with_unused_argument_over_ranges = note: requested on the command line with `-W clippy::map-with-unused-argument-over-ranges` help: remove the explicit range and use `repeat_n` | 3 - let random_numbers : Vec<_> = (0..10).map(|_| { 3 + 1 }).collect(); 3 + let random_numbers : Vec<_> = std::iter::repeat_n({ 3 + 1 }, 10).collect(); |
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 expresses the intent more clearly to take the correct number of times from a generating function than to apply a closure to each number in a range only to discard them.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let random_numbers : Vec<_> = (0..10).map(|_| { 3 + 1 }).collect();
Write this instead
let f : Vec<_> = std::iter::repeat( 3 + 1 ).take(10).collect();
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
map_with_unused_argument_over_ranges = "warn"In code
#![warn(clippy::map_with_unused_argument_over_ranges)]One run
cargo clippy -- -W clippy::map-with-unused-argument-over-rangesThe [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.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for Iterator::map over ranges without using the parameter which
could be more clearly expressed using std::iter::repeat(...).take(...)
or std::iter::repeat_n.
Why is this bad?
It expresses the intent more clearly to take the correct number of times
from a generating function than to apply a closure to each number in a
range only to discard them.
Example
let random_numbers : Vec<_> = (0..10).map(|_| { 3 + 1 }).collect();
Use instead:
let f : Vec<_> = std::iter::repeat( 3 + 1 ).take(10).collect();
Known Issues
This lint may suggest replacing a Map<Range> with a Take<RepeatWith>.
The former implements some traits that the latter does not, such as
DoubleEndedIterator.
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- little_endian_bytesuse of `to_le_bytes`
- lossy_float_literalliteral cannot be represented as the underlying type without loss of precision
- map_err_ignore`map_err(|_|...` wildcard pattern discards the original error
- mem_forgetChecks for usage of std::mem::forget(t) where t is Drop or has a field that implements Drop
- min_ident_charsChecks for identifiers which consist of a single character (or fewer than the configured threshold). Note: This lint can be very noisy when enabled; it may be desirable to only enable it temporarily
- missing_assert_messageChecks assertions without a custom panic message
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?