]> git.lizzy.rs Git - rust.git/blob - src/docs/missing_const_for_fn.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / missing_const_for_fn.txt
1 ### What it does
2 Suggests the use of `const` in functions and methods where possible.
3
4 ### Why is this bad?
5 Not having the function const prevents callers of the function from being const as well.
6
7 ### Known problems
8 Const functions are currently still being worked on, with some features only being available
9 on nightly. This lint does not consider all edge cases currently and the suggestions may be
10 incorrect if you are using this lint on stable.
11
12 Also, the lint only runs one pass over the code. Consider these two non-const functions:
13
14 ```
15 fn a() -> i32 {
16     0
17 }
18 fn b() -> i32 {
19     a()
20 }
21 ```
22
23 When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
24 can't be const as it calls a non-const function. Making `a` const and running Clippy again,
25 will suggest to make `b` const, too.
26
27 ### Example
28 ```
29 fn new() -> Self {
30     Self { random_number: 42 }
31 }
32 ```
33
34 Could be a const fn:
35
36 ```
37 const fn new() -> Self {
38     Self { random_number: 42 }
39 }
40 ```