]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use clippy_utils::diagnostics::span_lint; |
2 | use clippy_utils::qualify_min_const_fn::is_min_const_fn; | |
3 | use clippy_utils::ty::has_drop; | |
4 | use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, msrvs, trait_ref_of_method}; | |
f20569fa | 5 | use rustc_hir as hir; |
5e7ed085 | 6 | use rustc_hir::def_id::CRATE_DEF_ID; |
f20569fa XL |
7 | use rustc_hir::intravisit::FnKind; |
8 | use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId}; | |
5099ac24 | 9 | use rustc_lint::{LateContext, LateLintPass}; |
f20569fa XL |
10 | use rustc_middle::lint::in_external_macro; |
11 | use rustc_semver::RustcVersion; | |
12 | use rustc_session::{declare_tool_lint, impl_lint_pass}; | |
13 | use rustc_span::Span; | |
14 | use rustc_typeck::hir_ty_to_ty; | |
15 | ||
f20569fa | 16 | declare_clippy_lint! { |
94222f64 | 17 | /// ### What it does |
f20569fa XL |
18 | /// Suggests the use of `const` in functions and methods where possible. |
19 | /// | |
94222f64 | 20 | /// ### Why is this bad? |
f20569fa XL |
21 | /// Not having the function const prevents callers of the function from being const as well. |
22 | /// | |
94222f64 | 23 | /// ### Known problems |
f20569fa XL |
24 | /// Const functions are currently still being worked on, with some features only being available |
25 | /// on nightly. This lint does not consider all edge cases currently and the suggestions may be | |
26 | /// incorrect if you are using this lint on stable. | |
27 | /// | |
28 | /// Also, the lint only runs one pass over the code. Consider these two non-const functions: | |
29 | /// | |
30 | /// ```rust | |
31 | /// fn a() -> i32 { | |
32 | /// 0 | |
33 | /// } | |
34 | /// fn b() -> i32 { | |
35 | /// a() | |
36 | /// } | |
37 | /// ``` | |
38 | /// | |
39 | /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time | |
40 | /// can't be const as it calls a non-const function. Making `a` const and running Clippy again, | |
41 | /// will suggest to make `b` const, too. | |
42 | /// | |
94222f64 | 43 | /// ### Example |
f20569fa XL |
44 | /// ```rust |
45 | /// # struct Foo { | |
46 | /// # random_number: usize, | |
47 | /// # } | |
48 | /// # impl Foo { | |
49 | /// fn new() -> Self { | |
50 | /// Self { random_number: 42 } | |
51 | /// } | |
52 | /// # } | |
53 | /// ``` | |
54 | /// | |
55 | /// Could be a const fn: | |
56 | /// | |
57 | /// ```rust | |
58 | /// # struct Foo { | |
59 | /// # random_number: usize, | |
60 | /// # } | |
61 | /// # impl Foo { | |
62 | /// const fn new() -> Self { | |
63 | /// Self { random_number: 42 } | |
64 | /// } | |
65 | /// # } | |
66 | /// ``` | |
a2a8927a | 67 | #[clippy::version = "1.34.0"] |
f20569fa XL |
68 | pub MISSING_CONST_FOR_FN, |
69 | nursery, | |
70 | "Lint functions definitions that could be made `const fn`" | |
71 | } | |
72 | ||
73 | impl_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]); | |
74 | ||
75 | pub struct MissingConstForFn { | |
76 | msrv: Option<RustcVersion>, | |
77 | } | |
78 | ||
79 | impl MissingConstForFn { | |
80 | #[must_use] | |
81 | pub fn new(msrv: Option<RustcVersion>) -> Self { | |
82 | Self { msrv } | |
83 | } | |
84 | } | |
85 | ||
86 | impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { | |
87 | fn check_fn( | |
88 | &mut self, | |
89 | cx: &LateContext<'_>, | |
90 | kind: FnKind<'_>, | |
91 | _: &FnDecl<'_>, | |
92 | _: &Body<'_>, | |
93 | span: Span, | |
94 | hir_id: HirId, | |
95 | ) { | |
cdc7bbd5 | 96 | if !meets_msrv(self.msrv.as_ref(), &msrvs::CONST_IF_MATCH) { |
f20569fa XL |
97 | return; |
98 | } | |
99 | ||
100 | let def_id = cx.tcx.hir().local_def_id(hir_id); | |
101 | ||
102 | if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) { | |
103 | return; | |
104 | } | |
105 | ||
106 | // Building MIR for `fn`s with unsatisfiable preds results in ICE. | |
107 | if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) { | |
108 | return; | |
109 | } | |
110 | ||
111 | // Perform some preliminary checks that rule out constness on the Clippy side. This way we | |
112 | // can skip the actual const check and return early. | |
113 | match kind { | |
114 | FnKind::ItemFn(_, generics, header, ..) => { | |
115 | let has_const_generic_params = generics | |
116 | .params | |
117 | .iter() | |
118 | .any(|param| matches!(param.kind, GenericParamKind::Const { .. })); | |
119 | ||
120 | if already_const(header) || has_const_generic_params { | |
121 | return; | |
122 | } | |
123 | }, | |
124 | FnKind::Method(_, sig, ..) => { | |
5099ac24 | 125 | if trait_ref_of_method(cx, def_id).is_some() |
f20569fa XL |
126 | || already_const(sig.header) |
127 | || method_accepts_dropable(cx, sig.decl.inputs) | |
128 | { | |
129 | return; | |
130 | } | |
131 | }, | |
132 | FnKind::Closure => return, | |
5e7ed085 FG |
133 | } |
134 | ||
135 | // Const fns are not allowed as methods in a trait. | |
136 | { | |
137 | let parent = cx.tcx.hir().get_parent_item(hir_id); | |
138 | if parent != CRATE_DEF_ID { | |
139 | if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent) { | |
140 | if let hir::ItemKind::Trait(..) = &item.kind { | |
141 | return; | |
142 | } | |
143 | } | |
144 | } | |
f20569fa XL |
145 | } |
146 | ||
147 | let mir = cx.tcx.optimized_mir(def_id); | |
148 | ||
cdc7bbd5 XL |
149 | if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, self.msrv.as_ref()) { |
150 | if cx.tcx.is_const_fn_raw(def_id.to_def_id()) { | |
f20569fa XL |
151 | cx.tcx.sess.span_err(span, &err); |
152 | } | |
153 | } else { | |
154 | span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`"); | |
155 | } | |
156 | } | |
157 | extract_msrv_attr!(LateContext); | |
158 | } | |
159 | ||
160 | /// Returns true if any of the method parameters is a type that implements `Drop`. The method | |
161 | /// can't be made const then, because `drop` can't be const-evaluated. | |
162 | fn method_accepts_dropable(cx: &LateContext<'_>, param_tys: &[hir::Ty<'_>]) -> bool { | |
163 | // If any of the params are droppable, return true | |
164 | param_tys.iter().any(|hir_ty| { | |
165 | let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty); | |
166 | has_drop(cx, ty_ty) | |
167 | }) | |
168 | } | |
169 | ||
170 | // We don't have to lint on something that's already `const` | |
171 | #[must_use] | |
172 | fn already_const(header: hir::FnHeader) -> bool { | |
173 | header.constness == Constness::Const | |
174 | } |