Skip to content

Commit

Permalink
ignore assertions-on-constants in const contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed May 24, 2024
1 parent d04413a commit 42b24c8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 deletions.
5 changes: 5 additions & 0 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::consts::{constant_with_source, Constant, ConstantSource};
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
use clippy_utils::qualify_min_const_fn::is_inside_always_const_context;
use rustc_hir::{Expr, Item, ItemKind, Node};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
Expand Down Expand Up @@ -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;
};
Expand Down
1 change: 0 additions & 1 deletion tests/ui/assertions_on_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 1 addition & 25 deletions tests/ui/assertions_on_constants.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42b24c8

Please sign in to comment.