]> git.lizzy.rs Git - rust.git/blob - src/docs/branches_sharing_code.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / branches_sharing_code.txt
1 ### What it does
2 Checks if the `if` and `else` block contain shared code that can be
3 moved out of the blocks.
4
5 ### Why is this bad?
6 Duplicate code is less maintainable.
7
8 ### Known problems
9 * The lint doesn't check if the moved expressions modify values that are being used in
10   the if condition. The suggestion can in that case modify the behavior of the program.
11   See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452)
12
13 ### Example
14 ```
15 let foo = if … {
16     println!("Hello World");
17     13
18 } else {
19     println!("Hello World");
20     42
21 };
22 ```
23
24 Use instead:
25 ```
26 println!("Hello World");
27 let foo = if … {
28     13
29 } else {
30     42
31 };
32 ```