From ac600282a0cc35bd99b3e3152a655991d3c1dfea Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 24 May 2024 10:51:15 +0700 Subject: [PATCH] ignore `assertions-on-constants` in const contexts --- clippy_lints/src/assertions_on_constants.rs | 5 ++++ tests/ui/assertions_on_constants.rs | 1 - tests/ui/assertions_on_constants.stderr | 26 +-------------------- 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 2003dd1fb0e2..863c25aba861 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,5 +1,6 @@ use clippy_utils::consts::{constant_with_source, Constant, ConstantSource}; use clippy_utils::diagnostics::span_lint_and_help; +use clippy_utils::is_inside_always_const_context; use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn}; use rustc_hir::{Expr, Item, ItemKind, Node}; use rustc_lint::{LateContext, LateLintPass}; @@ -31,6 +32,10 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { + if is_inside_always_const_context(cx.tcx, e.hir_id) { + return; + } + let Some(macro_call) = root_macro_call_first_node(cx, e) else { return; }; diff --git a/tests/ui/assertions_on_constants.rs b/tests/ui/assertions_on_constants.rs index 2e0fee08a595..c3574b0b466c 100644 --- a/tests/ui/assertions_on_constants.rs +++ b/tests/ui/assertions_on_constants.rs @@ -47,7 +47,6 @@ fn main() { assert!(!CFG_FLAG); const _: () = assert!(true); - //~^ ERROR: `assert!(true)` will be optimized out by the compiler // Don't lint if the value is dependent on a defined constant: const N: usize = 1024; diff --git a/tests/ui/assertions_on_constants.stderr b/tests/ui/assertions_on_constants.stderr index 3a64f1892ad9..aa4868af5a8c 100644 --- a/tests/ui/assertions_on_constants.stderr +++ b/tests/ui/assertions_on_constants.stderr @@ -72,29 +72,5 @@ LL | debug_assert!(true); | = help: remove it -error: `assert!(true)` will be optimized out by the compiler - --> tests/ui/assertions_on_constants.rs:49:19 - | -LL | const _: () = assert!(true); - | ^^^^^^^^^^^^^ - | - = help: remove it - -error: `assert!(true)` will be optimized out by the compiler - --> tests/ui/assertions_on_constants.rs:59:5 - | -LL | assert!(true); - | ^^^^^^^^^^^^^ - | - = help: remove it - -error: `assert!(true)` will be optimized out by the compiler - --> tests/ui/assertions_on_constants.rs:60:5 - | -LL | assert!(8 == (7 + 1)); - | ^^^^^^^^^^^^^^^^^^^^^ - | - = help: remove it - -error: aborting due to 12 previous errors +error: aborting due to 9 previous errors