diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2025-01-06 19:47:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-01-06 19:47:54 +0000 |
| commit | 0e2505ba8f34303f7f049af20f58b0ddc80bb404 (patch) | |
| tree | 51946794896938f2f11cb71f2c0b39beda8b0c7d | |
| parent | a9c0e22dfaaaf97e3e589310459fc8c1775c1ab4 (diff) | |
| parent | ca55534c920fdd152c44ccb4051e928b8ffc94e6 (diff) | |
| download | rust-0e2505ba8f34303f7f049af20f58b0ddc80bb404.tar.gz rust-0e2505ba8f34303f7f049af20f58b0ddc80bb404.zip | |
Do not trigger `missing_const_for_fn` for tests (#13945)
Close #13938 changelog: [`missing_const_for_fn`]: do not trigger for tests
| -rw-r--r-- | clippy_lints/src/missing_const_for_fn.rs | 9 | ||||
| -rw-r--r-- | tests/ui/missing_const_for_fn/cant_be_const.rs | 29 |
2 files changed, 34 insertions, 4 deletions
diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 121c4326d64..2572e186ce6 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -2,7 +2,7 @@ use clippy_config::Conf; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::msrvs::{self, Msrv}; use clippy_utils::qualify_min_const_fn::is_min_const_fn; -use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, trait_ref_of_method}; +use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_macro, is_in_test, trait_ref_of_method}; use rustc_errors::Applicability; use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::intravisit::FnKind; @@ -97,6 +97,11 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { span: Span, def_id: LocalDefId, ) { + let hir_id = cx.tcx.local_def_id_to_hir_id(def_id); + if is_in_test(cx.tcx, hir_id) { + return; + } + if !self.msrv.meets(msrvs::CONST_IF_MATCH) { return; } @@ -136,8 +141,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { return; } - let hir_id = cx.tcx.local_def_id_to_hir_id(def_id); - // Const fns are not allowed as methods in a trait. { let parent = cx.tcx.hir().get_parent_item(hir_id).def_id; diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index ca323dcf173..d2f9e34a5ce 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -47,7 +47,34 @@ fn get_y() -> u32 { Y } -// Don't lint entrypoint functions +#[cfg(test)] +mod with_test_fn { + #[derive(Clone, Copy)] + pub struct Foo { + pub n: u32, + } + + impl Foo { + #[must_use] + pub const fn new(n: u32) -> Foo { + Foo { n } + } + } + + #[test] + fn foo_is_copy() { + let foo = Foo::new(42); + let one = foo; + let two = foo; + _ = one; + _ = two; + } +} + +// Allowing on this function, because it would lint, which we don't want in this case. +// if we have `#[start]` and `#[test]` check `is_entrypoint_fn(cx, def_id.to_def_id())` is stopped +// working +#[allow(clippy::missing_const_for_fn)] #[start] fn init(num: isize, something: *const *const u8) -> isize { 1 |
