]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/missing_const_for_fn.rs
Fix missing_const_for_fn false positive
[rust.git] / clippy_lints / src / missing_const_for_fn.rs
index 89398f82c9e5c702da57309eea22e2a176c2f478..2f20aa9c683c4e166c15d3fbfe77b8467d818891 100644 (file)
@@ -1,10 +1,11 @@
-use crate::utils::{is_entrypoint_fn, span_lint, trait_ref_of_method};
+use crate::utils::{has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method};
 use rustc::hir;
 use rustc::hir::intravisit::FnKind;
-use rustc::hir::{Body, Constness, FnDecl, HirId};
+use rustc::hir::{Body, Constness, FnDecl, HirId, HirVec};
 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass};
 use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn;
+use rustc_typeck::hir_ty_to_ty;
 use syntax_pos::Span;
 
 declare_clippy_lint! {
@@ -94,7 +95,10 @@ fn check_fn(
                 }
             },
             FnKind::Method(_, sig, ..) => {
-                if trait_ref_of_method(cx, hir_id).is_some() || already_const(sig.header) {
+                if trait_ref_of_method(cx, hir_id).is_some()
+                    || already_const(sig.header)
+                    || method_accepts_dropable(cx, &sig.decl.inputs)
+                {
                     return;
                 }
             },
@@ -113,6 +117,16 @@ fn check_fn(
     }
 }
 
+/// Returns true if any of the method parameters is a type that implements `Drop`. The method
+/// can't be made const then, because `drop` can't be const-evaluated.
+fn method_accepts_dropable(cx: &LateContext<'_, '_>, param_tys: &HirVec<hir::Ty>) -> bool {
+    // If any of the params are dropable, return true
+    param_tys.iter().any(|hir_ty| {
+        let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty);
+        has_drop(cx, ty_ty)
+    })
+}
+
 // We don't have to lint on something that's already `const`
 fn already_const(header: hir::FnHeader) -> bool {
     header.constness == Constness::Const