]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_const_for_fn.rs
partially HirIdify lints
[rust.git] / clippy_lints / src / missing_const_for_fn.rs
1 use crate::utils::{is_entrypoint_fn, span_lint};
2 use rustc::hir;
3 use rustc::hir::intravisit::FnKind;
4 use rustc::hir::{Body, Constness, FnDecl, HirId};
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
7 use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
8 use syntax_pos::Span;
9
10 /// **What it does:**
11 ///
12 /// Suggests the use of `const` in functions and methods where possible.
13 ///
14 /// **Why is this bad?**
15 ///
16 /// Not having the function const prevents callers of the function from being const as well.
17 ///
18 /// **Known problems:**
19 ///
20 /// Const functions are currently still being worked on, with some features only being available
21 /// on nightly. This lint does not consider all edge cases currently and the suggestions may be
22 /// incorrect if you are using this lint on stable.
23 ///
24 /// Also, the lint only runs one pass over the code. Consider these two non-const functions:
25 ///
26 /// ```rust
27 /// fn a() -> i32 {
28 ///     0
29 /// }
30 /// fn b() -> i32 {
31 ///     a()
32 /// }
33 /// ```
34 ///
35 /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
36 /// can't be const as it calls a non-const function. Making `a` const and running Clippy again,
37 /// will suggest to make `b` const, too.
38 ///
39 /// **Example:**
40 ///
41 /// ```rust
42 /// fn new() -> Self {
43 ///     Self { random_number: 42 }
44 /// }
45 /// ```
46 ///
47 /// Could be a const fn:
48 ///
49 /// ```rust
50 /// const fn new() -> Self {
51 ///     Self { random_number: 42 }
52 /// }
53 /// ```
54 declare_clippy_lint! {
55     pub MISSING_CONST_FOR_FN,
56     nursery,
57     "Lint functions definitions that could be made `const fn`"
58 }
59
60 #[derive(Clone)]
61 pub struct MissingConstForFn;
62
63 impl LintPass for MissingConstForFn {
64     fn get_lints(&self) -> LintArray {
65         lint_array!(MISSING_CONST_FOR_FN)
66     }
67
68     fn name(&self) -> &'static str {
69         "MissingConstForFn"
70     }
71 }
72
73 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
74     fn check_fn(
75         &mut self,
76         cx: &LateContext<'_, '_>,
77         kind: FnKind<'_>,
78         _: &FnDecl,
79         _: &Body,
80         span: Span,
81         hir_id: HirId,
82     ) {
83         let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
84
85         if is_entrypoint_fn(cx, def_id) {
86             return;
87         }
88
89         // Perform some preliminary checks that rule out constness on the Clippy side. This way we
90         // can skip the actual const check and return early.
91         match kind {
92             FnKind::ItemFn(_, _, header, ..) => {
93                 if already_const(header) {
94                     return;
95                 }
96             },
97             FnKind::Method(_, sig, ..) => {
98                 if already_const(sig.header) {
99                     return;
100                 }
101             },
102             _ => return,
103         }
104
105         let mir = cx.tcx.optimized_mir(def_id);
106
107         if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
108             if cx.tcx.is_min_const_fn(def_id) {
109                 cx.tcx.sess.span_err(span, &err);
110             }
111         } else {
112             span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a const_fn");
113         }
114     }
115 }
116
117 // We don't have to lint on something that's already `const`
118 fn already_const(header: hir::FnHeader) -> bool {
119     header.constness == Constness::Const
120 }