mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 21:40:51 -05:00
Remove unnecessary manual Generator invocations (#2129)
This commit is contained in:
@@ -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(),
|
||||
));
|
||||
|
||||
@@ -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(),
|
||||
));
|
||||
|
||||
@@ -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(),
|
||||
));
|
||||
|
||||
@@ -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(),
|
||||
));
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<String> {
|
||||
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<String> {
|
||||
})),
|
||||
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(),
|
||||
));
|
||||
|
||||
@@ -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<L
|
||||
|
||||
/// Generate a return statement for an `any` or `all` builtin comprehension.
|
||||
fn return_stmt(id: &str, test: &Expr, target: &Expr, iter: &Expr, stylist: &Stylist) -> 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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
));
|
||||
|
||||
@@ -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(),
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user