Small nits on INDEXING_SLICING
This commit is contained in:
parent
87ef5f4d3b
commit
2f13c3bdef
3 changed files with 16 additions and 29 deletions
|
@ -69,34 +69,26 @@ impl LateLintPass for ArrayIndexing {
|
||||||
if let Ok(ConstVal::Uint(const_index)) = const_index {
|
if let Ok(ConstVal::Uint(const_index)) = const_index {
|
||||||
if size <= const_index {
|
if size <= const_index {
|
||||||
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
|
utils::span_lint(cx, OUT_OF_BOUNDS_INDEXING, e.span, "const index is out of bounds");
|
||||||
utils::span_lint(cx, INDEXING_SLICING, e.span, "indexing may panic");
|
|
||||||
} else {
|
|
||||||
// Index is within bounds
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index is a constant range
|
// Index is a constant range
|
||||||
if let Some(range) = utils::unsugar_range(index) {
|
if let Some(range) = utils::unsugar_range(index) {
|
||||||
let start = range.start.map(|start|
|
let start = range.start.map(|start|
|
||||||
eval_const_expr_partial(cx.tcx, start, ExprTypeChecked, None));
|
eval_const_expr_partial(cx.tcx, start, ExprTypeChecked, None)).map(|v| v.ok());
|
||||||
let end = range.end.map(|end|
|
let end = range.end.map(|end|
|
||||||
eval_const_expr_partial(cx.tcx, end, ExprTypeChecked, None));
|
eval_const_expr_partial(cx.tcx, end, ExprTypeChecked, None)).map(|v| v.ok());
|
||||||
|
|
||||||
if let Some((start, end)) = to_const_range(start, end, range.limits, size) {
|
if let Some((start, end)) = to_const_range(start, end, range.limits, size) {
|
||||||
if start >= size && end >= size {
|
if start >= size || end >= size {
|
||||||
utils::span_lint(cx,
|
utils::span_lint(cx,
|
||||||
OUT_OF_BOUNDS_INDEXING,
|
OUT_OF_BOUNDS_INDEXING,
|
||||||
e.span,
|
e.span,
|
||||||
"range is out of bounds");
|
"range is out of bounds");
|
||||||
utils::span_lint(cx,
|
|
||||||
INDEXING_SLICING,
|
|
||||||
e.span,
|
|
||||||
"slicing may panic");
|
|
||||||
} else {
|
|
||||||
// Range is within bounds
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,19 +112,19 @@ impl LateLintPass for ArrayIndexing {
|
||||||
///
|
///
|
||||||
/// Note: we assume the start and the end of the range are unsigned, since array slicing
|
/// Note: we assume the start and the end of the range are unsigned, since array slicing
|
||||||
/// works only on usize
|
/// works only on usize
|
||||||
fn to_const_range<T>(start: Option<Result<ConstVal, T>>,
|
fn to_const_range(start: Option<Option<ConstVal>>,
|
||||||
end: Option<Result<ConstVal, T>>,
|
end: Option<Option<ConstVal>>,
|
||||||
limits: RangeLimits,
|
limits: RangeLimits,
|
||||||
array_size: u64)
|
array_size: u64)
|
||||||
-> Option<(u64, u64)> {
|
-> Option<(u64, u64)> {
|
||||||
let start = match start {
|
let start = match start {
|
||||||
Some(Ok(ConstVal::Uint(x))) => x,
|
Some(Some(ConstVal::Uint(x))) => x,
|
||||||
Some(_) => return None,
|
Some(_) => return None,
|
||||||
None => 0,
|
None => 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
let end = match end {
|
let end = match end {
|
||||||
Some(Ok(ConstVal::Uint(x))) => {
|
Some(Some(ConstVal::Uint(x))) => {
|
||||||
if limits == RangeLimits::Closed {
|
if limits == RangeLimits::Closed {
|
||||||
x
|
x
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#![feature(rustc_private, collections)]
|
#![feature(rustc_private, collections)]
|
||||||
#![feature(iter_arith)]
|
#![feature(iter_arith)]
|
||||||
#![feature(custom_attribute)]
|
#![feature(custom_attribute)]
|
||||||
#![allow(unknown_lints)]
|
#![allow(indexing_slicing, shadow_reuse, unknown_lints)]
|
||||||
|
|
||||||
// this only exists to allow the "dogfood" integration test to work
|
// this only exists to allow the "dogfood" integration test to work
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
|
@ -9,20 +9,15 @@ fn main() {
|
||||||
let x = [1,2,3,4];
|
let x = [1,2,3,4];
|
||||||
x[0];
|
x[0];
|
||||||
x[3];
|
x[3];
|
||||||
x[4]; //~ERROR: indexing may panic
|
x[4]; //~ERROR: const index is out of bounds
|
||||||
//~^ ERROR: const index is out of bounds
|
x[1 << 3]; //~ERROR: const index is out of bounds
|
||||||
x[1 << 3]; //~ERROR: indexing may panic
|
&x[1..5]; //~ERROR: range is out of bounds
|
||||||
//~^ ERROR: const index is out of bounds
|
|
||||||
&x[1..5]; //~ERROR: slicing may panic
|
|
||||||
//~^ ERROR: range is out of bounds
|
|
||||||
&x[0..3];
|
&x[0..3];
|
||||||
&x[0...4]; //~ERROR: slicing may panic
|
&x[0...4]; //~ERROR: range is out of bounds
|
||||||
//~^ ERROR: range is out of bounds
|
|
||||||
&x[..];
|
&x[..];
|
||||||
&x[1..];
|
&x[1..];
|
||||||
&x[..4];
|
&x[..4];
|
||||||
&x[..5]; //~ERROR: slicing may panic
|
&x[..5]; //~ERROR: range is out of bounds
|
||||||
//~^ ERROR: range is out of bounds
|
|
||||||
|
|
||||||
let y = &x;
|
let y = &x;
|
||||||
y[0]; //~ERROR: indexing may panic
|
y[0]; //~ERROR: indexing may panic
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue