1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#![warn(clippy::range_minus_one, clippy::range_plus_one)]
#![allow(unused_parens)]
#![allow(clippy::iter_with_drain)]
use std::ops::{Index, IndexMut, Range, RangeBounds, RangeInclusive};
fn f() -> usize {
42
}
macro_rules! macro_plus_one {
($m: literal) => {
for i in 0..$m + 1 {
println!("{}", i);
}
};
}
macro_rules! macro_minus_one {
($m: literal) => {
for i in 0..=$m - 1 {
println!("{}", i);
}
};
}
fn main() {
for _ in 0..2 {}
for _ in 0..=2 {}
for _ in 0..=3 {}
//~^ range_plus_one
for _ in 0..=3 + 1 {}
for _ in 0..=5 {}
//~^ range_plus_one
for _ in 0..=1 + 5 {}
for _ in 1..=1 {}
//~^ range_plus_one
for _ in 1..=1 + 1 {}
for _ in 0..13 + 13 {}
for _ in 0..=13 - 7 {}
for _ in 0..=f() {}
//~^ range_plus_one
for _ in 0..=(1 + f()) {}
// Those are not linted, as in the general case we cannot be sure that the exact type won't be
// important.
let _ = ..11 - 1;
let _ = ..=11 - 1;
let _ = ..=(11 - 1);
let _ = (1..11 + 1);
let _ = (f() + 1)..(f() + 1);
const ONE: usize = 1;
// integer consts are linted, too
for _ in 1..=ONE {}
//~^ range_plus_one
let mut vec: Vec<()> = std::vec::Vec::new();
vec.drain(..);
macro_plus_one!(5);
macro_minus_one!(5);
// As an instance of `Iterator`
(1..=10).for_each(|_| {});
//~^ range_plus_one
// As an instance of `IntoIterator`
#[allow(clippy::useless_conversion)]
(1..=10).into_iter().for_each(|_| {});
//~^ range_plus_one
// As an instance of `RangeBounds`
{
let _ = (1..=10).start_bound();
//~^ range_plus_one
}
// As a `SliceIndex`
let a = [10, 20, 30];
let _ = &a[1..=1];
//~^ range_plus_one
// As method call argument
vec.drain(2..=3);
//~^ range_plus_one
// As function call argument
take_arg(10..=20);
//~^ range_plus_one
// As function call argument inside a block
take_arg({ 10..=20 });
//~^ range_plus_one
// Do not lint in case types are unified
take_arg(if true { 10..20 } else { 10..20 + 1 });
// Do not lint, as the same type is used for both parameters
take_args(10..20 + 1, 10..21);
// Do not lint, as the range type is also used indirectly in second parameter
take_arg_and_struct(10..20 + 1, S { t: 1..2 });
// As target of `IndexMut`
let mut a = [10, 20, 30];
a[0..=2][0] = 1;
//~^ range_plus_one
}
fn take_arg<T: Iterator<Item = u32>>(_: T) {}
fn take_args<T: Iterator<Item = u32>>(_: T, _: T) {}
struct S<T> {
t: T,
}
fn take_arg_and_struct<T: Iterator<Item = u32>>(_: T, _: S<T>) {}
fn no_index_by_range_inclusive(a: usize) {
struct S;
impl Index<Range<usize>> for S {
type Output = [u32];
fn index(&self, _: Range<usize>) -> &Self::Output {
&[]
}
}
_ = &S[0..a + 1];
}
fn no_index_mut_with_switched_range(a: usize) {
struct S(u32);
impl Index<Range<usize>> for S {
type Output = u32;
fn index(&self, _: Range<usize>) -> &Self::Output {
&self.0
}
}
impl IndexMut<Range<usize>> for S {
fn index_mut(&mut self, _: Range<usize>) -> &mut Self::Output {
&mut self.0
}
}
impl Index<RangeInclusive<usize>> for S {
type Output = u32;
fn index(&self, _: RangeInclusive<usize>) -> &Self::Output {
&self.0
}
}
S(2)[0..a + 1] = 3;
}
fn issue9908() {
// Simplified test case
let _ = || 0..=1;
// Original test case
let full_length = 1024;
let range = {
// do some stuff, omit here
None
};
let range = range.map(|(s, t)| s..=t).unwrap_or(0..=(full_length - 1));
assert_eq!(range, 0..=1023);
}
fn issue9908_2(n: usize) -> usize {
(1..n).sum()
//~^ range_minus_one
}
|