Skip to content

Commit 6dfc169

Browse files
committed
Add ui test of lint
1 parent b87d42c commit 6dfc169

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

tests/ui/might_panic.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(clippy::might_panic)]
2+
3+
fn main() {
4+
// declare 2 array to access
5+
let arr1 = &[1, 2, 3];
6+
let arr2 = &[[1], [2], [3]];
7+
8+
// trigger `might-panic` lint
9+
// with and without explicit type declaration
10+
let Some(_num1) = arr1.get(1) else { panic!() };
11+
let Some(_num2): Option<&i32> = arr1.get(5) else { panic!() };
12+
13+
let Some(_num3) = arr2.get(1) else { panic!() };
14+
let Some(_num4): Option<&[i32; 1]> = arr2.get(5) else { panic!() };
15+
16+
// not trigger `might-panic` lint
17+
let _num5 = arr2[0][0];
18+
}

tests/ui/might_panic.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![allow(clippy::might_panic)]
2+
3+
fn main() {
4+
// declare 2 array to access
5+
let arr1 = &[1, 2, 3];
6+
let arr2 = &[[1], [2], [3]];
7+
8+
// trigger `might-panic` lint
9+
// with and without explicit type declaration
10+
let _num1 = arr1[1]; // warning
11+
let _num2: i32 = arr1[5]; // warning
12+
13+
let _num3 = arr2[1]; // warning
14+
let _num4: [i32; 1] = arr2[5]; // warning
15+
16+
// not trigger `might-panic` lint
17+
let _num5 = arr2[0][0]; // no warning
18+
}

tests/ui/might_panic.stderr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
warning: use `get()` function if you're not sure whether index is legal
2+
--> src/main.rs:8:5
3+
|
4+
8 | let _num1 = arr1[1];
5+
| ^^^^^^^^^^^^^^^^^^^^ help: Try: `let Some(_num1) = arr1.get(1) else { panic!() };`
6+
|
7+
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#might_panic
8+
= note: `#[warn(clippy::might_panic)]` on by default
9+
10+
warning: use `get()` function if you're not sure whether index is legal
11+
--> src/main.rs:9:5
12+
|
13+
9 | let _num2: i32 = arr1[5];
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: Try: `let Some(_num2): Option<&i32> = arr1.get(5) else { panic!() };`
15+
|
16+
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#might_panic
17+
18+
warning: use `get()` function if you're not sure whether index is legal
19+
--> src/main.rs:11:5
20+
|
21+
11 | let _num3 = arr2[1];
22+
| ^^^^^^^^^^^^^^^^^^^^ help: Try: `let Some(_num3) = arr2.get(1) else { panic!() };`
23+
|
24+
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#might_panic
25+
26+
warning: use `get()` function if you're not sure whether index is legal
27+
--> src/main.rs:12:5
28+
|
29+
12 | let _num4: [i32; 1] = arr2[5];
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: Try: `let Some(_num4): Option<&[i32; 1]> = arr2.get(5) else { panic!() };`
31+
|
32+
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#might_panic
33+
34+
warning: `src` (bin "src") generated 4 warnings (run `cargo clippy --fix --bin "src"` to apply 4 suggestions)

0 commit comments

Comments
 (0)