Skip to content

Commit 1daad06

Browse files
committed
feat: add const_is_empty lint
1 parent 74f611f commit 1daad06

File tree

6 files changed

+141
-1
lines changed

6 files changed

+141
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5109,6 +5109,7 @@ Released 2018-09-13
51095109
[`collection_is_never_read`]: https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
51105110
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
51115111
[`comparison_to_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty
5112+
[`const_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_is_empty
51125113
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
51135114
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
51145115
[`crate_in_macro_def`]: https://rust-lang.github.io/rust-clippy/master/index.html#crate_in_macro_def

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
349349
crate::methods::CLONE_ON_COPY_INFO,
350350
crate::methods::CLONE_ON_REF_PTR_INFO,
351351
crate::methods::COLLAPSIBLE_STR_REPLACE_INFO,
352+
crate::methods::CONST_IS_EMPTY_INFO,
352353
crate::methods::DRAIN_COLLECT_INFO,
353354
crate::methods::ERR_EXPECT_INFO,
354355
crate::methods::EXPECT_FUN_CALL_INFO,

clippy_lints/src/methods/is_empty.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use clippy_utils::diagnostics::span_lint_and_note;
2+
use clippy_utils::expr_or_init;
3+
use rustc_ast::LitKind;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::{LateContext, LintContext};
6+
use rustc_middle::lint::in_external_macro;
7+
use rustc_span::source_map::Spanned;
8+
9+
use super::CONST_IS_EMPTY;
10+
11+
pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, receiver: &Expr<'_>) {
12+
if in_external_macro(cx.sess(), expr.span) || !receiver.span.eq_ctxt(expr.span) {
13+
return;
14+
}
15+
let init_expr = expr_or_init(cx, receiver);
16+
if let Some(init_is_empty) = is_empty(init_expr)
17+
&& init_expr.span.eq_ctxt(receiver.span)
18+
{
19+
span_lint_and_note(
20+
cx,
21+
CONST_IS_EMPTY,
22+
expr.span,
23+
&format!("this expression always evaluates to {init_is_empty:?}"),
24+
Some(init_expr.span),
25+
"because its initialization value is constant",
26+
);
27+
}
28+
}
29+
30+
fn is_empty(expr: &'_ rustc_hir::Expr<'_>) -> Option<bool> {
31+
if let ExprKind::Lit(Spanned { node, .. }) = expr.kind {
32+
match node {
33+
LitKind::Str(sym, _) => Some(sym.is_empty()),
34+
LitKind::ByteStr(value, _) | LitKind::CStr(value, _) => Some(value.is_empty()),
35+
_ => None,
36+
}
37+
} else {
38+
None
39+
}
40+
}

clippy_lints/src/methods/mod.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod inefficient_to_string;
3636
mod inspect_for_each;
3737
mod into_iter_on_ref;
3838
mod is_digit_ascii_radix;
39+
mod is_empty;
3940
mod iter_cloned_collect;
4041
mod iter_count;
4142
mod iter_filter;
@@ -4011,6 +4012,30 @@ declare_clippy_lint! {
40114012
r#"creating a `CStr` through functions when `c""` literals can be used"#
40124013
}
40134014

