Skip to content

Commit b3dd25d

Browse files
committed
Fix false positive in missing_const_for_fn
This fixes some additional false positives with functions or methods that return types which implement drop. Since `drop` can't be const-evaluated, these cases can't be made const. Fixes #4979
1 parent 99dd0bb commit b3dd25d

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

clippy_lints/src/missing_const_for_fn.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method};
1+
use crate::utils::{has_drop, is_entrypoint_fn, span_lint, trait_ref_of_method, return_ty};
22
use rustc::declare_lint_pass;
33
use rustc::hir;
44
use rustc::hir::intravisit::FnKind;
@@ -91,14 +91,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
9191
// can skip the actual const check and return early.
9292
match kind {
9393
FnKind::ItemFn(_, _, header, ..) => {
94-
if already_const(header) {
94+
if already_const(header)
95+
|| returns_dropable(cx, hir_id)
96+
{
9597
return;
9698
}
9799
},
98100
FnKind::Method(_, sig, ..) => {
99101
if trait_ref_of_method(cx, hir_id).is_some()
100102
|| already_const(sig.header)
101103
|| method_accepts_dropable(cx, sig.decl.inputs)
104+
|| returns_dropable(cx, hir_id)
102105
{
103106
return;
104107
}
@@ -128,6 +131,11 @@ fn method_accepts_dropable(cx: &LateContext<'_, '_>, param_tys: &[hir::Ty<'_>])
128131
})
129132
}
130133

134+
fn returns_dropable(cx: &LateContext<'_, '_>, ret_ty: hir::HirId) -> bool {
135+
let return_ty = return_ty(cx, ret_ty);
136+
has_drop(cx, return_ty)
137+
}
138+
131139
// We don't have to lint on something that's already `const`
132140
#[must_use]
133141
fn already_const(header: hir::FnHeader) -> bool {

tests/ui/missing_const_for_fn/cant_be_const.rs

+23
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,27 @@ mod with_drop {
8989
B
9090
}
9191
}
92+
93+
struct CustomDrop {
94+
field: i32
95+
}
96+
97+
impl Drop for CustomDrop {
98+
fn drop(&mut self) { }
99+
}
100+
101+
struct Bar {
102+
field: CustomDrop
103+
}
104+
105+
impl Bar {
106+
fn take(self) -> CustomDrop {
107+
self.field
108+
}
109+
}
110+
111+
fn take() -> CustomDrop {
112+
let x = Bar { field: CustomDrop { field: 1 } };
113+
x.field
114+
}
92115
}

0 commit comments

Comments
 (0)