BinOp helper to detect assignment

This commit is contained in:
Jeremy Kolb 2020-01-04 15:54:31 -05:00
parent c15293e019
commit 5afb22e2b3
2 changed files with 19 additions and 11 deletions

View file

@ -56,17 +56,7 @@ enum FlipAction {
impl From<BinOp> for FlipAction { impl From<BinOp> for FlipAction {
fn from(op_kind: BinOp) -> Self { fn from(op_kind: BinOp) -> Self {
match op_kind { match op_kind {
BinOp::Assignment => FlipAction::DontFlip, kind if kind.is_assignment() => FlipAction::DontFlip,
BinOp::AddAssign => FlipAction::DontFlip,
BinOp::DivAssign => FlipAction::DontFlip,
BinOp::MulAssign => FlipAction::DontFlip,
BinOp::RemAssign => FlipAction::DontFlip,
BinOp::ShrAssign => FlipAction::DontFlip,
BinOp::ShlAssign => FlipAction::DontFlip,
BinOp::SubAssign => FlipAction::DontFlip,
BinOp::BitOrAssign => FlipAction::DontFlip,
BinOp::BitAndAssign => FlipAction::DontFlip,
BinOp::BitXorAssign => FlipAction::DontFlip,
BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"), BinOp::GreaterTest => FlipAction::FlipAndReplaceOp("<"),
BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="), BinOp::GreaterEqualTest => FlipAction::FlipAndReplaceOp("<="),
BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"), BinOp::LesserTest => FlipAction::FlipAndReplaceOp(">"),

View file

@ -126,6 +126,24 @@ pub enum BinOp {
BitXorAssign, BitXorAssign,
} }
impl BinOp {
pub fn is_assignment(&self) -> bool {
match *self {
BinOp::Assignment
| BinOp::AddAssign
| BinOp::DivAssign
| BinOp::MulAssign
| BinOp::RemAssign
| BinOp::ShrAssign
| BinOp::ShlAssign
| BinOp::SubAssign
| BinOp::BitOrAssign
| BinOp::BitAndAssign
| BinOp::BitXorAssign => true,
_ => false,
}
}
}
impl ast::BinExpr { impl ast::BinExpr {
pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> { pub fn op_details(&self) -> Option<(SyntaxToken, BinOp)> {
self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| { self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| {