1 use crate::utils::{has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method};
2 use rustc::declare_lint_pass;
4 use rustc::hir::intravisit::FnKind;
5 use rustc::hir::{Body, Constness, FnDecl, HirId};
6 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
7 use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
8 use rustc_session::declare_tool_lint;
9 use rustc_typeck::hir_ty_to_ty;
12 declare_clippy_lint! {
15 /// Suggests the use of `const` in functions and methods where possible.
17 /// **Why is this bad?**
19 /// Not having the function const prevents callers of the function from being const as well.
21 /// **Known problems:**
23 /// Const functions are currently still being worked on, with some features only being available
24 /// on nightly. This lint does not consider all edge cases currently and the suggestions may be
25 /// incorrect if you are using this lint on stable.
27 /// Also, the lint only runs one pass over the code. Consider these two non-const functions:
38 /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time
39 /// can't be const as it calls a non-const function. Making `a` const and running Clippy again,
40 /// will suggest to make `b` const, too.
46 /// # random_number: usize,
49 /// fn new() -> Self {
50 /// Self { random_number: 42 }
55 /// Could be a const fn:
59 /// # random_number: usize,
62 /// const fn new() -> Self {
63 /// Self { random_number: 42 }
67 pub MISSING_CONST_FOR_FN,
69 "Lint functions definitions that could be made `const fn`"
72 declare_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]);
74 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
77 cx: &LateContext<'_, '_>,
84 let def_id = cx.tcx.hir().local_def_id(hir_id);
86 if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) {
90 // Perform some preliminary checks that rule out constness on the Clippy side. This way we
91 // can skip the actual const check and return early.
93 FnKind::ItemFn(_, _, header, ..) => {
94 if already_const(header) {
98 FnKind::Method(_, sig, ..) => {
99 if trait_ref_of_method(cx, hir_id).is_some()
100 || already_const(sig.header)
101 || method_accepts_dropable(cx, sig.decl.inputs)
109 let mir = cx.tcx.optimized_mir(def_id);
111 if let Err((span, err)) = is_min_const_fn(cx.tcx, def_id, &mir) {
112 if cx.tcx.is_min_const_fn(def_id) {
113 cx.tcx.sess.span_err(span, &err);
116 span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a const_fn");
121 /// Returns true if any of the method parameters is a type that implements `Drop`. The method
122 /// can't be made const then, because `drop` can't be const-evaluated.
123 fn method_accepts_dropable(cx: &LateContext<'_, '_>, param_tys: &[hir::Ty<'_>]) -> bool {
124 // If any of the params are dropable, return true
125 param_tys.iter().any(|hir_ty| {
126 let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty);
131 // We don't have to lint on something that's already `const`
133 fn already_const(header: hir::FnHeader) -> bool {
134 header.constness == Constness::Const