Skip to content

Don't trigger const_is_empty for inline const assertions #13558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion clippy_lints/src/methods/is_empty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::consts::ConstEvalCtxt;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::{find_binding_init, path_to_local};
use clippy_utils::macros::{is_assert_macro, root_macro_call};
use clippy_utils::{find_binding_init, get_parent_expr, is_inside_always_const_context, path_to_local};
use rustc_hir::{Expr, HirId};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro;
Expand All @@ -14,6 +15,16 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, receiver: &Expr<'_
if in_external_macro(cx.sess(), expr.span) || !receiver.span.eq_ctxt(expr.span) {
return;
}
if let Some(parent) = get_parent_expr(cx, expr) {
if let Some(parent) = get_parent_expr(cx, parent) {
if is_inside_always_const_context(cx.tcx, expr.hir_id)
&& let Some(macro_call) = root_macro_call(parent.span)
&& is_assert_macro(cx, macro_call.def_id)
{
return;
}
}
}
let init_expr = expr_or_init(cx, receiver);
if !receiver.span.eq_ctxt(init_expr.span) {
return;
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/const_is_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,17 @@ fn constant_from_external_crate() {
let _ = std::env::consts::EXE_EXTENSION.is_empty();
// Do not lint, `exe_ext` comes from the `std` crate
}

fn issue_13106() {
const {
assert!(!NON_EMPTY_STR.is_empty());
}

const {
assert!(EMPTY_STR.is_empty());
}

const {
EMPTY_STR.is_empty();
}
}
8 changes: 7 additions & 1 deletion tests/ui/const_is_empty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,11 @@ error: this expression always evaluates to true
LL | let _ = val.is_empty();
| ^^^^^^^^^^^^^^

error: aborting due to 26 previous errors
error: this expression always evaluates to true
--> tests/ui/const_is_empty.rs:185:9
|
LL | EMPTY_STR.is_empty();
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 27 previous errors