Skip to content

Commit 2f13c3b

Browse files
committed
Small nits on INDEXING_SLICING
1 parent 87ef5f4 commit 2f13c3b

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

src/array_indexing.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,26 @@ impl LateLintPass for ArrayIndexing {
6969
if let Ok(ConstVal::Uint(const_index)) = const_index {
7070
if size <= const_index {
7171
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
72-
utils::span_lint(cx, INDEXING_SLICING, e.span, "indexing may panic");
73-
} else {
74-
// Index is within bounds
75-
return;
7672
}
73+
74+
return;
7775
}
7876

7977
// Index is a constant range
8078
if let Some(range) = utils::unsugar_range(index) {
8179
let start = range.start.map(|start|
82-
eval_const_expr_partial(cx.tcx, start, ExprTypeChecked, None));
80+
eval_const_expr_partial(cx.tcx, start, ExprTypeChecked, None)).map(|v| v.ok());
8381
let end = range.end.map(|end|
84-
eval_const_expr_partial(cx.tcx, end, ExprTypeChecked, None));
82+
eval_const_expr_partial(cx.tcx, end, ExprTypeChecked, None)).map(|v| v.ok());
8583

8684
if let Some((start, end)) = to_const_range(start, end, range.limits, size) {
87-
if start >= size && end >= size {
85+
if start >= size || end >= size {
8886
utils::span_lint(cx,
8987
OUT_OF_BOUNDS_INDEXING,
9088
e.span,
9189
"range is out of bounds");
92-
utils::span_lint(cx,
93-
INDEXING_SLICING,
94-
e.span,
95-
"slicing may panic");
96-
} else {
97-
// Range is within bounds
98-
return;
9990
}
91+
return;
10092
}
10193
}
10294
}
@@ -120,19 +112,19 @@ impl LateLintPass for ArrayIndexing {
120112
///
121113
/// Note: we assume the start and the end of the range are unsigned, since array slicing
122114
/// works only on usize
123-
fn to_const_range<T>(start: Option<Result<ConstVal, T>>,
124-
end: Option<Result<ConstVal, T>>,
115+
fn to_const_range(start: Option<Option<ConstVal>>,
116+
end: Option<Option<ConstVal>>,
125117
limits: RangeLimits,
126118
array_size: u64)
127119
-> Option<(u64, u64)> {
128120
let start = match start {
129-
Some(Ok(ConstVal::Uint(x))) => x,
121+
Some(Some(ConstVal::Uint(x))) => x,
130122
Some(_) => return None,
131123
None => 0,
132124
};
133125

134126
let end = match end {
135-
Some(Ok(ConstVal::Uint(x))) => {
127+
Some(Some(ConstVal::Uint(x))) => {
136128
if limits == RangeLimits::Closed {
137129
x
138130
} else {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(rustc_private, collections)]
33
#![feature(iter_arith)]
44
#![feature(custom_attribute)]
5-
#![allow(unknown_lints)]
5+
#![allow(indexing_slicing, shadow_reuse, unknown_lints)]
66

77
// this only exists to allow the "dogfood" integration test to work
88
#[allow(dead_code)]

tests/compile-fail/array_indexing.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@ fn main() {
99
let x = [1,2,3,4];
1010
x[0];
1111
x[3];
12-
x[4]; //~ERROR: indexing may panic
13-
//~^ ERROR: const index is out of bounds
14-
x[1 << 3]; //~ERROR: indexing may panic
15-
//~^ ERROR: const index is out of bounds
16-
&x[1..5]; //~ERROR: slicing may panic
17-
//~^ ERROR: range is out of bounds
12+
x[4]; //~ERROR: const index is out of bounds
13+
x[1 << 3]; //~ERROR: const index is out of bounds
14+
&x[1..5]; //~ERROR: range is out of bounds
1815
&x[0..3];
19-
&x[0...4]; //~ERROR: slicing may panic
20-
//~^ ERROR: range is out of bounds
16+
&x[0...4]; //~ERROR: range is out of bounds
2117
&x[..];
2218
&x[1..];
2319
&x[..4];
24-
&x[..5]; //~ERROR: slicing may panic
25-
//~^ ERROR: range is out of bounds
20+
&x[..5]; //~ERROR: range is out of bounds
2621

2722
let y = &x;
2823
y[0]; //~ERROR: indexing may panic

0 commit comments

Comments
 (0)