From b0dc5a86a1f13beb5e4826c8d615e473ac7682a8 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 30 Oct 2023 14:17:44 +0530 Subject: [PATCH] Impl `Default` for `(String|Bytes|Boolean|None|Ellipsis)Literal` (#8341) ## Summary This PR adds `Default` for the following literal nodes: * `StringLiteral` * `BytesLiteral` * `BooleanLiteral` * `NoneLiteral` * `EllipsisLiteral` The implementation creates the zero value of the respective literal nodes in terms of the Python language. ## Test Plan `cargo test` --- .../flake8_pytest_style/rules/parametrize.rs | 8 ++----- .../rules/flake8_simplify/rules/ast_expr.rs | 5 ++--- crates/ruff_linter/src/rules/flynt/helpers.rs | 4 +--- .../rules/pyupgrade/rules/native_literals.rs | 21 +++---------------- .../pyupgrade/rules/use_pep604_annotation.rs | 4 +--- .../refurb/rules/isinstance_type_none.rs | 4 +--- .../src/rules/ruff/rules/implicit_optional.rs | 4 +--- crates/ruff_python_ast/src/nodes.rs | 10 ++++----- 8 files changed, 16 insertions(+), 44 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs index cd943e2434..5d494ed82e 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -326,9 +326,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { .map(|name| { Expr::StringLiteral(ast::ExprStringLiteral { value: (*name).to_string(), - unicode: false, - implicit_concatenated: false, - range: TextRange::default(), + ..ast::ExprStringLiteral::default() }) }) .collect(), @@ -361,9 +359,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { .map(|name| { Expr::StringLiteral(ast::ExprStringLiteral { value: (*name).to_string(), - unicode: false, - implicit_concatenated: false, - range: TextRange::default(), + ..ast::ExprStringLiteral::default() }) }) .collect(), diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs index 0a4cdb82c8..b0e85b4f27 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_expr.rs @@ -1,5 +1,5 @@ use ruff_python_ast::{self as ast, Arguments, Expr}; -use ruff_text_size::{Ranged, TextRange}; +use ruff_text_size::Ranged; use crate::fix::snippet::SourceCodeSnippet; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, FixAvailability, Violation}; @@ -219,8 +219,7 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) { let node = ast::ExprStringLiteral { value: capital_env_var, unicode: *unicode, - implicit_concatenated: false, - range: TextRange::default(), + ..ast::ExprStringLiteral::default() }; let new_env_var = node.into(); diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( diff --git a/crates/ruff_linter/src/rules/flynt/helpers.rs b/crates/ruff_linter/src/rules/flynt/helpers.rs index 685d90388b..1caa107c40 100644 --- a/crates/ruff_linter/src/rules/flynt/helpers.rs +++ b/crates/ruff_linter/src/rules/flynt/helpers.rs @@ -17,9 +17,7 @@ fn to_formatted_value_expr(inner: &Expr) -> Expr { pub(super) fn to_constant_string(s: &str) -> Expr { let node = ast::ExprStringLiteral { value: s.to_owned(), - unicode: false, - implicit_concatenated: false, - range: TextRange::default(), + ..ast::ExprStringLiteral::default() }; node.into() } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/native_literals.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/native_literals.rs index c5d96934d7..4828c46ebd 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/native_literals.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/native_literals.rs @@ -35,19 +35,8 @@ impl FromStr for LiteralType { impl LiteralType { fn as_zero_value_expr(self) -> Expr { match self { - LiteralType::Str => ast::ExprStringLiteral { - value: String::new(), - unicode: false, - implicit_concatenated: false, - range: TextRange::default(), - } - .into(), - LiteralType::Bytes => ast::ExprBytesLiteral { - value: Vec::new(), - implicit_concatenated: false, - range: TextRange::default(), - } - .into(), + LiteralType::Str => ast::ExprStringLiteral::default().into(), + LiteralType::Bytes => ast::ExprBytesLiteral::default().into(), LiteralType::Int => ast::ExprNumberLiteral { value: ast::Number::Int(0.into()), range: TextRange::default(), @@ -58,11 +47,7 @@ impl LiteralType { range: TextRange::default(), } .into(), - LiteralType::Bool => ast::ExprBooleanLiteral { - value: false, - range: TextRange::default(), - } - .into(), + LiteralType::Bool => ast::ExprBooleanLiteral::default().into(), } } } diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs index d3d0866c39..73f1075140 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_annotation.rs @@ -131,9 +131,7 @@ fn optional(expr: &Expr) -> Expr { ast::ExprBinOp { left: Box::new(expr.clone()), op: Operator::BitOr, - right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral { - range: TextRange::default(), - })), + right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral::default())), range: TextRange::default(), } .into() diff --git a/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs b/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs index 31822e17e8..65399c5b79 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/isinstance_type_none.rs @@ -130,9 +130,7 @@ fn generate_replacement(name: &str, generator: Generator) -> String { let compare = ast::ExprCompare { left: Box::new(var.into()), ops: vec![ast::CmpOp::Is], - comparators: vec![ast::Expr::NoneLiteral(ast::ExprNoneLiteral { - range: TextRange::default(), - })], + comparators: vec![ast::Expr::NoneLiteral(ast::ExprNoneLiteral::default())], range: TextRange::default(), }; generator.expr(&compare.into()) diff --git a/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs b/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs index 2a0634f819..0707c57bb0 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/implicit_optional.rs @@ -127,9 +127,7 @@ fn generate_fix(checker: &Checker, conversion_type: ConversionType, expr: &Expr) let new_expr = Expr::BinOp(ast::ExprBinOp { left: Box::new(expr.clone()), op: Operator::BitOr, - right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral { - range: TextRange::default(), - })), + right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral::default())), range: TextRange::default(), }); let content = checker.generator().expr(&new_expr); diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index f583f2e3e9..5e3eea5a5c 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -987,7 +987,7 @@ impl From for Expr { } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ExprStringLiteral { pub range: TextRange, pub value: String, @@ -1015,7 +1015,7 @@ impl Deref for ExprStringLiteral { } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ExprBytesLiteral { pub range: TextRange, pub value: Vec, @@ -1059,7 +1059,7 @@ pub enum Number { Complex { real: f64, imag: f64 }, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ExprBooleanLiteral { pub range: TextRange, pub value: bool, @@ -1077,7 +1077,7 @@ impl Ranged for ExprBooleanLiteral { } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ExprNoneLiteral { pub range: TextRange, } @@ -1094,7 +1094,7 @@ impl Ranged for ExprNoneLiteral { } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct ExprEllipsisLiteral { pub range: TextRange, }