1 use crate::utils::qualify_min_const_fn::is_min_const_fn;
3 fn_has_unsatisfiable_preds, has_drop, is_entrypoint_fn, meets_msrv, span_lint, trait_ref_of_method,
6 use rustc_hir::intravisit::FnKind;
7 use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
8 use rustc_lint::{LateContext, LateLintPass, LintContext};
9 use rustc_middle::lint::in_external_macro;
10 use rustc_semver::RustcVersion;
11 use rustc_session::{declare_tool_lint, impl_lint_pass};
13 use rustc_typeck::hir_ty_to_ty;
15 const MISSING_CONST_FOR_FN_MSRV: RustcVersion = RustcVersion::new(1, 37, 0);
17 declare_clippy_lint! {
20 /// Suggests the use of `const` in functions and methods where possible.
22 /// **Why is this bad?**
24 /// Not having the function const prevents callers of the function from being const as well.
26 /// **Known problems:**
28 /// Const functions are currently still being worked on, with some features only being available
29 /// on nightly. This lint does not consider all edge cases currently and the suggestions may be
30 /// incorrect if you are using this lint on stable.
32 /// Also, the lint only runs one pass over the code. Consider these two non-const functions:
43 /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
44 /// can't be const as it calls a non-const function. Making `a` const and running Clippy again,
45 /// will suggest to make `b` const, too.
51 /// # random_number: usize,
54 /// fn new() -> Self {
55 /// Self { random_number: 42 }
60 /// Could be a const fn:
64 /// # random_number: usize,
67 /// const fn new() -> Self {
68 /// Self { random_number: 42 }
72 pub MISSING_CONST_FOR_FN,
74 "Lint functions definitions that could be made `const fn`"
77 impl_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]);
79 pub struct MissingConstForFn {
80 msrv: Option<RustcVersion>,
83 impl MissingConstForFn {
85 pub fn new(msrv: Option<RustcVersion>) -> Self {
90 impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
100 if !meets_msrv(self.msrv.as_ref(), &MISSING_CONST_FOR_FN_MSRV) {
104 let def_id = cx.tcx.hir().local_def_id(hir_id);
106 if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
110 // Building MIR for `fn`s with unsatisfiable preds results in ICE.
111 if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
115 // Perform some preliminary checks that rule out constness on the Clippy side. This way we
116 // can skip the actual const check and return early.
118 FnKind::ItemFn(_, generics, header, ..) => {
119 let has_const_generic_params = generics
122 .any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
124 if already_const(header) || has_const_generic_params {
128 FnKind::Method(_, sig, ..) => {
129 if trait_ref_of_method(cx, hir_id).is_some()
130 || already_const(sig.header)
131 || method_accepts_dropable(cx, sig.decl.inputs)
136 FnKind::Closure => return,
139 let mir = cx.tcx.optimized_mir(def_id);
141 if let Err((span, err)) = is_min_const_fn(cx.tcx, &mir) {
142 if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id.to_def_id()) {
143 cx.tcx.sess.span_err(span, &err);
146 span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");
149 extract_msrv_attr!(LateContext);
152 /// Returns true if any of the method parameters is a type that implements `Drop`. The method
153 /// can't be made const then, because `drop` can't be const-evaluated.
154 fn method_accepts_dropable(cx: &LateContext<'_>, param_tys: &[hir::Ty<'_>]) -> bool {
155 // If any of the params are droppable, return true
156 param_tys.iter().any(|hir_ty| {
157 let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty);
162 // We don't have to lint on something that's already `const`
164 fn already_const(header: hir::FnHeader) -> bool {
165 header.constness == Constness::Const