Skip to content

Commit c7074de

Browse files
committed
ignore assertions-on-constants in const contexts
1 parent bc4c21c commit c7074de

File tree

3 files changed

+6
-26
lines changed

3 files changed

+6
-26
lines changed

clippy_lints/src/assertions_on_constants.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::consts::{constant_with_source, Constant, ConstantSource};
22
use clippy_utils::diagnostics::span_lint_and_help;
3+
use clippy_utils::is_inside_always_const_context;
34
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
45
use rustc_hir::{Expr, Item, ItemKind, Node};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -31,6 +32,10 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3132

3233
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
3334
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
35+
if is_inside_always_const_context(cx.tcx, e.hir_id) {
36+
return;
37+
}
38+
3439
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
3540
return;
3641
};

tests/ui/assertions_on_constants.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ fn main() {
4747
assert!(!CFG_FLAG);
4848

4949
const _: () = assert!(true);
50-
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
5150

5251
// Don't lint if the value is dependent on a defined constant:
5352
const N: usize = 1024;

tests/ui/assertions_on_constants.stderr

+1-25
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,5 @@ LL | debug_assert!(true);
7272
|
7373
= help: remove it
7474

75-
error: `assert!(true)` will be optimized out by the compiler
76-
--> tests/ui/assertions_on_constants.rs:49:19
77-
|
78-
LL | const _: () = assert!(true);
79-
| ^^^^^^^^^^^^^
80-
|
81-
= help: remove it
82-
83-
error: `assert!(true)` will be optimized out by the compiler
84-
--> tests/ui/assertions_on_constants.rs:59:5
85-
|
86-
LL | assert!(true);
87-
| ^^^^^^^^^^^^^
88-
|
89-
= help: remove it
90-
91-
error: `assert!(true)` will be optimized out by the compiler
92-
--> tests/ui/assertions_on_constants.rs:60:5
93-
|
94-
LL | assert!(8 == (7 + 1));
95-
| ^^^^^^^^^^^^^^^^^^^^^
96-
|
97-
= help: remove it
98-
99-
error: aborting due to 12 previous errors
75+
error: aborting due to 9 previous errors
10076

0 commit comments

Comments
 (0)