Specialize ConversionFlag (#4450)

This commit is contained in:
Jeong, YunWon 2023-05-17 01:00:13 +09:00 committed by GitHub
parent f0465bf106
commit 4b05ca1198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 27 deletions

15
Cargo.lock generated
View file

@ -2001,7 +2001,7 @@ dependencies = [
[[package]] [[package]]
name = "ruff_text_size" name = "ruff_text_size"
version = "0.0.0" version = "0.0.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"schemars", "schemars",
"serde", "serde",
@ -2072,7 +2072,7 @@ dependencies = [
[[package]] [[package]]
name = "rustpython-ast" name = "rustpython-ast"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"is-macro", "is-macro",
"num-bigint", "num-bigint",
@ -2083,7 +2083,7 @@ dependencies = [
[[package]] [[package]]
name = "rustpython-format" name = "rustpython-format"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"bitflags 2.2.1", "bitflags 2.2.1",
"itertools", "itertools",
@ -2095,9 +2095,10 @@ dependencies = [
[[package]] [[package]]
name = "rustpython-literal" name = "rustpython-literal"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"hexf-parse", "hexf-parse",
"is-macro",
"lexical-parse-float", "lexical-parse-float",
"num-traits", "num-traits",
"unic-ucd-category", "unic-ucd-category",
@ -2106,9 +2107,10 @@ dependencies = [
[[package]] [[package]]
name = "rustpython-parser" name = "rustpython-parser"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"is-macro",
"itertools", "itertools",
"lalrpop-util", "lalrpop-util",
"log", "log",
@ -2128,8 +2130,9 @@ dependencies = [
[[package]] [[package]]
name = "rustpython-parser-core" name = "rustpython-parser-core"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee" source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [ dependencies = [
"is-macro",
"ruff_text_size", "ruff_text_size",
] ]

View file

@ -31,10 +31,10 @@ proc-macro2 = { version = "1.0.51" }
quote = { version = "1.0.23" } quote = { version = "1.0.23" }
regex = { version = "1.7.1" } regex = { version = "1.7.1" }
rustc-hash = { version = "1.1.0" } rustc-hash = { version = "1.1.0" }
ruff_text_size = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" } ruff_text_size = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-format = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" } rustpython-format = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" } rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] } rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
schemars = { version = "0.8.12" } schemars = { version = "0.8.12" }
serde = { version = "1.0.152", features = ["derive"] } serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93", features = ["preserve_order"] } serde_json = { version = "1.0.93", features = ["preserve_order"] }

View file

@ -1,11 +1,11 @@
use ruff_text_size::TextRange; use ruff_text_size::TextRange;
use rustpython_parser::ast::{self, Constant, Expr, Int}; use rustpython_parser::ast::{self, Constant, ConversionFlag, Expr};
/// Wrap an expression in a `FormattedValue` with no special formatting. /// Wrap an expression in a `FormattedValue` with no special formatting.
fn to_formatted_value_expr(inner: &Expr) -> Expr { fn to_formatted_value_expr(inner: &Expr) -> Expr {
let node = ast::ExprFormattedValue { let node = ast::ExprFormattedValue {
value: Box::new(inner.clone()), value: Box::new(inner.clone()),
conversion: Int::new(0), conversion: ConversionFlag::None,
format_spec: None, format_spec: None,
range: TextRange::default(), range: TextRange::default(),
}; };

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{self, Expr, Ranged}; use rustpython_parser::ast::{self, ConversionFlag, Expr, Ranged};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
@ -43,10 +43,10 @@ impl AlwaysAutofixableViolation for ExplicitFStringTypeConversion {
pub(crate) fn explicit_f_string_type_conversion( pub(crate) fn explicit_f_string_type_conversion(
checker: &mut Checker, checker: &mut Checker,
formatted_value: &Expr, formatted_value: &Expr,
conversion: ast::Int, conversion: ConversionFlag,
) { ) {
// Skip if there's already a conversion flag. // Skip if there's already a conversion flag.
if conversion != ast::ConversionFlag::None as u32 { if !conversion.is_none() {
return; return;
} }

View file

@ -3,8 +3,9 @@
use num_bigint::BigInt; use num_bigint::BigInt;
use rustpython_parser::ast::{ use rustpython_parser::ast::{
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr, self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
ExprContext, Identifier, Int, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem, Excepthandler, Expr, ExprContext, Identifier, Int, Keyword, MatchCase, Operator, Pattern, Stmt,
Unaryop, Withitem,
}; };
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
@ -481,7 +482,7 @@ pub enum ComparableExpr<'a> {
}, },
FormattedValue { FormattedValue {
value: Box<ComparableExpr<'a>>, value: Box<ComparableExpr<'a>>,
conversion: Int, conversion: ConversionFlag,
format_spec: Option<Box<ComparableExpr<'a>>>, format_spec: Option<Box<ComparableExpr<'a>>>,
}, },
JoinedStr { JoinedStr {

View file

@ -5,7 +5,7 @@ use std::ops::Deref;
use rustpython_literal::escape::{AsciiEscape, Escape, UnicodeEscape}; use rustpython_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
use rustpython_parser::ast::{ use rustpython_parser::ast::{
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag, self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
Excepthandler, Expr, Identifier, Int, MatchCase, Operator, Pattern, Stmt, Suite, Withitem, Excepthandler, Expr, Identifier, MatchCase, Operator, Pattern, Stmt, Suite, Withitem,
}; };
use crate::newlines::LineEnding; use crate::newlines::LineEnding;
@ -1340,7 +1340,12 @@ impl<'a> Generator<'a> {
} }
} }
fn unparse_formatted<U>(&mut self, val: &Expr<U>, conversion: Int, spec: Option<&Expr<U>>) { fn unparse_formatted<U>(
&mut self,
val: &Expr<U>,
conversion: ConversionFlag,
spec: Option<&Expr<U>>,
) {
let mut generator = Generator::new(self.indent, self.quote, self.line_ending); let mut generator = Generator::new(self.indent, self.quote, self.line_ending);
generator.unparse_expr(val, precedence::FORMATTED_VALUE); generator.unparse_expr(val, precedence::FORMATTED_VALUE);
let brace = if generator.buffer.starts_with('{') { let brace = if generator.buffer.starts_with('{') {
@ -1352,10 +1357,10 @@ impl<'a> Generator<'a> {
self.p(brace); self.p(brace);
self.buffer += &generator.buffer; self.buffer += &generator.buffer;
if conversion.to_u32() != ConversionFlag::None as u32 { if !conversion.is_none() {
self.p("!"); self.p("!");
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
self.p(&format!("{}", conversion.to_u32() as u8 as char)); self.p(&format!("{}", conversion as u8 as char));
} }
if let Some(spec) = spec { if let Some(spec) = spec {

View file

@ -5,7 +5,7 @@ use std::ops::Deref;
use itertools::Itertools; use itertools::Itertools;
use ruff_text_size::{TextRange, TextSize}; use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::{Constant, Ranged}; use rustpython_parser::ast::{Constant, ConversionFlag, Ranged};
use rustpython_parser::{ast, Mode}; use rustpython_parser::{ast, Mode};
use ruff_python_ast::source_code::Locator; use ruff_python_ast::source_code::Locator;
@ -410,7 +410,7 @@ pub enum ExprKind {
}, },
FormattedValue { FormattedValue {
value: Box<Expr>, value: Box<Expr>,
conversion: usize, conversion: ConversionFlag,
format_spec: Option<Box<Expr>>, format_spec: Option<Box<Expr>>,
}, },
JoinedStr { JoinedStr {
@ -2038,7 +2038,7 @@ impl From<(rustpython_parser::ast::Expr, &Locator<'_>)> for Expr {
range, range,
node: ExprKind::FormattedValue { node: ExprKind::FormattedValue {
value: Box::new((*value, locator).into()), value: Box::new((*value, locator).into()),
conversion: conversion.to_usize(), conversion,
format_spec: format_spec.map(|f| Box::new((*f, locator).into())), format_spec: format_spec.map(|f| Box::new((*f, locator).into())),
}, },
trivia: vec![], trivia: vec![],

View file

@ -1,6 +1,6 @@
#![allow(unused_variables, clippy::too_many_arguments)] #![allow(unused_variables, clippy::too_many_arguments)]
use rustpython_parser::ast::Constant; use rustpython_parser::ast::{Constant, ConversionFlag};
use ruff_formatter::prelude::*; use ruff_formatter::prelude::*;
use ruff_formatter::{format_args, write}; use ruff_formatter::{format_args, write};
@ -246,7 +246,7 @@ fn format_formatted_value(
f: &mut Formatter<ASTFormatContext>, f: &mut Formatter<ASTFormatContext>,
expr: &Expr, expr: &Expr,
value: &Expr, value: &Expr,
_conversion: usize, _conversion: ConversionFlag,
format_spec: Option<&Expr>, format_spec: Option<&Expr>,
) -> FormatResult<()> { ) -> FormatResult<()> {
write!(f, [text("!")])?; write!(f, [text("!")])?;