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