Skip to content

Commit 7901289

Browse files
committed
Auto merge of rust-lang#13432 - samueltardieu:issue-13401, r=Manishearth
Lint comparison to empty slice using `PartialEq` methods changelog: [`comparison_to_empty`]: Also detect comparaisons using `PartialEq` methods Fix rust-lang#13401
2 parents 8ab744e + acff511 commit 7901289

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

clippy_lints/src/len_zero.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
33
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
4-
use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators};
4+
use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, is_trait_method, peel_ref_operators};
55
use rustc_ast::ast::LitKind;
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
@@ -185,6 +185,19 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
185185
);
186186
}
187187

188+
if let ExprKind::MethodCall(method, lhs_expr, [rhs_expr], _) = expr.kind
189+
&& is_trait_method(cx, expr, sym::PartialEq)
190+
&& !expr.span.from_expansion()
191+
{
192+
check_empty_expr(
193+
cx,
194+
expr.span,
195+
lhs_expr,
196+
peel_ref_operators(cx, rhs_expr),
197+
(method.ident.name == sym::ne).then_some("!").unwrap_or_default(),
198+
);
199+
}
200+
188201
if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind
189202
&& !expr.span.from_expansion()
190203
{

tests/ui/comparison_to_empty.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ fn main() {
3333
if let [0] = &*s
3434
&& s == [0]
3535
{}
36+
37+
// Also lint the `PartialEq` methods
38+
let s = String::new();
39+
let _ = s.is_empty();
40+
let _ = !s.is_empty();
41+
let v = vec![0];
42+
let _ = v.is_empty();
43+
let _ = !v.is_empty();
3644
}

tests/ui/comparison_to_empty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ fn main() {
3333
if let [0] = &*s
3434
&& s == [0]
3535
{}
36+
37+
// Also lint the `PartialEq` methods
38+
let s = String::new();
39+
let _ = s.eq("");
40+
let _ = s.ne("");
41+
let v = vec![0];
42+
let _ = v.eq(&[]);
43+
let _ = v.ne(&[]);
3644
}

tests/ui/comparison_to_empty.stderr

+25-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,29 @@ error: comparison to empty slice
5555
LL | && s == []
5656
| ^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`
5757

58-
error: aborting due to 9 previous errors
58+
error: comparison to empty slice
59+
--> tests/ui/comparison_to_empty.rs:39:13
60+
|
61+
LL | let _ = s.eq("");
62+
| ^^^^^^^^ help: using `is_empty` is clearer and more explicit: `s.is_empty()`
63+
64+
error: comparison to empty slice
65+
--> tests/ui/comparison_to_empty.rs:40:13
66+
|
67+
LL | let _ = s.ne("");
68+
| ^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!s.is_empty()`
69+
70+
error: comparison to empty slice
71+
--> tests/ui/comparison_to_empty.rs:42:13
72+
|
73+
LL | let _ = v.eq(&[]);
74+
| ^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `v.is_empty()`
75+
76+
error: comparison to empty slice
77+
--> tests/ui/comparison_to_empty.rs:43:13
78+
|
79+
LL | let _ = v.ne(&[]);
80+
| ^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!v.is_empty()`
81+
82+
error: aborting due to 13 previous errors
5983

0 commit comments

Comments
 (0)