summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/branches_sharing_code.txt
blob: 79be6124798a254ec8db803b30ea7ba4e9676168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
### What it does
Checks if the `if` and `else` block contain shared code that can be
moved out of the blocks.

### Why is this bad?
Duplicate code is less maintainable.

### Known problems
* The lint doesn't check if the moved expressions modify values that are being used in
  the if condition. The suggestion can in that case modify the behavior of the program.
  See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452)

### Example
```
let foo = if … {
    println!("Hello World");
    13
} else {
    println!("Hello World");
    42
};
```

Use instead:
```
println!("Hello World");
let foo = if … {
    13
} else {
    42
};
```