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 c7074de420a2192fb40d3f2194a20dd0d1b65cc6.
This commit is contained in:
parent
ac600282a0
commit
a0234b4e8b
@ -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;
|
||||
@ -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;
|
||||
};
|
||||
@ -47,17 +43,16 @@ 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,
|
||||
|
@ -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) => {
|
||||
@ -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));
|
||||
};
|
||||
|
@ -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
|
||||
|
||||
|
@ -124,4 +124,5 @@ const _: () = {
|
||||
|
||||
const fn foo() {
|
||||
assert!([42, 55].len() > get_usize());
|
||||
//~^ ERROR: unnecessary operation
|
||||
}
|
||||
|
@ -128,4 +128,5 @@ const _: () = {
|
||||
|
||||
const fn foo() {
|
||||
[42, 55][get_usize()];
|
||||
//~^ ERROR: unnecessary operation
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user