Skip to content

Make comparison_to_empty work on if let/let chains #11029

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 2 commits into from
Jul 23, 2023
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
30 changes: 27 additions & 3 deletions clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{
AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, GenericBound, ImplItem, ImplItemKind,
ImplicitSelfKind, Item, ItemKind, Mutability, Node, PathSegment, PrimTy, QPath, TraitItemRef, TyKind,
TypeBindingKind,
ImplicitSelfKind, Item, ItemKind, LangItem, Mutability, Node, PatKind, PathSegment, PrimTy, QPath, TraitItemRef,
TyKind, TypeBindingKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, AssocKind, FnSig, Ty};
Expand Down Expand Up @@ -168,6 +167,31 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
return;
}

if let ExprKind::Let(lt) = expr.kind
&& has_is_empty(cx, lt.init)
&& match lt.pat.kind {
PatKind::Slice([], None, []) => true,
PatKind::Lit(lit) if is_empty_string(lit) => true,
_ => false,
}
{
let mut applicability = Applicability::MachineApplicable;

let lit1 = peel_ref_operators(cx, lt.init);
let lit_str =
Sugg::hir_with_context(cx, lit1, lt.span.ctxt(), "_", &mut applicability).maybe_par();

span_lint_and_sugg(
cx,
COMPARISON_TO_EMPTY,
lt.span,
"comparison to empty slice using `if let`",
"using `is_empty` is clearer and more explicit",
format!("{lit_str}.is_empty()"),
applicability,
);
}

if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind {
// expr.span might contains parenthesis, see issue #10529
let actual_span = left.span.with_hi(right.span.hi());
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/comparison_to_empty.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//@run-rustfix

#![warn(clippy::comparison_to_empty)]
#![allow(clippy::useless_vec)]
#![allow(clippy::borrow_deref_ref, clippy::needless_if, clippy::useless_vec)]
#![feature(let_chains)]

fn main() {
// Disallow comparisons to empty
Expand All @@ -12,6 +13,11 @@ fn main() {
let v = vec![0];
let _ = v.is_empty();
let _ = !v.is_empty();
if (*v).is_empty() {}
let s = [0].as_slice();
if s.is_empty() {}
if s.is_empty() {}
if s.is_empty() && s.is_empty() {}

// Allow comparisons to non-empty
let s = String::new();
Expand All @@ -21,4 +27,8 @@ fn main() {
let v = vec![0];
let _ = v == [0];
let _ = v != [0];
if let [0] = &*v {}
let s = [0].as_slice();
if let [0] = s {}
if let [0] = &*s && s == [0] {}
}
12 changes: 11 additions & 1 deletion tests/ui/comparison_to_empty.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//@run-rustfix

#![warn(clippy::comparison_to_empty)]
#![allow(clippy::useless_vec)]
#![allow(clippy::borrow_deref_ref, clippy::needless_if, clippy::useless_vec)]
#![feature(let_chains)]

fn main() {
// Disallow comparisons to empty
Expand All @@ -12,6 +13,11 @@ fn main() {
let v = vec![0];
let _ = v == [];
let _ = v != [];
if let [] = &*v {}
let s = [0].as_slice();
if let [] = s {}
if let [] = &*s {}
if let [] = &*s && s == [] {}

// Allow comparisons to non-empty
let s = String::new();
Expand All @@ -21,4 +27,8 @@ fn main() {
let v = vec![0];
let _ = v == [0];
let _ = v != [0];
if let [0] = &*v {}
let s = [0].as_slice();
if let [0] = s {}
if let [0] = &*s && s == [0] {}
}
40 changes: 35 additions & 5 deletions tests/ui/comparison_to_empty.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
error: comparison to empty slice
--> $DIR/comparison_to_empty.rs:9:13
--> $DIR/comparison_to_empty.rs:10:13
|
LL | let _ = s == "";
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`
|
= note: `-D clippy::comparison-to-empty` implied by `-D warnings`

error: comparison to empty slice
--> $DIR/comparison_to_empty.rs:10:13
--> $DIR/comparison_to_empty.rs:11:13
|
LL | let _ = s != "";
| ^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!s.is_empty()`

error: comparison to empty slice
--> $DIR/comparison_to_empty.rs:13:13
--> $DIR/comparison_to_empty.rs:14:13
|
LL | let _ = v == [];
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `v.is_empty()`

error: comparison to empty slice
--> $DIR/comparison_to_empty.rs:14:13
--> $DIR/comparison_to_empty.rs:15:13
|
LL | let _ = v != [];
| ^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!v.is_empty()`

error: aborting due to 4 previous errors
error: comparison to empty slice using `if let`
--> $DIR/comparison_to_empty.rs:16:8
|
LL | if let [] = &*v {}
| ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(*v).is_empty()`

error: comparison to empty slice using `if let`
--> $DIR/comparison_to_empty.rs:18:8
|
LL | if let [] = s {}
| ^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

error: comparison to empty slice using `if let`
--> $DIR/comparison_to_empty.rs:19:8
|
LL | if let [] = &*s {}
| ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

error: comparison to empty slice using `if let`
--> $DIR/comparison_to_empty.rs:20:8
|
LL | if let [] = &*s && s == [] {}
| ^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

error: comparison to empty slice
--> $DIR/comparison_to_empty.rs:20:24
|
LL | if let [] = &*s && s == [] {}
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

error: aborting due to 9 previous errors