]> git.lizzy.rs Git - rust.git/blob - src/docs/needless_continue.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / needless_continue.txt
1 ### What it does
2 The lint checks for `if`-statements appearing in loops
3 that contain a `continue` statement in either their main blocks or their
4 `else`-blocks, when omitting the `else`-block possibly with some
5 rearrangement of code can make the code easier to understand.
6
7 ### Why is this bad?
8 Having explicit `else` blocks for `if` statements
9 containing `continue` in their THEN branch adds unnecessary branching and
10 nesting to the code. Having an else block containing just `continue` can
11 also be better written by grouping the statements following the whole `if`
12 statement within the THEN block and omitting the else block completely.
13
14 ### Example
15 ```
16 while condition() {
17     update_condition();
18     if x {
19         // ...
20     } else {
21         continue;
22     }
23     println!("Hello, world");
24 }
25 ```
26
27 Could be rewritten as
28
29 ```
30 while condition() {
31     update_condition();
32     if x {
33         // ...
34         println!("Hello, world");
35     }
36 }
37 ```
38
39 As another example, the following code
40
41 ```
42 loop {
43     if waiting() {
44         continue;
45     } else {
46         // Do something useful
47     }
48     # break;
49 }
50 ```
51 Could be rewritten as
52
53 ```
54 loop {
55     if waiting() {
56         continue;
57     }
58     // Do something useful
59     # break;
60 }
61 ```