rust-lang / rust-lang/rust-clippy

lint suggestion: suggest use of Result::map/Result::and_then

Open
#2,271 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

In my own code I noticed that a common pattern like this:

let file = File::Create(file_name)?;
let mut writer = BufWriter::new(file);

can be more succinctly expressed as:

let mut writer = File::Create(file_name).map(BufWriter::new)?;

This seems like it would be an easy lint to implement and would be applicable any time a let-binded variable that came through a try! or operator? is immediately moved into a successive function. Usage of map or and_then could be suggested based on the whether the return type is a Result or not. If the chain is large there may be some small performance benefit to doing things this way due to the reduced number of implicit try! calls, but I'd have to measure this to be sure.

However there is an issue that many programmers may find the non-chained code to be more clear, so I'm not sure if it would be an appropriate lint for something like clippy.

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

The issue names no files, tests, or entry points. Start with the Rust examples showing File::Create followed by BufWriter::new, then resolve when map versus and_then should be suggested and whether the chained form is clearer; done means the lint’s scope and behavior are agreed and implemented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.