1 use crate::utils::qualify_min_const_fn::is_min_const_fn;
2 use crate::utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, meets_msrv, span_lint, trait_ref_of_method};
3 use clippy_utils::ty::has_drop;
5 use rustc_hir::intravisit::FnKind;
6 use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId};
7 use rustc_lint::{LateContext, LateLintPass, LintContext};
8 use rustc_middle::lint::in_external_macro;
9 use rustc_semver::RustcVersion;
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
12 use rustc_typeck::hir_ty_to_ty;
14 const MISSING_CONST_FOR_FN_MSRV: RustcVersion = RustcVersion::new(1, 37, 0);
16 declare_clippy_lint! {
19 /// Suggests the use of `const` in functions and methods where possible.
21 /// **Why is this bad?**
23 /// Not having the function const prevents callers of the function from being const as well.
25 /// **Known problems:**
27 /// Const functions are currently still being worked on, with some features only being available
28 /// on nightly. This lint does not consider all edge cases currently and the suggestions may be
29 /// incorrect if you are using this lint on stable.
31 /// Also, the lint only runs one pass over the code. Consider these two non-const functions:
42 /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
43 /// can't be const as it calls a non-const function. Making `a` const and running Clippy again,
44 /// will suggest to make `b` const, too.
50 /// # random_number: usize,
53 /// fn new() -> Self {
54 /// Self { random_number: 42 }
59 /// Could be a const fn:
63 /// # random_number: usize,
66 /// const fn new() -> Self {
67 /// Self { random_number: 42 }
71 pub MISSING_CONST_FOR_FN,
73 "Lint functions definitions that could be made `const fn`"
76 impl_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]);
78 pub struct MissingConstForFn {
79 msrv: Option<RustcVersion>,
82 impl MissingConstForFn {
84 pub fn new(msrv: Option<RustcVersion>) -> Self {
89 impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
99 if !meets_msrv(self.msrv.as_ref(), &MISSING_CONST_FOR_FN_MSRV) {
103 let def_id = cx.tcx.hir().local_def_id(hir_id);
105 if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
109 // Building MIR for `fn`s with unsatisfiable preds results in ICE.
110 if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
114 // Perform some preliminary checks that rule out constness on the Clippy side. This way we
115 // can skip the actual const check and return early.
117 FnKind::ItemFn(_, generics, header, ..) => {
118 let has_const_generic_params = generics
121 .any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
123 if already_const(header) || has_const_generic_params {
127 FnKind::Method(_, sig, ..) => {
128 if trait_ref_of_method(cx, hir_id).is_some()
129 || already_const(sig.header)
130 || method_accepts_dropable(cx, sig.decl.inputs)
135 FnKind::Closure => return,
138 let mir = cx.tcx.optimized_mir(def_id);
140 if let Err((span, err)) = is_min_const_fn(cx.tcx, &mir) {
141 if rustc_mir::const_eval::is_min_const_fn(cx.tcx, def_id.to_def_id()) {
142 cx.tcx.sess.span_err(span, &err);
145 span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");
148 extract_msrv_attr!(LateContext);
151 /// Returns true if any of the method parameters is a type that implements `Drop`. The method
152 /// can't be made const then, because `drop` can't be const-evaluated.
153 fn method_accepts_dropable(cx: &LateContext<'_>, param_tys: &[hir::Ty<'_>]) -> bool {
154 // If any of the params are droppable, return true
155 param_tys.iter().any(|hir_ty| {
156 let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty);
161 // We don't have to lint on something that's already `const`
163 fn already_const(header: hir::FnHeader) -> bool {
164 header.constness == Constness::Const