4015+
declare_clippy_lint! {
4016+
/// ### What it does
4017+
/// It identifies calls to `.is_empty()` on a value initialized from a literal string.
4018+
///
4019+
/// ### Why is this bad?
4020+
/// Literal strings are known at compile time. Their emptyness can be determined statically.
4021+
///
4022+
/// ### Example
4023+
/// ```no_run
4024+
/// let value = "";
4025+
/// if value.is_empty() {
4026+
/// println!("the string is empty");
4027+
/// }
4028+
/// ```
4029+
/// Use instead:
4030+
/// ```no_run
4031+
/// println!("the string is empty");
4032+
/// ```
4033+
#[clippy::version = "1.78.0"]
4034+
pub CONST_IS_EMPTY,
4035+
suspicious,
4036+
"is_empty() called on strings known at compile time"
4037+
}
4038+
40144039
pub struct Methods {
40154040
avoid_breaking_exported_api: bool,
40164041
msrv: Msrv,
@@ -4059,6 +4084,7 @@ impl_lint_pass!(Methods => [
40594084
CLONE_ON_COPY,
40604085
CLONE_ON_REF_PTR,
40614086
COLLAPSIBLE_STR_REPLACE,
4087+
CONST_IS_EMPTY,
40624088
ITER_OVEREAGER_CLONED,
40634089
CLONED_INSTEAD_OF_COPIED,
40644090
FLAT_MAP_OPTION,
@@ -4411,7 +4437,7 @@ impl Methods {
44114437
("as_deref" | "as_deref_mut", []) => {
44124438
needless_option_as_deref::check(cx, expr, recv, name);
44134439
},
4414-
("as_bytes" | "is_empty", []) => {
4440+
("as_bytes", []) => {
44154441
if let Some(("as_str", recv, [], as_str_span, _)) = method_call(recv) {
44164442
redundant_as_str::check(cx, expr, recv, as_str_span, span);
44174443
}
@@ -4585,6 +4611,12 @@ impl Methods {
45854611
("hash", [arg]) => {
45864612
unit_hash::check(cx, expr, recv, arg);
45874613
},
4614+
("is_empty", []) => {
4615+
if let Some(("as_str", recv, [], as_str_span, _)) = method_call(recv) {
4616+
redundant_as_str::check(cx, expr, recv, as_str_span, span);
4617+
}
4618+
is_empty::check(cx, expr, recv);
4619+
},
45884620
("is_file", []) => filetype_is_file::check(cx, expr, recv),
45894621
("is_digit", [radix]) => is_digit_ascii_radix::check(cx, expr, recv, radix, &self.msrv),
45904622
("is_none", []) => check_is_some_is_none(cx, expr, recv, false),

tests/ui/const_is_empty.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::const_is_empty)]
2+
3+
fn main() {
4+
let value = "foobar";
5+
let _ = value.is_empty();
6+
//~^ ERROR: this expression always evaluates to false
7+
let x = value;
8+
let _ = x.is_empty();
9+
//~^ ERROR: this expression always evaluates to false
10+
let _ = "".is_empty();
11+
//~^ ERROR: this expression always evaluates to true
12+
let _ = b"".is_empty();
13+
//~^ ERROR: this expression always evaluates to true
14+
}

tests/ui/const_is_empty.stderr

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: this expression always evaluates to false
2+
--> tests/ui/const_is_empty.rs:5:13
3+
|
4+
LL | let _ = value.is_empty();
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
note: because its initialization value is constant
8+
--> tests/ui/const_is_empty.rs:4:17
9+
|
10+
LL | let value = "foobar";
11+
| ^^^^^^^^
12+
= note: `-D clippy::const-is-empty` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::const_is_empty)]`
14+
15+
error: this expression always evaluates to false
16+
--> tests/ui/const_is_empty.rs:8:13
17+
|
18+
LL | let _ = x.is_empty();
19+
| ^^^^^^^^^^^^
20+
|
21+
note: because its initialization value is constant
22+
--> tests/ui/const_is_empty.rs:4:17
23+
|
24+
LL | let value = "foobar";
25+
| ^^^^^^^^
26+
27+
error: this expression always evaluates to true
28+
--> tests/ui/const_is_empty.rs:10:13
29+
|
30+
LL | let _ = "".is_empty();
31+
| ^^^^^^^^^^^^^
32+
|
33+
note: because its initialization value is constant
34+
--> tests/ui/const_is_empty.rs:10:13
35+
|
36+
LL | let _ = "".is_empty();
37+
| ^^
38+
39+
error: this expression always evaluates to true
40+
--> tests/ui/const_is_empty.rs:12:13
41+
|
42+
LL | let _ = b"".is_empty();
43+
| ^^^^^^^^^^^^^^
44+
|
45+
note: because its initialization value is constant
46+
--> tests/ui/const_is_empty.rs:12:13
47+
|
48+
LL | let _ = b"".is_empty();
49+
| ^^^
50+
51+
error: aborting due to 4 previous errors
52+

0 commit comments

Comments
 (0)