1 use clippy_utils
::diagnostics
::span_lint
;
2 use clippy_utils
::msrvs
::{self, Msrv}
;
3 use clippy_utils
::qualify_min_const_fn
::is_min_const_fn
;
4 use clippy_utils
::ty
::has_drop
;
5 use clippy_utils
::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method}
;
7 use rustc_hir
::def_id
::CRATE_DEF_ID
;
8 use rustc_hir
::intravisit
::FnKind
;
9 use rustc_hir
::{Body, Constness, FnDecl, GenericParamKind, HirId}
;
10 use rustc_hir_analysis
::hir_ty_to_ty
;
11 use rustc_lint
::{LateContext, LateLintPass}
;
12 use rustc_middle
::lint
::in_external_macro
;
13 use rustc_session
::{declare_tool_lint, impl_lint_pass}
;
16 declare_clippy_lint
! {
18 /// Suggests the use of `const` in functions and methods where possible.
20 /// ### Why is this bad?
21 /// Not having the function const prevents callers of the function from being const as well.
23 /// ### Known problems
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.
28 /// Also, the lint only runs one pass over the code. Consider these two non-const functions:
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.
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 #[clippy::version = "1.34.0"]
68 pub MISSING_CONST_FOR_FN
,
70 "Lint functions definitions that could be made `const fn`"
73 impl_lint_pass
!(MissingConstForFn
=> [MISSING_CONST_FOR_FN
]);
75 pub struct MissingConstForFn
{
79 impl MissingConstForFn
{
81 pub fn new(msrv
: Msrv
) -> Self {
86 impl<'tcx
> LateLintPass
<'tcx
> for MissingConstForFn
{
89 cx
: &LateContext
<'tcx
>,
96 if !self.msrv
.meets(msrvs
::CONST_IF_MATCH
) {
100 let def_id
= cx
.tcx
.hir().local_def_id(hir_id
);
102 if in_external_macro(cx
.tcx
.sess
, span
) || is_entrypoint_fn(cx
, def_id
.to_def_id()) {
106 // Building MIR for `fn`s with unsatisfiable preds results in ICE.
107 if fn_has_unsatisfiable_preds(cx
, def_id
.to_def_id()) {
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.
114 FnKind
::ItemFn(_
, generics
, header
, ..) => {
115 let has_const_generic_params
= generics
118 .any(|param
| matches
!(param
.kind
, GenericParamKind
::Const { .. }
));
120 if already_const(header
) || has_const_generic_params
{
124 FnKind
::Method(_
, sig
, ..) => {
125 if trait_ref_of_method(cx
, def_id
).is_some()
126 || already_const(sig
.header
)
127 || method_accepts_droppable(cx
, sig
.decl
.inputs
)
132 FnKind
::Closure
=> return,
135 // Const fns are not allowed as methods in a trait.
137 let parent
= cx
.tcx
.hir().get_parent_item(hir_id
).def_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
{
147 if is_from_proc_macro(cx
, &(&kind
, body
, hir_id
, span
)) {
151 let mir
= cx
.tcx
.optimized_mir(def_id
);
153 if let Err((span
, err
)) = is_min_const_fn(cx
.tcx
, mir
, &self.msrv
) {
154 if cx
.tcx
.is_const_fn_raw(def_id
.to_def_id()) {
155 cx
.tcx
.sess
.span_err(span
, err
.as_ref());
158 span_lint(cx
, MISSING_CONST_FOR_FN
, span
, "this could be a `const fn`");
161 extract_msrv_attr
!(LateContext
);
164 /// Returns true if any of the method parameters is a type that implements `Drop`. The method
165 /// can't be made const then, because `drop` can't be const-evaluated.
166 fn method_accepts_droppable(cx
: &LateContext
<'_
>, param_tys
: &[hir
::Ty
<'_
>]) -> bool
{
167 // If any of the params are droppable, return true
168 param_tys
.iter().any(|hir_ty
| {
169 let ty_ty
= hir_ty_to_ty(cx
.tcx
, hir_ty
);
174 // We don't have to lint on something that's already `const`
176 fn already_const(header
: hir
::FnHeader
) -> bool
{
177 header
.constness
== Constness
::Const