Skip to content

Commit 0129b93

Browse files
committed
let qualify_min_const_fn deal with drop checks
1 parent f5e2501 commit 0129b93

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint;
33
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
4-
use clippy_utils::ty::has_drop;
54
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method};
65
use rustc_hir as hir;
76
use rustc_hir::def_id::CRATE_DEF_ID;
@@ -121,10 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
121120
}
122121
},
123122
FnKind::Method(_, sig, ..) => {
124-
if trait_ref_of_method(cx, def_id).is_some()
125-
|| already_const(sig.header)
126-
|| method_accepts_droppable(cx, def_id)
127-
{
123+
if trait_ref_of_method(cx, def_id).is_some() || already_const(sig.header) {
128124
return;
129125
}
130126
},
@@ -162,15 +158,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
162158
extract_msrv_attr!(LateContext);
163159
}
164160

165-
/// Returns true if any of the method parameters is a type that implements `Drop`. The method
166-
/// can't be made const then, because `drop` can't be const-evaluated.
167-
fn method_accepts_droppable(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
168-
let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
169-
170-
// If any of the params are droppable, return true
171-
sig.inputs().iter().any(|&ty| has_drop(cx, ty))
172-
}
173-
174161
// We don't have to lint on something that's already `const`
175162
#[must_use]
176163
fn already_const(header: hir::FnHeader) -> bool {

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,21 @@ impl const Drop for D {
113113
// Lint this, since it can be dropped in const contexts
114114
// FIXME(effects)
115115
fn d(this: D) {}
116+
117+
mod issue12677 {
118+
pub struct Wrapper {
119+
pub strings: Vec<String>,
120+
}
121+
122+
impl Wrapper {
123+
#[must_use]
124+
pub fn new(strings: Vec<String>) -> Self {
125+
Self { strings }
126+
}
127+
128+
#[must_use]
129+
pub fn empty() -> Self {
130+
Self { strings: Vec::new() }
131+
}
132+
}
133+
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,21 @@ LL | | 46
102102
LL | | }
103103
| |_^
104104

105-
error: aborting due to 11 previous errors
105+
error: this could be a `const fn`
106+
--> tests/ui/missing_const_for_fn/could_be_const.rs:124:9
107+
|
108+
LL | / pub fn new(strings: Vec<String>) -> Self {
109+
LL | | Self { strings }
110+
LL | | }
111+
| |_________^
112+
113+
error: this could be a `const fn`
114+
--> tests/ui/missing_const_for_fn/could_be_const.rs:129:9
115+
|
116+
LL | / pub fn empty() -> Self {
117+
LL | | Self { strings: Vec::new() }
118+
LL | | }
119+
| |_________^
120+
121+
error: aborting due to 13 previous errors
106122

0 commit comments

Comments
 (0)