Skip to content

Commit

Permalink
Lint comparison to empty slice using PartialEq methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Sep 22, 2024
1 parent 43e3384 commit acff511
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
15 changes: 14 additions & 1 deletion clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators};
use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, is_trait_method, peel_ref_operators};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
Expand Down Expand Up @@ -185,6 +185,19 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
);
}

if let ExprKind::MethodCall(method, lhs_expr, [rhs_expr], _) = expr.kind
&& is_trait_method(cx, expr, sym::PartialEq)
&& !expr.span.from_expansion()
{
check_empty_expr(
cx,
expr.span,
lhs_expr,
peel_ref_operators(cx, rhs_expr),
(method.ident.name == sym::ne).then_some("!").unwrap_or_default(),
);
}

if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind
&& !expr.span.from_expansion()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/comparison_to_empty.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ fn main() {
if let [0] = &*s
&& s == [0]
{}

// Also lint the `PartialEq` methods
let s = String::new();
let _ = s.is_empty();
let _ = !s.is_empty();
let v = vec![0];
let _ = v.is_empty();
let _ = !v.is_empty();
}
8 changes: 8 additions & 0 deletions tests/ui/comparison_to_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ fn main() {
if let [0] = &*s
&& s == [0]
{}

// Also lint the `PartialEq` methods
let s = String::new();
let _ = s.eq("");
let _ = s.ne("");
let v = vec![0];
let _ = v.eq(&[]);
let _ = v.ne(&[]);
}
26 changes: 25 additions & 1 deletion tests/ui/comparison_to_empty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,29 @@ error: comparison to empty slice
LL | && s == []
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

error: aborting due to 9 previous errors
error: comparison to empty slice
--> tests/ui/comparison_to_empty.rs:39:13
|
LL | let _ = s.eq("");
| ^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`

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

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

error: comparison to empty slice
--> tests/ui/comparison_to_empty.rs:43:13
|
LL | let _ = v.ne(&[]);
| ^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!v.is_empty()`

error: aborting due to 13 previous errors

0 comments on commit acff511

Please sign in to comment.