Skip to content

Commit 41c6ede

Browse files
committed
Move DoubleComparison into Operators lint pass
1 parent 2cd0331 commit 41c6ede

File tree

7 files changed

+88
-101
lines changed

7 files changed

+88
-101
lines changed

clippy_lints/src/double_comparison.rs

Lines changed: 0 additions & 96 deletions
This file was deleted.

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
4747
LintId::of(disallowed_types::DISALLOWED_TYPES),
4848
LintId::of(doc::MISSING_SAFETY_DOC),
4949
LintId::of(doc::NEEDLESS_DOCTEST_MAIN),
50-
LintId::of(double_comparison::DOUBLE_COMPARISONS),
5150
LintId::of(double_parens::DOUBLE_PARENS),
5251
LintId::of(drop_forget_ref::DROP_COPY),
5352
LintId::of(drop_forget_ref::DROP_NON_DROP),
@@ -251,6 +250,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
251250
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
252251
LintId::of(operators::ASSIGN_OP_PATTERN),
253252
LintId::of(operators::BAD_BIT_MASK),
253+
LintId::of(operators::DOUBLE_COMPARISONS),
254254
LintId::of(operators::INEFFECTIVE_BIT_MASK),
255255
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
256256
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),

clippy_lints/src/lib.register_complexity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
99
LintId::of(casts::CHAR_LIT_AS_U8),
1010
LintId::of(casts::UNNECESSARY_CAST),
1111
LintId::of(derivable_impls::DERIVABLE_IMPLS),
12-
LintId::of(double_comparison::DOUBLE_COMPARISONS),
1312
LintId::of(double_parens::DOUBLE_PARENS),
1413
LintId::of(duration_subsec::DURATION_SUBSEC),
1514
LintId::of(explicit_write::EXPLICIT_WRITE),
@@ -68,6 +67,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
6867
LintId::of(neg_cmp_op_on_partial_ord::NEG_CMP_OP_ON_PARTIAL_ORD),
6968
LintId::of(no_effect::NO_EFFECT),
7069
LintId::of(no_effect::UNNECESSARY_OPERATION),
70+
LintId::of(operators::DOUBLE_COMPARISONS),
7171
LintId::of(overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
7272
LintId::of(partialeq_ne_impl::PARTIALEQ_NE_IMPL),
7373
LintId::of(precedence::PRECEDENCE),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ store.register_lints(&[
118118
doc::MISSING_SAFETY_DOC,
119119
doc::NEEDLESS_DOCTEST_MAIN,
120120
doc_link_with_quotes::DOC_LINK_WITH_QUOTES,
121-
double_comparison::DOUBLE_COMPARISONS,
122121
double_parens::DOUBLE_PARENS,
123122
drop_forget_ref::DROP_COPY,
124123
drop_forget_ref::DROP_NON_DROP,
@@ -420,6 +419,7 @@ store.register_lints(&[
420419
operators::ABSURD_EXTREME_COMPARISONS,
421420
operators::ASSIGN_OP_PATTERN,
422421
operators::BAD_BIT_MASK,
422+
operators::DOUBLE_COMPARISONS,
423423
operators::FLOAT_ARITHMETIC,
424424
operators::INEFFECTIVE_BIT_MASK,
425425
operators::INTEGER_ARITHMETIC,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ mod disallowed_script_idents;
206206
mod disallowed_types;
207207
mod doc;
208208
mod doc_link_with_quotes;
209-
mod double_comparison;
210209
mod double_parens;
211210
mod drop_forget_ref;
212211
mod duplicate_mod;
@@ -701,7 +700,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
701700
store.register_late_pass(|| Box::new(useless_conversion::UselessConversion::default()));
702701
store.register_late_pass(|| Box::new(implicit_hasher::ImplicitHasher));
703702
store.register_late_pass(|| Box::new(fallible_impl_from::FallibleImplFrom));
704-
store.register_late_pass(|| Box::new(double_comparison::DoubleComparisons));
705703
store.register_late_pass(|| Box::new(question_mark::QuestionMark));
706704
store.register_early_pass(|| Box::new(suspicious_operation_groupings::SuspiciousOperationGroupings));
707705
store.register_late_pass(|| Box::new(suspicious_trait_impl::SuspiciousImpl));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::eq_expr_value;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{BinOpKind, Expr, ExprKind};
6+
use rustc_lint::LateContext;
7+
use rustc_span::source_map::Span;
8+
9+
use super::DOUBLE_COMPARISONS;
10+
11+
#[expect(clippy::similar_names)]
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, op: BinOpKind, lhs: &'tcx Expr<'_>, rhs: &'tcx Expr<'_>, span: Span) {
13+
let (lkind, llhs, lrhs, rkind, rlhs, rrhs) = match (&lhs.kind, &rhs.kind) {
14+
(ExprKind::Binary(lb, llhs, lrhs), ExprKind::Binary(rb, rlhs, rrhs)) => {
15+
(lb.node, llhs, lrhs, rb.node, rlhs, rrhs)
16+
},
17+
_ => return,
18+
};
19+
if !(eq_expr_value(cx, llhs, rlhs) && eq_expr_value(cx, lrhs, rrhs)) {
20+
return;
21+
}
22+
macro_rules! lint_double_comparison {
23+
($op:tt) => {{
24+
let mut applicability = Applicability::MachineApplicable;
25+
let lhs_str = snippet_with_applicability(cx, llhs.span, "", &mut applicability);
26+
let rhs_str = snippet_with_applicability(cx, lrhs.span, "", &mut applicability);
27+
let sugg = format!("{} {} {}", lhs_str, stringify!($op), rhs_str);
28+
span_lint_and_sugg(
29+
cx,
30+
DOUBLE_COMPARISONS,
31+
span,
32+
"this binary expression can be simplified",
33+
"try",
34+
sugg,
35+
applicability,
36+
);
37+
}};
38+
}
39+
match (op, lkind, rkind) {
40+
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Lt) | (BinOpKind::Or, BinOpKind::Lt, BinOpKind::Eq) => {
41+
lint_double_comparison!(<=);
42+
},
43+
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Eq) => {
44+
lint_double_comparison!(>=);
45+
},
46+
(BinOpKind::Or, BinOpKind::Lt, BinOpKind::Gt) | (BinOpKind::Or, BinOpKind::Gt, BinOpKind::Lt) => {
47+
lint_double_comparison!(!=);
48+
},
49+
(BinOpKind::And, BinOpKind::Le, BinOpKind::Ge) | (BinOpKind::And, BinOpKind::Ge, BinOpKind::Le) => {
50+
lint_double_comparison!(==);
51+
},
52+
_ => (),
53+
};
54+
}

clippy_lints/src/operators/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod absurd_extreme_comparisons;
66
mod arithmetic;
77
mod assign_op_pattern;
88
mod bit_mask;
9+
mod double_comparison;
910
mod misrefactored_assign_op;
1011
mod verbose_bit_mask;
1112

@@ -236,6 +237,34 @@ declare_clippy_lint! {
236237
"expressions where a bit mask is less readable than the corresponding method call"
237238
}
238239

240+
declare_clippy_lint! {
241+
/// ### What it does
242+
/// Checks for double comparisons that could be simplified to a single expression.
243+
///
244+
///
245+
/// ### Why is this bad?
246+
/// Readability.
247+
///
248+
/// ### Example
249+
/// ```rust
250+
/// # let x = 1;
251+
/// # let y = 2;
252+
/// if x == y || x < y {}
253+
/// ```
254+
///
255+
/// Could be written as:
256+
///
257+
/// ```rust
258+
/// # let x = 1;
259+
/// # let y = 2;
260+
/// if x <= y {}
261+
/// ```
262+
#[clippy::version = "pre 1.29.0"]
263+
pub DOUBLE_COMPARISONS,
264+
complexity,
265+
"unnecessary double comparisons that can be simplified"
266+
}
267+
239268
pub struct Operators {
240269
arithmetic_context: arithmetic::Context,
241270
verbose_bit_mask_threshold: u64,
@@ -249,6 +278,7 @@ impl_lint_pass!(Operators => [
249278
BAD_BIT_MASK,
250279
INEFFECTIVE_BIT_MASK,
251280
VERBOSE_BIT_MASK,
281+
DOUBLE_COMPARISONS,
252282
]);
253283
impl Operators {
254284
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -268,6 +298,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
268298
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
269299
bit_mask::check(cx, e, op.node, lhs, rhs);
270300
verbose_bit_mask::check(cx, e, op.node, lhs, rhs, self.verbose_bit_mask_threshold);
301+
double_comparison::check(cx, op.node, lhs, rhs, e.span);
271302
},
272303
ExprKind::AssignOp(op, lhs, rhs) => {
273304
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)