]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/src/docs/reversed_empty_ranges.txt
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / clippy / src / docs / reversed_empty_ranges.txt
1 ### What it does
2 Checks for range expressions `x..y` where both `x` and `y`
3 are constant and `x` is greater or equal to `y`.
4
5 ### Why is this bad?
6 Empty ranges yield no values so iterating them is a no-op.
7 Moreover, trying to use a reversed range to index a slice will panic at run-time.
8
9 ### Example
10 ```
11 fn main() {
12 (10..=0).for_each(|x| println!("{}", x));
13
14 let arr = [1, 2, 3, 4, 5];
15 let sub = &arr[3..1];
16 }
17 ```
18 Use instead:
19 ```
20 fn main() {
21 (0..=10).rev().for_each(|x| println!("{}", x));
22
23 let arr = [1, 2, 3, 4, 5];
24 let sub = &arr[1..3];
25 }
26 ```