let qualify_min_const_fn deal with drop checks

This commit is contained in:
y21
2024-04-16 20:25:35 +02:00
parent 1e407642e8
commit 6fdf295664
3 changed files with 36 additions and 15 deletions
+1 -14
View File
@@ -1,7 +1,6 @@
use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::qualify_min_const_fn::is_min_const_fn;
use clippy_utils::ty::has_drop;
use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method};
use rustc_hir as hir;
use rustc_hir::def_id::CRATE_DEF_ID;
@@ -121,10 +120,7 @@ fn check_fn(
}
},
FnKind::Method(_, sig, ..) => {
if trait_ref_of_method(cx, def_id).is_some()
|| already_const(sig.header)
|| method_accepts_droppable(cx, def_id)
{
if trait_ref_of_method(cx, def_id).is_some() || already_const(sig.header) {
return;
}
},
@@ -162,15 +158,6 @@ fn check_fn(
extract_msrv_attr!(LateContext);
}
/// 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_droppable(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
// If any of the params are droppable, return true
sig.inputs().iter().any(|&ty| has_drop(cx, ty))
}
// We don't have to lint on something that's already `const`
#[must_use]
fn already_const(header: hir::FnHeader) -> bool {
@@ -141,3 +141,21 @@ fn union_access_can_be_const() {
let _ = unsafe { bar.val };
}
}
mod issue12677 {
pub struct Wrapper {
pub strings: Vec<String>,
}
impl Wrapper {
#[must_use]
pub fn new(strings: Vec<String>) -> Self {
Self { strings }
}
#[must_use]
pub fn empty() -> Self {
Self { strings: Vec::new() }
}
}
}
@@ -130,5 +130,21 @@ LL | | let _ = unsafe { bar.val };
LL | | }
| |_____^
error: aborting due to 14 previous errors
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:152:9
|
LL | / pub fn new(strings: Vec<String>) -> Self {
LL | | Self { strings }
LL | | }
| |_________^
error: this could be a `const fn`
--> tests/ui/missing_const_for_fn/could_be_const.rs:157:9
|
LL | / pub fn empty() -> Self {
LL | | Self { strings: Vec::new() }
LL | | }
| |_________^
error: aborting due to 16 previous errors