Skip to content

Commit

Permalink
Ignore non ExprKind::{Path,Lit) inside const context
Browse files Browse the repository at this point in the history
- remove now dead code in ASSERTIONS_ON_CONSTANTS
  cc #11966
- Partially revert "ignore `assertions-on-constants` in const contexts"
  This reverts commit c7074de.
  • Loading branch information
tesuji committed Jun 8, 2024
1 parent c7074de commit 7b06d72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
24 changes: 10 additions & 14 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_utils::consts::{constant_with_source, Constant, ConstantSource};
use clippy_utils::consts::{constant, Constant};
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_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::sym;
Expand Down Expand Up @@ -32,10 +32,6 @@ 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 All @@ -47,17 +43,17 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else {
return;
};
let Some((Constant::Bool(val), source)) = constant_with_source(cx, cx.typeck_results(), condition) else {
let Some(Constant::Bool(val)) = constant(cx, cx.typeck_results(), condition) else {
return;
};
if let ConstantSource::Constant = source
&& let Node::Item(Item {
kind: ItemKind::Const(..),
..
}) = cx.tcx.parent_hir_node(e.hir_id)
{
return;

match condition.kind {
ExprKind::Path(..) => {},
ExprKind::Lit(_) => {},
_ if is_inside_always_const_context(cx.tcx, e.hir_id) => return,
_ => {},
}

if val {
span_lint_and_help(
cx,
Expand Down
8 changes: 6 additions & 2 deletions tests/ui/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(non_fmt_panics, clippy::needless_bool)]
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)]

macro_rules! assert_const {
($len:expr) => {
Expand Down Expand Up @@ -47,14 +47,18 @@ fn main() {
assert!(!CFG_FLAG);

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

assert!(8 == (7 + 1));
//~^ 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;
const _: () = assert!(N.is_power_of_two());
}

#[allow(clippy::eq_op)]
const _: () = {
assert!(true);
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
assert!(8 == (7 + 1));
};
26 changes: 25 additions & 1 deletion tests/ui/assertions_on_constants.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,29 @@ LL | debug_assert!(true);
|
= help: remove it

error: aborting due to 9 previous errors
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:52:5
|
LL | assert!(8 == (7 + 1));
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: remove it

error: `assert!(true)` will be optimized out by the compiler
--> tests/ui/assertions_on_constants.rs:61:5
|
LL | assert!(true);
| ^^^^^^^^^^^^^
|
= help: remove it

error: aborting due to 12 previous errors

0 comments on commit 7b06d72

Please sign in to comment.