rust-lang / rust-lang/rust-clippy

Factor `return`, `break` and `continue` from multiple branches

Open
#14,545 0 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

What it does

When all branches in a if/else[/if/…] or match are divergent, and several of them end with the same construct (return, break or continue), some things may be factored out, for example the most commonly used:

  • return or break may be moved before the if or match if they have a payload
  • in other cases, they may be moved after the if or match
Advantage
  • Remove redundant keywords
Drawbacks
  • May make the code harder to follow
Example
fn f(a: i32) -> i32 {
    if a > 3 {
        if a > 10 {
            return a;
        } else {
            return a+1;
        }
    }
    0
}

could be written as

fn f(a: i32) -> i32 {
    if a > 3 {
        return if a > 10 { a } else { a+1 };
    }
    0
}

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

No files, tests, or entry points are named. Start by examining the proposed Rust examples and existing Clippy handling for divergent if/match branches; done means eligible return, break, or continue constructs can be factored while preserving behavior.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.