]> git.lizzy.rs Git - rust.git/blob - src/docs/same_functions_in_if_condition.txt
Add iter_kv_map lint
[rust.git] / src / docs / same_functions_in_if_condition.txt
1 ### What it does
2 Checks for consecutive `if`s with the same function call.
3
4 ### Why is this bad?
5 This is probably a copy & paste error.
6 Despite the fact that function can have side effects and `if` works as
7 intended, such an approach is implicit and can be considered a "code smell".
8
9 ### Example
10 ```
11 if foo() == bar {
12     …
13 } else if foo() == bar {
14     …
15 }
16 ```
17
18 This probably should be:
19 ```
20 if foo() == bar {
21     …
22 } else if foo() == baz {
23     …
24 }
25 ```
26
27 or if the original code was not a typo and called function mutates a state,
28 consider move the mutation out of the `if` condition to avoid similarity to
29 a copy & paste error:
30
31 ```
32 let first = foo();
33 if first == bar {
34     …
35 } else {
36     let second = foo();
37     if second == bar {
38     …
39     }
40 }
41 ```