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`
This commit is contained in:
Dhruv Manilawala 2023-10-30 14:17:44 +05:30 committed by GitHub
parent b5a4a9a356
commit b0dc5a86a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 44 deletions

View file

@ -326,9 +326,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.map(|name| { .map(|name| {
Expr::StringLiteral(ast::ExprStringLiteral { Expr::StringLiteral(ast::ExprStringLiteral {
value: (*name).to_string(), value: (*name).to_string(),
unicode: false, ..ast::ExprStringLiteral::default()
implicit_concatenated: false,
range: TextRange::default(),
}) })
}) })
.collect(), .collect(),
@ -361,9 +359,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) {
.map(|name| { .map(|name| {
Expr::StringLiteral(ast::ExprStringLiteral { Expr::StringLiteral(ast::ExprStringLiteral {
value: (*name).to_string(), value: (*name).to_string(),
unicode: false, ..ast::ExprStringLiteral::default()
implicit_concatenated: false,
range: TextRange::default(),
}) })
}) })
.collect(), .collect(),

View file

@ -1,5 +1,5 @@
use ruff_python_ast::{self as ast, Arguments, Expr}; 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 crate::fix::snippet::SourceCodeSnippet;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, FixAvailability, Violation}; 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 { let node = ast::ExprStringLiteral {
value: capital_env_var, value: capital_env_var,
unicode: *unicode, unicode: *unicode,
implicit_concatenated: false, ..ast::ExprStringLiteral::default()
range: TextRange::default(),
}; };
let new_env_var = node.into(); let new_env_var = node.into();
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(

View file

@ -17,9 +17,7 @@ fn to_formatted_value_expr(inner: &Expr) -> Expr {
pub(super) fn to_constant_string(s: &str) -> Expr { pub(super) fn to_constant_string(s: &str) -> Expr {
let node = ast::ExprStringLiteral { let node = ast::ExprStringLiteral {
value: s.to_owned(), value: s.to_owned(),
unicode: false, ..ast::ExprStringLiteral::default()
implicit_concatenated: false,
range: TextRange::default(),
}; };
node.into() node.into()
} }

View file

@ -35,19 +35,8 @@ impl FromStr for LiteralType {
impl LiteralType { impl LiteralType {
fn as_zero_value_expr(self) -> Expr { fn as_zero_value_expr(self) -> Expr {
match self { match self {
LiteralType::Str => ast::ExprStringLiteral { LiteralType::Str => ast::ExprStringLiteral::default().into(),
value: String::new(), LiteralType::Bytes => ast::ExprBytesLiteral::default().into(),
unicode: false,
implicit_concatenated: false,
range: TextRange::default(),
}
.into(),
LiteralType::Bytes => ast::ExprBytesLiteral {
value: Vec::new(),
implicit_concatenated: false,
range: TextRange::default(),
}
.into(),
LiteralType::Int => ast::ExprNumberLiteral { LiteralType::Int => ast::ExprNumberLiteral {
value: ast::Number::Int(0.into()), value: ast::Number::Int(0.into()),
range: TextRange::default(), range: TextRange::default(),
@ -58,11 +47,7 @@ impl LiteralType {
range: TextRange::default(), range: TextRange::default(),
} }
.into(), .into(),
LiteralType::Bool => ast::ExprBooleanLiteral { LiteralType::Bool => ast::ExprBooleanLiteral::default().into(),
value: false,
range: TextRange::default(),
}
.into(),
} }
} }
} }

View file

@ -131,9 +131,7 @@ fn optional(expr: &Expr) -> Expr {
ast::ExprBinOp { ast::ExprBinOp {
left: Box::new(expr.clone()), left: Box::new(expr.clone()),
op: Operator::BitOr, op: Operator::BitOr,
right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral { right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral::default())),
range: TextRange::default(),
})),
range: TextRange::default(), range: TextRange::default(),
} }
.into() .into()

View file

@ -130,9 +130,7 @@ fn generate_replacement(name: &str, generator: Generator) -> String {
let compare = ast::ExprCompare { let compare = ast::ExprCompare {
left: Box::new(var.into()), left: Box::new(var.into()),
ops: vec![ast::CmpOp::Is], ops: vec![ast::CmpOp::Is],
comparators: vec![ast::Expr::NoneLiteral(ast::ExprNoneLiteral { comparators: vec![ast::Expr::NoneLiteral(ast::ExprNoneLiteral::default())],
range: TextRange::default(),
})],
range: TextRange::default(), range: TextRange::default(),
}; };
generator.expr(&compare.into()) generator.expr(&compare.into())

View file

@ -127,9 +127,7 @@ fn generate_fix(checker: &Checker, conversion_type: ConversionType, expr: &Expr)
let new_expr = Expr::BinOp(ast::ExprBinOp { let new_expr = Expr::BinOp(ast::ExprBinOp {
left: Box::new(expr.clone()), left: Box::new(expr.clone()),
op: Operator::BitOr, op: Operator::BitOr,
right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral { right: Box::new(Expr::NoneLiteral(ast::ExprNoneLiteral::default())),
range: TextRange::default(),
})),
range: TextRange::default(), range: TextRange::default(),
}); });
let content = checker.generator().expr(&new_expr); let content = checker.generator().expr(&new_expr);

View file

@ -987,7 +987,7 @@ impl From<ExprFString> for Expr {
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ExprStringLiteral { pub struct ExprStringLiteral {
pub range: TextRange, pub range: TextRange,
pub value: String, pub value: String,
@ -1015,7 +1015,7 @@ impl Deref for ExprStringLiteral {
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ExprBytesLiteral { pub struct ExprBytesLiteral {
pub range: TextRange, pub range: TextRange,
pub value: Vec<u8>, pub value: Vec<u8>,
@ -1059,7 +1059,7 @@ pub enum Number {
Complex { real: f64, imag: f64 }, Complex { real: f64, imag: f64 },
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ExprBooleanLiteral { pub struct ExprBooleanLiteral {
pub range: TextRange, pub range: TextRange,
pub value: bool, pub value: bool,
@ -1077,7 +1077,7 @@ impl Ranged for ExprBooleanLiteral {
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ExprNoneLiteral { pub struct ExprNoneLiteral {
pub range: TextRange, pub range: TextRange,
} }
@ -1094,7 +1094,7 @@ impl Ranged for ExprNoneLiteral {
} }
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct ExprEllipsisLiteral { pub struct ExprEllipsisLiteral {
pub range: TextRange, pub range: TextRange,
} }