rust/tests/ui/indexing_slicing.rs
Shea Newton a7c0ff3fa6
This commit represents an attempt to address changes requested in the process of reviewing PR #2790.
The changes reflected in this commit are as follows:

- Revised `IndexingSlicingPass` struct name to IndexingSlicing for consistency with the rest of the code base.
- Revised match arm condition to use `(..)` shorthand in favor of `(_, _, _)`.
- Restored a couple telling variable names.
- Calls to `cx.span_lint` were revised to use `utils::span_help_and_lint`.
- Took a stab at refactoring some generalizable calls to `utils::span_help_and_lint` to minimize duplicate code.
- Revised INDEXING_SLICING declaration to pedantic rather than restriction.
- Added `&x[0..].get(..3)` to the test cases.
2018-06-19 16:28:10 +00:00

61 lines
1,018 B
Rust

#![feature(plugin)]
#![warn(indexing_slicing)]
#![warn(out_of_bounds_indexing)]
#![allow(no_effect, unnecessary_operation)]
fn main() {
let x = [1, 2, 3, 4];
let index: usize = 1;
let index_from: usize = 2;
let index_to: usize = 3;
x[index];
&x[index_from..index_to];
&x[index_from..][..index_to];
&x[index..];
&x[..index];
x[0];
x[3];
x[4];
x[1 << 3];
&x[1..5];
&x[1..][..5];
&x[0..3];
&x[0..][..3];
&x[0..].get(..3); // Ok
&x[0..=4];
&x[..=4];
&x[..];
&x[1..];
&x[4..];
&x[5..];
&x[..4];
&x[..5];
let y = &x;
y[0];
&y[1..2];
&y[..];
&y[0..=4];
&y[..=4];
let empty: [i8; 0] = [];
empty[0];
&empty[1..5];
&empty[0..=4];
&empty[..=4];
&empty[..];
&empty[0..];
&empty[0..0];
&empty[0..=0];
&empty[..=0];
&empty[..0];
&empty[1..];
&empty[..4];
let v = vec![0; 5];
v[0];
v[10];
&v[10..100];
&v[10..];
&v[..100];
}