Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up indexing_slicing ui test #5130

Merged
merged 3 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_dev/src/stderr_length_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::prelude::*;
// The maximum length allowed for stderr files.
//
// We limit this because small files are easier to deal with than bigger files.
const LIMIT: usize = 220;
const LIMIT: usize = 200;

pub fn check() {
let stderr_files = stderr_files();
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/indexing_slicing_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#![warn(clippy::indexing_slicing)]
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
// we want to avoid false positives.
#![warn(clippy::out_of_bounds_indexing)]
#![allow(clippy::no_effect, clippy::unnecessary_operation)]

fn main() {
let x = [1, 2, 3, 4];
let index: usize = 1;
x[index];
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.

x[0]; // Ok, should not produce stderr.
x[3]; // Ok, should not produce stderr.

let y = &x;
y[0];

let v = vec![0; 5];
v[0];
v[10];
v[1 << 3];

const N: usize = 15; // Out of bounds
const M: usize = 3; // In bounds
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
x[M]; // Ok, should not produce stderr.
v[N];
v[M];
}
79 changes: 79 additions & 0 deletions tests/ui/indexing_slicing_index.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
error: index out of bounds: the len is 4 but the index is 4
--> $DIR/indexing_slicing_index.rs:11:5
|
LL | x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
| ^^^^
|
= note: `#[deny(const_err)]` on by default

error: index out of bounds: the len is 4 but the index is 8
--> $DIR/indexing_slicing_index.rs:12:5
|
LL | x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
| ^^^^^^^^^

error: index out of bounds: the len is 4 but the index is 15
--> $DIR/indexing_slicing_index.rs:27:5
|
LL | x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
| ^^^^

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:10:5
|
LL | x[index];
| ^^^^^^^^
|
= note: `-D clippy::indexing-slicing` implied by `-D warnings`
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:18:5
|
LL | y[0];
| ^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:21:5
|
LL | v[0];
| ^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:22:5
|
LL | v[10];
| ^^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:23:5
|
LL | v[1 << 3];
| ^^^^^^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:29:5
|
LL | v[N];
| ^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: indexing may panic.
--> $DIR/indexing_slicing_index.rs:30:5
|
LL | v[M];
| ^^^^
|
= help: Consider using `.get(n)` or `.get_mut(n)` instead

error: aborting due to 10 previous errors

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(plugin)]
#![warn(clippy::indexing_slicing)]
// We also check the out_of_bounds_indexing lint here, because it lints similar things and
// we want to avoid false positives.
Expand All @@ -10,49 +9,29 @@ fn main() {
let index: usize = 1;
let index_from: usize = 2;
let index_to: usize = 3;
x[index];
&x[index..];
&x[..index];
&x[index_from..index_to];
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
&x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
&x[0..][..3];
&x[1..][..5];

&x[0..].get(..3); // Ok, should not produce stderr.
x[0]; // Ok, should not produce stderr.
x[3]; // Ok, should not produce stderr.
&x[0..3]; // Ok, should not produce stderr.

let y = &x;
y[0];
&y[1..2];
&y[0..=4];
&y[..=4];

&y[..]; // Ok, should not produce stderr.

let v = vec![0; 5];
v[0];
v[10];
v[1 << 3];
&v[10..100];
&x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
&v[10..];
&v[..100];

&v[..]; // Ok, should not produce stderr.

//
// Continue tests at end function to minimize the changes to this file's corresponding stderr.
//

const N: usize = 15; // Out of bounds
const M: usize = 3; // In bounds
x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
x[M]; // Ok, should not produce stderr.
v[N];
v[M];
}
Loading