rust-lang / rust-lang/rust-clippy

Functional constructs tend to silently drop errors when iterating `Result`.

Open
#9,408 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-question
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

I recently found several bugs in production code. They are due to iter/into_iter being called on results.

Examples:

let my_result: Result<Vec<Result<_,_>>,_> = ...;
my_result
   .into_iter()
   .flat_map(...)
   .collect::<Result<Vec<_>,_>>()

will let us blissfully iterate our Vec and early return (intentionally?) on the Results inside the Vec. When the outside Result is an error, it becomes an empty iterator, dropping the error silently and collecting into Ok(vec![]).

let my_vec: Vec<Result<_,_>> = ...;

my_vec
   .into_iter()
   .flatten()
   ...

This does the same as above, because it calls into_iter on the the Results inside the Vec.

These are quite unintuitive ways of shooting oneself in the foot. One might expect that an error does not disappear unless you unwrap, match or get a must_use warning...

I'm not sure what the best solution is here. Should must_use be triggered here? Should clippy warn about this?

Version

No response

Additional Labels

@rustbot label +C-question

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

Begin with the two Rust examples in the issue, focusing on how iterating Result can discard errors. Determine whether the desired outcome is a must_use change or a Clippy lint, and define tests that distinguish intentional iteration from silently ignored errors. The issue is complete when the behavior and warning policy are decided and covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.