mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:53 +00:00
Implement as_str
& Display
for all operator enums (#10691)
## Summary
This PR adds the `as_str` implementation for all the operator methods.
It already exists for `CmpOp` which is being [used in the
linter](ffcd77860c/crates/ruff_linter/src/rules/flake8_simplify/rules/key_in_dict.rs (L117)
)
and it makes sense to implement it for the rest as well. This will also
be utilized in error messages for the new parser.
This commit is contained in:
parent
eee2d5b915
commit
99dd3a8ab0
1 changed files with 81 additions and 17 deletions
|
@ -2735,6 +2735,21 @@ pub enum BoolOp {
|
||||||
Or,
|
Or,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl BoolOp {
|
||||||
|
pub const fn as_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
BoolOp::And => "and",
|
||||||
|
BoolOp::Or => "or",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for BoolOp {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str(self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// See also [operator](https://docs.python.org/3/library/ast.html#ast.operator)
|
/// See also [operator](https://docs.python.org/3/library/ast.html#ast.operator)
|
||||||
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
||||||
pub enum Operator {
|
pub enum Operator {
|
||||||
|
@ -2753,6 +2768,32 @@ pub enum Operator {
|
||||||
FloorDiv,
|
FloorDiv,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Operator {
|
||||||
|
pub const fn as_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Operator::Add => "+",
|
||||||
|
Operator::Sub => "-",
|
||||||
|
Operator::Mult => "*",
|
||||||
|
Operator::MatMult => "@",
|
||||||
|
Operator::Div => "/",
|
||||||
|
Operator::Mod => "%",
|
||||||
|
Operator::Pow => "**",
|
||||||
|
Operator::LShift => "<<",
|
||||||
|
Operator::RShift => ">>",
|
||||||
|
Operator::BitOr => "|",
|
||||||
|
Operator::BitXor => "^",
|
||||||
|
Operator::BitAnd => "&",
|
||||||
|
Operator::FloorDiv => "//",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Operator {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str(self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// See also [unaryop](https://docs.python.org/3/library/ast.html#ast.unaryop)
|
/// See also [unaryop](https://docs.python.org/3/library/ast.html#ast.unaryop)
|
||||||
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
||||||
pub enum UnaryOp {
|
pub enum UnaryOp {
|
||||||
|
@ -2762,6 +2803,23 @@ pub enum UnaryOp {
|
||||||
USub,
|
USub,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UnaryOp {
|
||||||
|
pub const fn as_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
UnaryOp::Invert => "~",
|
||||||
|
UnaryOp::Not => "not",
|
||||||
|
UnaryOp::UAdd => "+",
|
||||||
|
UnaryOp::USub => "-",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for UnaryOp {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str(self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// See also [cmpop](https://docs.python.org/3/library/ast.html#ast.cmpop)
|
/// See also [cmpop](https://docs.python.org/3/library/ast.html#ast.cmpop)
|
||||||
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
|
||||||
pub enum CmpOp {
|
pub enum CmpOp {
|
||||||
|
@ -2777,6 +2835,29 @@ pub enum CmpOp {
|
||||||
NotIn,
|
NotIn,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CmpOp {
|
||||||
|
pub const fn as_str(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
CmpOp::Eq => "==",
|
||||||
|
CmpOp::NotEq => "!=",
|
||||||
|
CmpOp::Lt => "<",
|
||||||
|
CmpOp::LtE => "<=",
|
||||||
|
CmpOp::Gt => ">",
|
||||||
|
CmpOp::GtE => ">=",
|
||||||
|
CmpOp::Is => "is",
|
||||||
|
CmpOp::IsNot => "is not",
|
||||||
|
CmpOp::In => "in",
|
||||||
|
CmpOp::NotIn => "not in",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for CmpOp {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
f.write_str(self.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// See also [comprehension](https://docs.python.org/3/library/ast.html#ast.comprehension)
|
/// See also [comprehension](https://docs.python.org/3/library/ast.html#ast.comprehension)
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Comprehension {
|
pub struct Comprehension {
|
||||||
|
@ -3282,23 +3363,6 @@ impl Deref for TypeParams {
|
||||||
|
|
||||||
pub type Suite = Vec<Stmt>;
|
pub type Suite = Vec<Stmt>;
|
||||||
|
|
||||||
impl CmpOp {
|
|
||||||
pub fn as_str(&self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
CmpOp::Eq => "==",
|
|
||||||
CmpOp::NotEq => "!=",
|
|
||||||
CmpOp::Lt => "<",
|
|
||||||
CmpOp::LtE => "<=",
|
|
||||||
CmpOp::Gt => ">",
|
|
||||||
CmpOp::GtE => ">=",
|
|
||||||
CmpOp::Is => "is",
|
|
||||||
CmpOp::IsNot => "is not",
|
|
||||||
CmpOp::In => "in",
|
|
||||||
CmpOp::NotIn => "not in",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parameters {
|
impl Parameters {
|
||||||
pub fn empty(range: TextRange) -> Self {
|
pub fn empty(range: TextRange) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue