Skip to content

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

Open
@Darksonn

Description

@Darksonn

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions