diff --git a/src/rules/flake8_bugbear/rules/assert_false.rs b/src/rules/flake8_bugbear/rules/assert_false.rs index f356d9592d..a832448f0a 100644 --- a/src/rules/flake8_bugbear/rules/assert_false.rs +++ b/src/rules/flake8_bugbear/rules/assert_false.rs @@ -1,10 +1,10 @@ use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind}; +use crate::ast::helpers::unparse_stmt; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::Diagnostic; -use crate::source_code::Generator; use crate::violations; fn assertion_error(msg: Option<&Expr>) -> Stmt { @@ -48,10 +48,8 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option let mut diagnostic = Diagnostic::new(violations::DoNotAssertFalse, Range::from_located(test)); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_stmt(&assertion_error(msg)); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_stmt(&assertion_error(msg), checker.stylist), stmt.location, stmt.end_location.unwrap(), )); diff --git a/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs b/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs index 497e16e376..dc95630079 100644 --- a/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs +++ b/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs @@ -3,11 +3,11 @@ use rustc_hash::{FxHashMap, FxHashSet}; use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Location}; use crate::ast::helpers; +use crate::ast::helpers::unparse_expr; use crate::ast::types::{CallPath, Range}; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::{Diagnostic, Rule}; -use crate::source_code::Generator; use crate::violations; fn type_pattern(elts: Vec<&Expr>) -> Expr { @@ -59,14 +59,12 @@ fn duplicate_handler_exceptions<'a>( Range::from_located(expr), ); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - if unique_elts.len() == 1 { - generator.unparse_expr(unique_elts[0], 0); - } else { - generator.unparse_expr(&type_pattern(unique_elts), 0); - } diagnostic.amend(Fix::replacement( - generator.generate(), + if unique_elts.len() == 1 { + unparse_expr(unique_elts[0], checker.stylist) + } else { + unparse_expr(&type_pattern(unique_elts), checker.stylist) + }, expr.location, expr.end_location.unwrap(), )); diff --git a/src/rules/flake8_bugbear/rules/getattr_with_constant.rs b/src/rules/flake8_bugbear/rules/getattr_with_constant.rs index a96895b829..ae68e7145b 100644 --- a/src/rules/flake8_bugbear/rules/getattr_with_constant.rs +++ b/src/rules/flake8_bugbear/rules/getattr_with_constant.rs @@ -1,12 +1,12 @@ use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location}; +use crate::ast::helpers::unparse_expr; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::python::identifiers::is_identifier; use crate::python::keyword::KWLIST; use crate::registry::Diagnostic; -use crate::source_code::Generator; use crate::violations; fn attribute(value: &Expr, attr: &str) -> Expr { @@ -48,10 +48,8 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar let mut diagnostic = Diagnostic::new(violations::GetAttrWithConstant, Range::from_located(expr)); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(&attribute(obj, value), 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(&attribute(obj, value), checker.stylist), expr.location, expr.end_location.unwrap(), )); diff --git a/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs b/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs index 479d88c7a0..abf63b3dcb 100644 --- a/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs +++ b/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs @@ -5,7 +5,6 @@ use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::Diagnostic; -use crate::source_code::Generator; use crate::violations; /// B013 @@ -25,10 +24,8 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E Range::from_located(type_), ); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(elt, 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(elt, checker.stylist), type_.location, type_.end_location.unwrap(), )); diff --git a/src/rules/flake8_bugbear/rules/setattr_with_constant.rs b/src/rules/flake8_bugbear/rules/setattr_with_constant.rs index 179e35562a..8f63b90b90 100644 --- a/src/rules/flake8_bugbear/rules/setattr_with_constant.rs +++ b/src/rules/flake8_bugbear/rules/setattr_with_constant.rs @@ -1,12 +1,13 @@ use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind}; +use crate::ast::helpers::unparse_stmt; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::python::identifiers::is_identifier; use crate::python::keyword::KWLIST; use crate::registry::Diagnostic; -use crate::source_code::{Generator, Stylist}; +use crate::source_code::Stylist; use crate::violations; fn assignment(obj: &Expr, name: &str, value: &Expr, stylist: &Stylist) -> String { @@ -27,9 +28,7 @@ fn assignment(obj: &Expr, name: &str, value: &Expr, stylist: &Stylist) -> String type_comment: None, }, ); - let mut generator: Generator = stylist.into(); - generator.unparse_stmt(&stmt); - generator.generate() + unparse_stmt(&stmt, stylist) } /// B010 diff --git a/src/rules/flake8_pytest_style/rules/parametrize.rs b/src/rules/flake8_pytest_style/rules/parametrize.rs index ae4b2d5edb..6dfe3911e4 100644 --- a/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -2,7 +2,7 @@ use rustpython_ast::{Constant, Expr, ExprContext, ExprKind}; use super::super::types; use super::helpers::{is_pytest_parametrize, split_names}; -use crate::ast::helpers::create_expr; +use crate::ast::helpers::{create_expr, unparse_expr}; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; @@ -31,8 +31,7 @@ fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option { return None; } - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr( + Some(unparse_expr( &create_expr(ExprKind::Constant { value: Constant::Str(elts.iter().fold(String::new(), |mut acc, elt| { if let ExprKind::Constant { @@ -49,9 +48,8 @@ fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option { })), kind: None, }), - 0, - ); - Some(generator.generate()) + checker.stylist, + )) } /// PT006 @@ -102,24 +100,22 @@ fn check_names(checker: &mut Checker, expr: &Expr) { Range::from_located(expr), ); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr( - &create_expr(ExprKind::List { - elts: names - .iter() - .map(|&name| { - create_expr(ExprKind::Constant { - value: Constant::Str(name.to_string()), - kind: None, - }) - }) - .collect(), - ctx: ExprContext::Load, - }), - 0, - ); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr( + &create_expr(ExprKind::List { + elts: names + .iter() + .map(|&name| { + create_expr(ExprKind::Constant { + value: Constant::Str(name.to_string()), + kind: None, + }) + }) + .collect(), + ctx: ExprContext::Load, + }), + checker.stylist, + ), expr.location, expr.end_location.unwrap(), )); @@ -144,16 +140,14 @@ fn check_names(checker: &mut Checker, expr: &Expr) { Range::from_located(expr), ); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr( - &create_expr(ExprKind::List { - elts: elts.clone(), - ctx: ExprContext::Load, - }), - 0, - ); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr( + &create_expr(ExprKind::List { + elts: elts.clone(), + ctx: ExprContext::Load, + }), + checker.stylist, + ), expr.location, expr.end_location.unwrap(), )); @@ -285,10 +279,8 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) { ); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(&create_expr(value.node.clone()), 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(&create_expr(value.node.clone()), checker.stylist), expr.location, expr.end_location.unwrap(), )); diff --git a/src/rules/flake8_simplify/rules/ast_for.rs b/src/rules/flake8_simplify/rules/ast_for.rs index 5526b3e78e..549786f280 100644 --- a/src/rules/flake8_simplify/rules/ast_for.rs +++ b/src/rules/flake8_simplify/rules/ast_for.rs @@ -2,12 +2,12 @@ use rustpython_ast::{ Comprehension, Constant, Expr, ExprContext, ExprKind, Stmt, StmtKind, Unaryop, }; -use crate::ast::helpers::{create_expr, create_stmt}; +use crate::ast::helpers::{create_expr, create_stmt, unparse_stmt}; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::{Diagnostic, Rule}; -use crate::source_code::{Generator, Stylist}; +use crate::source_code::Stylist; use crate::violations; struct Loop<'a> { @@ -147,26 +147,27 @@ fn return_values_for_siblings<'a>(stmt: &'a Stmt, sibling: &'a Stmt) -> Option String { - let mut generator: Generator = stylist.into(); - generator.unparse_stmt(&create_stmt(StmtKind::Return { - value: Some(Box::new(create_expr(ExprKind::Call { - func: Box::new(create_expr(ExprKind::Name { - id: id.to_string(), - ctx: ExprContext::Load, - })), - args: vec![create_expr(ExprKind::GeneratorExp { - elt: Box::new(test.clone()), - generators: vec![Comprehension { - target: target.clone(), - iter: iter.clone(), - ifs: vec![], - is_async: 0, - }], - })], - keywords: vec![], - }))), - })); - generator.generate() + unparse_stmt( + &create_stmt(StmtKind::Return { + value: Some(Box::new(create_expr(ExprKind::Call { + func: Box::new(create_expr(ExprKind::Name { + id: id.to_string(), + ctx: ExprContext::Load, + })), + args: vec![create_expr(ExprKind::GeneratorExp { + elt: Box::new(test.clone()), + generators: vec![Comprehension { + target: target.clone(), + iter: iter.clone(), + ifs: vec![], + is_async: 0, + }], + })], + keywords: vec![], + }))), + }), + stylist, + ) } /// SIM110, SIM111 diff --git a/src/rules/pycodestyle/rules/do_not_assign_lambda.rs b/src/rules/pycodestyle/rules/do_not_assign_lambda.rs index b47eb131cb..16a39d1298 100644 --- a/src/rules/pycodestyle/rules/do_not_assign_lambda.rs +++ b/src/rules/pycodestyle/rules/do_not_assign_lambda.rs @@ -1,13 +1,13 @@ use rustpython_ast::{Arguments, Location, Stmt, StmtKind}; use rustpython_parser::ast::{Expr, ExprKind}; -use crate::ast::helpers::{match_leading_content, match_trailing_content}; +use crate::ast::helpers::{match_leading_content, match_trailing_content, unparse_stmt}; use crate::ast::types::Range; use crate::ast::whitespace::leading_space; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::Diagnostic; -use crate::source_code::{Generator, Stylist}; +use crate::source_code::Stylist; use crate::violations; /// E731 @@ -72,7 +72,5 @@ fn function(name: &str, args: &Arguments, body: &Expr, stylist: &Stylist) -> Str type_comment: None, }, ); - let mut generator: Generator = stylist.into(); - generator.unparse_stmt(&func); - generator.generate() + unparse_stmt(&func, stylist) } diff --git a/src/rules/pyupgrade/rules/remove_six_compat.rs b/src/rules/pyupgrade/rules/remove_six_compat.rs index 501b9fdfcb..7d6167f57e 100644 --- a/src/rules/pyupgrade/rules/remove_six_compat.rs +++ b/src/rules/pyupgrade/rules/remove_six_compat.rs @@ -1,11 +1,11 @@ use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Keyword, StmtKind}; -use crate::ast::helpers::{create_expr, create_stmt}; +use crate::ast::helpers::{create_expr, create_stmt, unparse_expr, unparse_stmt}; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::{Diagnostic, Rule}; -use crate::source_code::{Generator, Locator, Stylist}; +use crate::source_code::{Locator, Stylist}; use crate::violations; /// Return `true` if the call path is a reference to `${module}.${any}`. @@ -130,10 +130,8 @@ fn replace_call_on_arg_by_arg_method_call( fn replace_by_expr_kind(node: ExprKind, expr: &Expr, patch: bool, stylist: &Stylist) -> Diagnostic { let mut diagnostic = Diagnostic::new(violations::RemoveSixCompat, Range::from_located(expr)); if patch { - let mut generator: Generator = stylist.into(); - generator.unparse_expr(&create_expr(node), 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(&create_expr(node), stylist), expr.location, expr.end_location.unwrap(), )); @@ -144,10 +142,8 @@ fn replace_by_expr_kind(node: ExprKind, expr: &Expr, patch: bool, stylist: &Styl fn replace_by_stmt_kind(node: StmtKind, expr: &Expr, patch: bool, stylist: &Stylist) -> Diagnostic { let mut diagnostic = Diagnostic::new(violations::RemoveSixCompat, Range::from_located(expr)); if patch { - let mut generator: Generator = stylist.into(); - generator.unparse_stmt(&create_stmt(node)); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_stmt(&create_stmt(node), stylist), expr.location, expr.end_location.unwrap(), )); diff --git a/src/rules/pyupgrade/rules/use_pep604_annotation.rs b/src/rules/pyupgrade/rules/use_pep604_annotation.rs index 689e8ab26d..7ab5371d88 100644 --- a/src/rules/pyupgrade/rules/use_pep604_annotation.rs +++ b/src/rules/pyupgrade/rules/use_pep604_annotation.rs @@ -1,10 +1,10 @@ use rustpython_ast::{Constant, Expr, ExprKind, Location, Operator}; +use crate::ast::helpers::unparse_expr; use crate::ast::types::Range; use crate::checkers::ast::Checker; use crate::fix::Fix; use crate::registry::Diagnostic; -use crate::source_code::Generator; use crate::violations; fn optional(expr: &Expr) -> Expr { @@ -83,10 +83,8 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s let mut diagnostic = Diagnostic::new(violations::UsePEP604Annotation, Range::from_located(expr)); if checker.patch(diagnostic.kind.rule()) { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(&optional(slice), 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(&optional(slice), checker.stylist), expr.location, expr.end_location.unwrap(), )); @@ -102,20 +100,16 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s // Invalid type annotation. } ExprKind::Tuple { elts, .. } => { - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(&union(elts), 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(&union(elts), checker.stylist), expr.location, expr.end_location.unwrap(), )); } _ => { // Single argument. - let mut generator: Generator = checker.stylist.into(); - generator.unparse_expr(slice, 0); diagnostic.amend(Fix::replacement( - generator.generate(), + unparse_expr(slice, checker.stylist), expr.location, expr.end_location.unwrap(), ));