Skip to content

Functional constructs tend to silently drop errors when iterating Result. #9408

Open
@tdkt

Description

@tdkt

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions