rust-lang / rust-lang/rust-clippy

`range_plus_one` also triggers if both sides have a plus one

Open
#13,138 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

When constructing ranges where both sides have a + 1 the lint suggestion makes the code less clear.

Lint Name

range_plus_one

Reproducer

I tried this code:

let mmap = b"Some memory contents";
let offset = 4;
let load = 6;
let bits_eight = &mmap[offset + 1..offset + load + 1];

I saw this happen:

warning: an inclusive range would be more readable
   --> simdvcd/src/lib.rs:469:49
    |
469 |                     let bits_eight = &self.mmap[self.offset + 1..self.offset + load + 1];
    |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `(self.offset + 1)..=(self.offset + load)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
    = note: `-W clippy::range-plus-one` implied by `-W clippy::pedantic`
    = help: to override `-W clippy::pedantic` add `#[allow(clippy::range_plus_one)]`

I expected to see this happen:
No warning

Version
rustc 1.81.0-nightly (506985649 2024-07-20)
binary: rustc
commit-hash: 5069856495870486134dd2ca0b0e2516308c5c2a
commit-date: 2024-07-20
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
Additional Labels

No response

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

Start by locating the range_plus_one lint implementation and its test coverage. Reproduce the reported slice using offset + 1..offset + load + 1, then verify that this two-sided + 1 case produces no warning while the intended lint cases retain their existing behavior.

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.