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

Commit 8f001ea

Browse files
committed
In AssocOp::AssignOp, use BinOpKind instead of BinOpToken
`AssocOp::AssignOp` contains a `BinOpToken`. `ExprKind::AssignOp` contains a `BinOpKind`. Given that `AssocOp` is basically a cut-down version of `ExprKind`, it makes sense to make `AssocOp` more like `ExprKind`. Especially given that `AssocOp` and `BinOpKind` use semantic operation names (e.g. `Mul`, `Div`), but `BinOpToken` uses syntactic names (e.g. `Star`, `Slash`). This results in more concise code, and removes the need for various conversions. (Note that the removed functions `hirbinop2assignop` and `astbinop2assignop` are semantically identical, because `hir::BinOp` is just a synonum for `ast::BinOp`!) The only downside to this is that it allows the possibility of some nonsensical combinations, such as `AssocOp::AssignOp(BinOpKind::Lt)`. But `ExprKind::AssignOp` already has that problem. The problem can be fixed for both types in the future with some effort, by introducing an `AssignOpKind` type.
1 parent 17bda0c commit 8f001ea

File tree

2 files changed

+4
-55
lines changed

2 files changed

+4
-55
lines changed

clippy_utils/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
3232
extern crate rustc_abi;
3333
extern crate rustc_ast;
34-
extern crate rustc_ast_pretty;
3534
extern crate rustc_attr_parsing;
3635
extern crate rustc_const_eval;
3736
extern crate rustc_data_structures;

clippy_utils/src/sugg.rs

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::source::{snippet, snippet_opt, snippet_with_applicability, snippet_wi
55
use crate::ty::expr_sig;
66
use crate::{get_parent_expr_for_hir, higher};
77
use rustc_ast::util::parser::AssocOp;
8-
use rustc_ast::{ast, token};
9-
use rustc_ast_pretty::pprust::token_kind_to_string;
8+
use rustc_ast::ast;
109
use rustc_errors::Applicability;
1110
use rustc_hir as hir;
1211
use rustc_hir::{Closure, ExprKind, HirId, MutTy, TyKind};
@@ -158,7 +157,7 @@ impl<'a> Sugg<'a> {
158157
Sugg::BinOp(AssocOp::Assign, get_snippet(lhs.span), get_snippet(rhs.span))
159158
},
160159
ExprKind::AssignOp(op, lhs, rhs) => {
161-
Sugg::BinOp(hirbinop2assignop(op), get_snippet(lhs.span), get_snippet(rhs.span))
160+
Sugg::BinOp(AssocOp::AssignOp(op.node), get_snippet(lhs.span), get_snippet(rhs.span))
162161
},
163162
ExprKind::Binary(op, lhs, rhs) => Sugg::BinOp(
164163
AssocOp::from_ast_binop(op.node),
@@ -245,7 +244,7 @@ impl<'a> Sugg<'a> {
245244
snippet(rhs.span),
246245
),
247246
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => Sugg::BinOp(
248-
astbinop2assignop(op),
247+
AssocOp::AssignOp(op.node),
249248
snippet(lhs.span),
250249
snippet(rhs.span),
251250
),
@@ -389,7 +388,7 @@ fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
389388
},
390389
AssocOp::Assign => format!("{lhs} = {rhs}"),
391390
AssocOp::AssignOp(op) => {
392-
format!("{lhs} {}= {rhs}", token_kind_to_string(&token::BinOp(op)))
391+
format!("{lhs} {}= {rhs}", op.as_str())
393392
},
394393
AssocOp::As => format!("{lhs} as {rhs}"),
395394
AssocOp::DotDot => format!("{lhs}..{rhs}"),
@@ -619,55 +618,6 @@ fn associativity(op: AssocOp) -> Associativity {
619618
}
620619
}
621620

622-
/// Converts a `hir::BinOp` to the corresponding assigning binary operator.
623-
fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
624-
use rustc_ast::token::BinOpToken::{And, Caret, Minus, Or, Percent, Plus, Shl, Shr, Slash, Star};
625-
626-
AssocOp::AssignOp(match op.node {
627-
hir::BinOpKind::Add => Plus,
628-
hir::BinOpKind::BitAnd => And,
629-
hir::BinOpKind::BitOr => Or,
630-
hir::BinOpKind::BitXor => Caret,
631-
hir::BinOpKind::Div => Slash,
632-
hir::BinOpKind::Mul => Star,
633-
hir::BinOpKind::Rem => Percent,
634-
hir::BinOpKind::Shl => Shl,
635-
hir::BinOpKind::Shr => Shr,
636-
hir::BinOpKind::Sub => Minus,
637-
638-
hir::BinOpKind::And
639-
| hir::BinOpKind::Eq
640-
| hir::BinOpKind::Ge
641-
| hir::BinOpKind::Gt
642-
| hir::BinOpKind::Le
643-
| hir::BinOpKind::Lt
644-
| hir::BinOpKind::Ne
645-
| hir::BinOpKind::Or => panic!("This operator does not exist"),
646-
})
647-
}
648-
649-
/// Converts an `ast::BinOp` to the corresponding assigning binary operator.
650-
fn astbinop2assignop(op: ast::BinOp) -> AssocOp {
651-
use rustc_ast::ast::BinOpKind::{
652-
Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub,
653-
};
654-
use rustc_ast::token::BinOpToken;
655-
656-
AssocOp::AssignOp(match op.node {
657-
Add => BinOpToken::Plus,
658-
BitAnd => BinOpToken::And,
659-
BitOr => BinOpToken::Or,
660-
BitXor => BinOpToken::Caret,
661-
Div => BinOpToken::Slash,
662-
Mul => BinOpToken::Star,
663-
Rem => BinOpToken::Percent,
664-
Shl => BinOpToken::Shl,
665-
Shr => BinOpToken::Shr,
666-
Sub => BinOpToken::Minus,
667-
And | Eq | Ge | Gt | Le | Lt | Ne | Or => panic!("This operator does not exist"),
668-
})
669-
}
670-
671621
/// Returns the indentation before `span` if there are nothing but `[ \t]`
672622
/// before it on its line.
673623
fn indentation<T: LintContext>(cx: &T, span: Span) -> Option<String> {

0 commit comments

Comments
 (0)