Skip to content

Commit a8364f3

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 ac91805 commit a8364f3

File tree

4 files changed

+23
-77
lines changed

4 files changed

+23
-77
lines changed

compiler/rustc_ast/src/util/parser.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub enum AssocOp {
4646
GreaterEqual,
4747
/// `=`
4848
Assign,
49-
/// `?=` where ? is one of the BinOpToken
50-
AssignOp(BinOpToken),
49+
/// `?=` where ? is one of the assignable BinOps
50+
AssignOp(BinOpKind),
5151
/// `as`
5252
As,
5353
/// `..` range
@@ -71,18 +71,27 @@ impl AssocOp {
7171
pub fn from_token(t: &Token) -> Option<AssocOp> {
7272
use AssocOp::*;
7373
match t.kind {
74-
token::BinOpEq(k) => Some(AssignOp(k)),
7574
token::Eq => Some(Assign),
75+
token::BinOpEq(BinOpToken::Plus) => Some(AssignOp(BinOpKind::Add)),
76+
token::BinOpEq(BinOpToken::Minus) => Some(AssignOp(BinOpKind::Sub)),
77+
token::BinOpEq(BinOpToken::Star) => Some(AssignOp(BinOpKind::Mul)),
78+
token::BinOpEq(BinOpToken::Slash) => Some(AssignOp(BinOpKind::Div)),
79+
token::BinOpEq(BinOpToken::Percent) => Some(AssignOp(BinOpKind::Rem)),
80+
token::BinOpEq(BinOpToken::Caret) => Some(AssignOp(BinOpKind::BitXor)),
81+
token::BinOpEq(BinOpToken::And) => Some(AssignOp(BinOpKind::BitAnd)),
82+
token::BinOpEq(BinOpToken::Or) => Some(AssignOp(BinOpKind::BitOr)),
83+
token::BinOpEq(BinOpToken::Shl) => Some(AssignOp(BinOpKind::Shl)),
84+
token::BinOpEq(BinOpToken::Shr) => Some(AssignOp(BinOpKind::Shr)),
85+
token::BinOp(BinOpToken::Plus) => Some(Add),
86+
token::BinOp(BinOpToken::Minus) => Some(Subtract),
7687
token::BinOp(BinOpToken::Star) => Some(Multiply),
7788
token::BinOp(BinOpToken::Slash) => Some(Divide),
7889
token::BinOp(BinOpToken::Percent) => Some(Modulus),
79-
token::BinOp(BinOpToken::Plus) => Some(Add),
80-
token::BinOp(BinOpToken::Minus) => Some(Subtract),
81-
token::BinOp(BinOpToken::Shl) => Some(ShiftLeft),
82-
token::BinOp(BinOpToken::Shr) => Some(ShiftRight),
83-
token::BinOp(BinOpToken::And) => Some(BitAnd),
8490
token::BinOp(BinOpToken::Caret) => Some(BitXor),
91+
token::BinOp(BinOpToken::And) => Some(BitAnd),
8592
token::BinOp(BinOpToken::Or) => Some(BitOr),
93+
token::BinOp(BinOpToken::Shl) => Some(ShiftLeft),
94+
token::BinOp(BinOpToken::Shr) => Some(ShiftRight),
8695
token::Lt => Some(Less),
8796
token::Le => Some(LessEqual),
8897
token::Ge => Some(GreaterEqual),

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,7 @@ impl<'a> Parser<'a> {
313313
self.mk_expr(span, binary)
314314
}
315315
AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs, cur_op_span)),
316-
AssocOp::AssignOp(k) => {
317-
let aop = match k {
318-
token::Plus => BinOpKind::Add,
319-
token::Minus => BinOpKind::Sub,
320-
token::Star => BinOpKind::Mul,
321-
token::Slash => BinOpKind::Div,
322-
token::Percent => BinOpKind::Rem,
323-
token::Caret => BinOpKind::BitXor,
324-
token::And => BinOpKind::BitAnd,
325-
token::Or => BinOpKind::BitOr,
326-
token::Shl => BinOpKind::Shl,
327-
token::Shr => BinOpKind::Shr,
328-
};
316+
AssocOp::AssignOp(aop) => {
329317
let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs);
330318
self.mk_expr(span, aopexpr)
331319
}
@@ -395,7 +383,7 @@ impl<'a> Parser<'a> {
395383
AssocOp::ShiftRight
396384
| AssocOp::Greater
397385
| AssocOp::GreaterEqual
398-
| AssocOp::AssignOp(token::BinOpToken::Shr),
386+
| AssocOp::AssignOp(BinOpKind::Shr),
399387
),
400388
_,
401389
) if self.restrictions.contains(Restrictions::CONST_EXPR) => {

src/tools/clippy/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;

src/tools/clippy/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)