Skip to content

Commit

Permalink
Fix missing_const_for_fn false positive
Browse files Browse the repository at this point in the history
We don't want to lint if the type of the method implements drop.

(constant functions cannot evaluate destructors)
  • Loading branch information
phansch committed Aug 25, 2019
1 parent 05f603e commit 8d4dd77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion clippy_lints/src/missing_const_for_fn.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::utils::{is_entrypoint_fn, span_lint, trait_ref_of_method};
use crate::utils::{has_drop, is_entrypoint_fn, is_self_ty, span_lint, trait_ref_of_method};
use rustc::hir;
use rustc::hir::intravisit::FnKind;
use rustc::hir::{Body, Constness, FnDecl, HirId};
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! {
Expand Down Expand Up @@ -94,6 +95,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
}
},
FnKind::Method(_, sig, ..) => {
// Don't lint if `Self` implements drop
let input_tys = &sig.decl.inputs;
if input_tys.len() > 0 {
let hir_ty = &input_tys[0];
let ty_ty = hir_ty_to_ty(cx.tcx, hir_ty);
if is_self_ty(hir_ty) && has_drop(cx, ty_ty) {
return;
}
}
if trait_ref_of_method(cx, hir_id).is_some() || already_const(sig.header) {
return;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/missing_const_for_fn/cant_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ impl std::ops::Add for Point {
Point(self.0 + other.0, self.1 + other.1)
}
}

pub struct A;
impl A {
// This should not be const because the type implements `Drop`.
pub fn a(self) -> B {
B
}
}
impl Drop for A {
fn drop(&mut self) {}
}

pub struct B;

0 comments on commit 8d4dd77

Please sign in to comment.