Skip to content

Commit 6bcd0b9

Browse files
committed
Auto merge of rust-lang#13558 - alex-semenyuk:const_is_empty_fix, r=dswij
Don't trigger `const_is_empty` for inline const assertions Close rust-lang#13106 Considered case was described [here](rust-lang/rust-clippy#13114 (comment)) changelog: [`const_is_empty`]: skip const_is_empty for inline const assertions
2 parents c2534dc + 8555922 commit 6bcd0b9

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

clippy_lints/src/methods/is_empty.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::consts::ConstEvalCtxt;
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::{find_binding_init, path_to_local};
3+
use clippy_utils::macros::{is_assert_macro, root_macro_call};
4+
use clippy_utils::{find_binding_init, get_parent_expr, is_inside_always_const_context, path_to_local};
45
use rustc_hir::{Expr, HirId};
56
use rustc_lint::{LateContext, LintContext};
67
use rustc_middle::lint::in_external_macro;
@@ -14,6 +15,16 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, receiver: &Expr<'_
1415
if in_external_macro(cx.sess(), expr.span) || !receiver.span.eq_ctxt(expr.span) {
1516
return;
1617
}
18+
if let Some(parent) = get_parent_expr(cx, expr) {
19+
if let Some(parent) = get_parent_expr(cx, parent) {
20+
if is_inside_always_const_context(cx.tcx, expr.hir_id)
21+
&& let Some(macro_call) = root_macro_call(parent.span)
22+
&& is_assert_macro(cx, macro_call.def_id)
23+
{
24+
return;
25+
}
26+
}
27+
}
1728
let init_expr = expr_or_init(cx, receiver);
1829
if !receiver.span.eq_ctxt(init_expr.span) {
1930
return;

tests/ui/const_is_empty.rs

+14
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,17 @@ fn constant_from_external_crate() {
171171
let _ = std::env::consts::EXE_EXTENSION.is_empty();
172172
// Do not lint, `exe_ext` comes from the `std` crate
173173
}
174+
175+
fn issue_13106() {
176+
const {
177+
assert!(!NON_EMPTY_STR.is_empty());
178+
}
179+
180+
const {
181+
assert!(EMPTY_STR.is_empty());
182+
}
183+
184+
const {
185+
EMPTY_STR.is_empty();
186+
}
187+
}

tests/ui/const_is_empty.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,11 @@ error: this expression always evaluates to true
157157
LL | let _ = val.is_empty();
158158
| ^^^^^^^^^^^^^^
159159

160-
error: aborting due to 26 previous errors
160+
error: this expression always evaluates to true
161+
--> tests/ui/const_is_empty.rs:185:9
162+
|
163+
LL | EMPTY_STR.is_empty();
164+
| ^^^^^^^^^^^^^^^^^^^^
165+
166+
error: aborting due to 27 previous errors
161167

0 commit comments

Comments
 (0)