]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/missing_const_for_fn.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / missing_const_for_fn.rs
index 19757f32e6b49534e64b3f3c8ba5d3163019c279..2f20aa9c683c4e166c15d3fbfe77b8467d818891 100644 (file)
@@ -1,11 +1,11 @@
-use crate::utils::{is_entrypoint_fn, span_lint};
-use if_chain::if_chain;
+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_tool_lint, lint_array};
+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! {
     /// **Example:**
     ///
     /// ```rust
+    /// # struct Foo {
+    /// #     random_number: usize,
+    /// # }
+    /// # impl Foo {
     /// fn new() -> Self {
     ///     Self { random_number: 42 }
     /// }
+    /// # }
     /// ```
     ///
     /// Could be a const fn:
     ///
     /// ```rust
+    /// # struct Foo {
+    /// #     random_number: usize,
+    /// # }
+    /// # impl Foo {
     /// const fn new() -> Self {
     ///     Self { random_number: 42 }
     /// }
+    /// # }
     /// ```
     pub MISSING_CONST_FOR_FN,
     nursery,
     "Lint functions definitions that could be made `const fn`"
 }
 
-#[derive(Clone)]
-pub struct MissingConstForFn;
-
-impl LintPass for MissingConstForFn {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(MISSING_CONST_FOR_FN)
-    }
-
-    fn name(&self) -> &'static str {
-        "MissingConstForFn"
-    }
-}
+declare_lint_pass!(MissingConstForFn => [MISSING_CONST_FOR_FN]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
     fn check_fn(
@@ -81,7 +80,7 @@ fn check_fn(
         span: Span,
         hir_id: HirId,
     ) {
-        let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
+        let def_id = cx.tcx.hir().local_def_id(hir_id);
 
         if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) {
             return;
@@ -96,7 +95,10 @@ fn check_fn(
                 }
             },
             FnKind::Method(_, sig, ..) => {
-                if is_trait_method(cx, hir_id) || 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;
                 }
             },
@@ -115,16 +117,14 @@ fn check_fn(
     }
 }
 
-fn is_trait_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> bool {
-    // Get the implemented trait for the current function
-    let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
-    if_chain! {
-        if parent_impl != hir::CRATE_HIR_ID;
-        if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
-        if let hir::ItemKind::Impl(_, _, _, _, Some(_trait_ref), _, _) = &item.node;
-        then { return true; }
-    }
-    false
+/// 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`