rust-lang / rust-lang/rust-clippy

Suggestion: lint against side-effectful map

Open
#6,014 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-tests
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Lint against side-effects in map, or at least println!

Categories (optional)
  • Kind: Correctness or Style

rustc already lints against unused iterators, but sometimes people "fix" this by marking the iterator as discarded. Clippy should be more suspicious: It is not very likely an experienced Rustacean really meant to use map and not for_each here.

Drawbacks

Occasionally, sometimes this will be on purpose.

Example
fn main() {
    let values = vec!["a", "b", "c"];
    let _results = values.into_iter()
        .map(|val| {
            println!("Value: {}", val);
        });
}

Could be written as:

fn main() {
    let values = vec!["a", "b", "c"];
    let _results = values.into_iter()
        .for_each(|val| {
            println!("Value: {}", val);
        });
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Clippy's existing iterator-related lints and the issue's map and for_each examples. Determine how side effects such as println! should be identified and how intentional uses can avoid noisy warnings. Done means the lint detects the described pattern, gives a useful recommendation, and covers both the warning and acceptable cases with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.