Skip to content

Commit e6cb5a2

Browse files
committed
Ignore non ExprKind::{Path,Lit) inside const context
- remove now dead code in ASSERTIONS_ON_CONSTANTS cc #11966 - Partially revert "ignore `assertions-on-constants` in const contexts" This reverts commit c7074de.
1 parent c7074de commit e6cb5a2

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

clippy_lints/src/assertions_on_constants.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use clippy_utils::consts::{constant_with_source, Constant, ConstantSource};
1+
use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint_and_help;
33
use clippy_utils::is_inside_always_const_context;
44
use clippy_utils::macros::{find_assert_args, root_macro_call_first_node, PanicExpn};
5-
use rustc_hir::{Expr, Item, ItemKind, Node};
5+
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
88
use rustc_span::sym;
@@ -32,10 +32,6 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]);
3232

3333
impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
3434
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-
3935
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
4036
return;
4137
};
@@ -47,17 +43,17 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
4743
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else {
4844
return;
4945
};
50-
let Some((Constant::Bool(val), source)) = constant_with_source(cx, cx.typeck_results(), condition) else {
46+
let Some(Constant::Bool(val)) = constant(cx, cx.typeck_results(), condition) else {
5147
return;
5248
};
53-
if let ConstantSource::Constant = source
54-
&& let Node::Item(Item {
55-
kind: ItemKind::Const(..),
56-
..
57-
}) = cx.tcx.parent_hir_node(e.hir_id)
58-
{
59-
return;
49+
50+
match condition.kind {
51+
ExprKind::Path(..) => {},
52+
ExprKind::Lit(_) => {},
53+
_ if is_inside_always_const_context(cx.tcx, e.hir_id) => return,
54+
_ => {},
6055
}
56+
6157
if val {
6258
span_lint_and_help(
6359
cx,

tests/ui/assertions_on_constants.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(non_fmt_panics, clippy::needless_bool)]
1+
#![allow(non_fmt_panics, clippy::needless_bool, clippy::eq_op)]
22

33
macro_rules! assert_const {
44
($len:expr) => {
@@ -47,14 +47,18 @@ fn main() {
4747
assert!(!CFG_FLAG);
4848

4949
const _: () = assert!(true);
50+
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
51+
52+
assert!(8 == (7 + 1));
53+
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
5054

5155
// Don't lint if the value is dependent on a defined constant:
5256
const N: usize = 1024;
5357
const _: () = assert!(N.is_power_of_two());
5458
}
5559

56-
#[allow(clippy::eq_op)]
5760
const _: () = {
5861
assert!(true);
62+
//~^ ERROR: `assert!(true)` will be optimized out by the compiler
5963
assert!(8 == (7 + 1));
6064
};

tests/ui/assertions_on_constants.stderr

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

75-
error: aborting due to 9 previous errors
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:52:5
85+
|
86+
LL | assert!(8 == (7 + 1));
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!(true);
95+
| ^^^^^^^^^^^^^
96+
|
97+
= help: remove it
98+
99+
error: aborting due to 12 previous errors
76100

0 commit comments

Comments
 (0)