Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c79a7ed

Browse files
committed
Introduce AssocOp::Binary.
It mirrors `ExprKind::Binary`, and contains a `BinOpKind`. This makes `AssocOp` more like `ExprKind`. Note that the variants removed from `AssocOp` are all named differently to `BinOpToken`, e.g. `Multiply` instead of `Mul`, so that's an inconsistency removed. The commit adds `precedence` and `fixity` methods to `BinOpKind`, and calls them from the corresponding methods in `AssocOp`. This avoids the need to create an `AssocOp` from a `BinOpKind` in a bunch of places, and `AssocOp::from_ast_binop` is removed. `AssocOp::to_ast_binop` is also no longer needed. Overall things are shorter and nicer.
1 parent 8f001ea commit c79a7ed

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

clippy_lints/src/operators/float_equality_without_abs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn check<'tcx>(
5050
// format the suggestion
5151
let suggestion = format!(
5252
"{}.abs()",
53-
sugg::make_assoc(AssocOp::Subtract, &sug_l, &sug_r).maybe_par()
53+
sugg::make_assoc(AssocOp::Binary(BinOpKind::Sub), &sug_l, &sug_r).maybe_par()
5454
);
5555
// spans the lint
5656
span_lint_and_then(

clippy_utils/src/sugg.rs

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'a> Sugg<'a> {
160160
Sugg::BinOp(AssocOp::AssignOp(op.node), get_snippet(lhs.span), get_snippet(rhs.span))
161161
},
162162
ExprKind::Binary(op, lhs, rhs) => Sugg::BinOp(
163-
AssocOp::from_ast_binop(op.node),
163+
AssocOp::Binary(op.node),
164164
get_snippet(lhs.span),
165165
get_snippet(rhs.span),
166166
),
@@ -249,7 +249,7 @@ impl<'a> Sugg<'a> {
249249
snippet(rhs.span),
250250
),
251251
ast::ExprKind::Binary(op, ref lhs, ref rhs) => Sugg::BinOp(
252-
AssocOp::from_ast_binop(op.node),
252+
AssocOp::Binary(op.node),
253253
snippet(lhs.span),
254254
snippet(rhs.span),
255255
),
@@ -366,30 +366,9 @@ impl<'a> Sugg<'a> {
366366
/// Generates a string from the operator and both sides.
367367
fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
368368
match op {
369-
AssocOp::Add
370-
| AssocOp::Subtract
371-
| AssocOp::Multiply
372-
| AssocOp::Divide
373-
| AssocOp::Modulus
374-
| AssocOp::LAnd
375-
| AssocOp::LOr
376-
| AssocOp::BitXor
377-
| AssocOp::BitAnd
378-
| AssocOp::BitOr
379-
| AssocOp::ShiftLeft
380-
| AssocOp::ShiftRight
381-
| AssocOp::Equal
382-
| AssocOp::Less
383-
| AssocOp::LessEqual
384-
| AssocOp::NotEqual
385-
| AssocOp::Greater
386-
| AssocOp::GreaterEqual => {
387-
format!("{lhs} {} {rhs}", op.to_ast_binop().expect("Those are AST ops").as_str())
388-
},
369+
AssocOp::Binary(op) => format!("{lhs} {} {rhs}", op.as_str()),
389370
AssocOp::Assign => format!("{lhs} = {rhs}"),
390-
AssocOp::AssignOp(op) => {
391-
format!("{lhs} {}= {rhs}", op.as_str())
392-
},
371+
AssocOp::AssignOp(op) => format!("{lhs} {}= {rhs}", op.as_str()),
393372
AssocOp::As => format!("{lhs} as {rhs}"),
394373
AssocOp::DotDot => format!("{lhs}..{rhs}"),
395374
AssocOp::DotDotEq => format!("{lhs}..={rhs}"),
@@ -476,16 +455,17 @@ impl Neg for Sugg<'_> {
476455
impl<'a> Not for Sugg<'a> {
477456
type Output = Sugg<'a>;
478457
fn not(self) -> Sugg<'a> {
479-
use AssocOp::{Equal, Greater, GreaterEqual, Less, LessEqual, NotEqual};
458+
use AssocOp::Binary;
459+
use ast::BinOpKind::{Eq, Gt, Ge, Lt, Le, Ne};
480460

481461
if let Sugg::BinOp(op, lhs, rhs) = self {
482462
let to_op = match op {
483-
Equal => NotEqual,
484-
NotEqual => Equal,
485-
Less => GreaterEqual,
486-
GreaterEqual => Less,
487-
Greater => LessEqual,
488-
LessEqual => Greater,
463+
Binary(Eq) => Binary(Ne),
464+
Binary(Ne) => Binary(Eq),
465+
Binary(Lt) => Binary(Ge),
466+
Binary(Ge) => Binary(Lt),
467+
Binary(Gt) => Binary(Le),
468+
Binary(Le) => Binary(Gt),
489469
_ => return make_unop("!", Sugg::BinOp(op, lhs, rhs)),
490470
};
491471
Sugg::BinOp(to_op, lhs, rhs)
@@ -537,15 +517,21 @@ pub fn make_unop(op: &str, expr: Sugg<'_>) -> Sugg<'static> {
537517
pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> {
538518
/// Returns `true` if the operator is a shift operator `<<` or `>>`.
539519
fn is_shift(op: AssocOp) -> bool {
540-
matches!(op, AssocOp::ShiftLeft | AssocOp::ShiftRight)
520+
matches!(op, AssocOp::Binary(ast::BinOpKind::Shl | ast::BinOpKind::Shr))
541521
}
542522

543523
/// Returns `true` if the operator is an arithmetic operator
544524
/// (i.e., `+`, `-`, `*`, `/`, `%`).
545525
fn is_arith(op: AssocOp) -> bool {
546526
matches!(
547527
op,
548-
AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide | AssocOp::Modulus
528+
AssocOp::Binary(
529+
ast::BinOpKind::Add
530+
| ast::BinOpKind::Sub
531+
| ast::BinOpKind::Mul
532+
| ast::BinOpKind::Div
533+
| ast::BinOpKind::Rem
534+
)
549535
)
550536
}
551537

@@ -577,9 +563,9 @@ pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static>
577563
Sugg::BinOp(op, lhs.into(), rhs.into())
578564
}
579565

580-
/// Convenience wrapper around `make_assoc` and `AssocOp::from_ast_binop`.
566+
/// Convenience wrapper around `make_assoc` and `AssocOp::Binary`.
581567
pub fn make_binop(op: ast::BinOpKind, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> {
582-
make_assoc(AssocOp::from_ast_binop(op), lhs, rhs)
568+
make_assoc(AssocOp::Binary(op), lhs, rhs)
583569
}
584570

585571
#[derive(PartialEq, Eq, Clone, Copy)]
@@ -604,16 +590,15 @@ enum Associativity {
604590
/// associative.
605591
#[must_use]
606592
fn associativity(op: AssocOp) -> Associativity {
607-
use rustc_ast::util::parser::AssocOp::{
608-
Add, As, Assign, AssignOp, BitAnd, BitOr, BitXor, Divide, DotDot, DotDotEq, Equal, Greater, GreaterEqual, LAnd,
609-
LOr, Less, LessEqual, Modulus, Multiply, NotEqual, ShiftLeft, ShiftRight, Subtract,
593+
use rustc_ast::util::parser::AssocOp::{As, Assign, AssignOp, Binary, DotDot, DotDotEq};
594+
use ast::BinOpKind::{
595+
Add, BitAnd, BitOr, BitXor, Div, Eq, Gt, Ge, And, Or, Lt, Le, Rem, Mul, Ne, Shl, Shr, Sub,
610596
};
611597

612598
match op {
613599
Assign | AssignOp(_) => Associativity::Right,
614-
Add | BitAnd | BitOr | BitXor | LAnd | LOr | Multiply | As => Associativity::Both,
615-
Divide | Equal | Greater | GreaterEqual | Less | LessEqual | Modulus | NotEqual | ShiftLeft | ShiftRight
616-
| Subtract => Associativity::Left,
600+
Binary(Add | BitAnd | BitOr | BitXor | And | Or | Mul) | As => Associativity::Both,
601+
Binary(Div | Eq | Gt | Ge | Lt | Le | Rem | Ne | Shl | Shr | Sub) => Associativity::Left,
617602
DotDot | DotDotEq => Associativity::None,
618603
}
619604
}

0 commit comments

Comments
 (0)