rust-lang / rust-lang/rust-clippy

Do not lint range_plus_one inside implementations of Index<RangeInclusive<_>>

Open
#3,583 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When implementing indexing on RangeInclusive, we obviously cannot replace start .. end+1 with start ..= end, as that would be an infinite loop.

My situation is something like the following:

struct MyVec {
    inner: Vec<i64>,
}

impl Index<Range<usize>> for MyVec {
    type Output = [i64];
    fn index(&self, i: Range<usize>) -> &[i64] {
        &self.inner[i.start .. i.end]
    }
}

impl Index<RangeInclusive<usize>> for MyVec {
    type Output = [i64];
    fn index(&self, i: RangeInclusive<usize>) -> &[i64] {
        self.index(*i.start()..(*i.end() + 1))
    }
}

Here the impl of RangeInclusive delegates to the impl of Range by turning the inclusive range into an exclusive range.

Of course the same goes for IndexMut.

The warning is:

warning: an inclusive range would be more readable                                                                                                                                             
   --> src/lib.rs:15:20                                                                                                                                                                       
    |                                                                                                                                                                                          
 15 |         self.index(*i.start()..(*i.end() + 1))                                                                                                                                           
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `*i.start()..=*i.end()`                                                                                                         
    |                                                                                                                                                                                          
    = note: #[warn(clippy::range_plus_one)] on by default                                                                                                                                      
    = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#range_plus_one 

Version:

$ cargo clippy -V
clippy 0.0.212 (2e26fdc 2018-11-22)

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 at the range_plus_one lint and reproduce the example from src/lib.rs, including both Index and IndexMut implementations. The work is done when these delegation patterns no longer produce the warning without suppressing valid range_plus_one diagnostics.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.