rust-lang / rust-lang/rust-clippy

range_plus_one help suggestion should not remove braces

Open
#3,103 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The following code triggers the range_plus_one lint correctly. However the suggestion, if applied explicitly ie by an IDE, removes the braces which causes a compile error.

fn main() {
    let from = 5;
    let to = 16;
    for n in (from..to + 1).rev() {
        eprintln!("{}", n);
    }
}
  --> src/main.rs:17:14
   |
17 |     for n in (from..to + 1).rev() {
   |              ^^^^^^^^^^^^^^ help: use: `from..=to`
   |
   = note: #[warn(range_plus_one)] on by default
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#range_plus_one

Incorrect suggested code:

fn main() {
    let from = 5;
    let to = 16;
    for n in from..=to.rev() {
        eprintln!("{}", n);
    }
}

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

Reproduce the range_plus_one lint with the Rust example in the issue and inspect the lint's suggestion-generation entry point. Check why the replacement drops the surrounding range parentheses, then verify that the suggested code preserves them, compiles, and still produces the intended inclusive range.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.