]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/bool_to_int_with_if.txt
:arrow_up: rust-analyzer
[rust.git] / src / tools / clippy / src / docs / bool_to_int_with_if.txt
1 ### What it does
2 Instead of using an if statement to convert a bool to an int,
3 this lint suggests using a `from()` function or an `as` coercion.
4
5 ### Why is this bad?
6 Coercion or `from()` is idiomatic way to convert bool to a number.
7 Both methods are guaranteed to return 1 for true, and 0 for false.
8
9 See https://doc.rust-lang.org/std/primitive.bool.html#impl-From%3Cbool%3E
10
11 ### Example
12 ```
13 if condition {
14     1_i64
15 } else {
16     0
17 };
18 ```
19 Use instead:
20 ```
21 i64::from(condition);
22 ```
23 or
24 ```
25 condition as i64;
26 ```