Lint comparison to empty slice using PartialEq
methods
This commit is contained in:
parent
43e3384581
commit
acff511871
@ -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;
|
||||
@ -185,6 +185,19 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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(&[]);
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user