Skip to content

Commit 763e334

Browse files
committed
let qualify_min_const_fn deal with drop checks
1 parent caad063 commit 763e334

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
@@ -141,3 +141,21 @@ mod msrv {
141141
let _ = unsafe { bar.val };
142142
}
143143
}
144+
145+
mod issue12677 {
146+
pub struct Wrapper {
147+
pub strings: Vec<String>,
148+
}
149+
150+
impl Wrapper {
151+
#[must_use]
152+
pub fn new(strings: Vec<String>) -> Self {
153+
Self { strings }
154+
}
155+
156+
#[must_use]
157+
pub fn empty() -> Self {
158+
Self { strings: Vec::new() }
159+
}
160+
}
161+
}

tests/ui/missing_const_for_fn/could_be_const.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,21 @@ LL | | let _ = unsafe { bar.val };
130130
LL | | }
131131
| |_____^
132132

133-
error: aborting due to 14 previous errors
133+
error: this could be a `const fn`
134+
--> tests/ui/missing_const_for_fn/could_be_const.rs:152:9
135+
|
136+
LL | / pub fn new(strings: Vec<String>) -> Self {
137+
LL | | Self { strings }
138+
LL | | }
139+
| |_________^
140+
141+
error: this could be a `const fn`
142+
--> tests/ui/missing_const_for_fn/could_be_const.rs:157:9
143+
|
144+
LL | / pub fn empty() -> Self {
145+
LL | | Self { strings: Vec::new() }
146+
LL | | }
147+
| |_________^
148+
149+
error: aborting due to 16 previous errors
134150

0 commit comments

Comments
 (0)