Avoid silently dropping code generator errors (#1598)

This commit is contained in:
Charlie Marsh 2023-01-03 08:11:18 -05:00 committed by GitHub
parent 68fbd0f029
commit b9e92affb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 53 deletions

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind};
use crate::ast::types::Range;
@ -53,13 +54,16 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option
checker.style.line_ending(),
);
generator.unparse_stmt(&assertion_error(msg));
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
stmt.location,
stmt.end_location.unwrap(),
));
}
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
stmt.location,
stmt.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite `assert False`: {e}"),
};
}
checker.add_check(check);
}

View File

@ -1,4 +1,5 @@
use itertools::Itertools;
use log::error;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Location};
@ -64,12 +65,15 @@ fn duplicate_handler_exceptions<'a>(
} else {
generator.unparse_expr(&type_pattern(unique_elts), 0);
}
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to remove duplicate exceptions: {e}"),
}
}
checker.add_check(check);

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location};
use crate::ast::types::Range;
@ -52,12 +53,15 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
checker.style.line_ending(),
);
generator.unparse_expr(&attribute(obj, value), 0);
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite `getattr`: {e}"),
}
}
checker.add_check(check);

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind};
use crate::ast::types::Range;
@ -29,12 +30,15 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E
checker.style.line_ending(),
);
generator.unparse_expr(elt, 0);
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
type_.location,
type_.end_location.unwrap(),
));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
type_.location,
type_.end_location.unwrap(),
));
}
Err(e) => error!("Failed to remove redundant tuple: {e}"),
}
}
checker.add_check(check);

View File

@ -1,5 +1,6 @@
use std::string::ToString;
use log::error;
use rustc_hash::FxHashSet;
use rustpython_ast::{Keyword, KeywordData};
use rustpython_parser::ast::{Constant, Expr, ExprKind};
@ -115,9 +116,11 @@ pub(crate) fn percent_format_extra_named_arguments(
location,
);
if checker.patch(check.kind.code()) {
if let Ok(fix) = remove_unused_format_arguments_from_dict(&missing, right, checker.locator)
{
check.amend(fix);
match remove_unused_format_arguments_from_dict(&missing, right, checker.locator) {
Ok(fix) => {
check.amend(fix);
}
Err(e) => error!("Failed to remove unused format arguments: {e}"),
}
}
checker.add_check(check);
@ -272,10 +275,12 @@ pub(crate) fn string_dot_format_extra_named_arguments(
location,
);
if checker.patch(check.kind.code()) {
if let Ok(fix) =
remove_unused_keyword_arguments_from_format_call(&missing, location, checker.locator)
match remove_unused_keyword_arguments_from_format_call(&missing, location, checker.locator)
{
check.amend(fix);
Ok(fix) => {
check.amend(fix);
}
Err(e) => error!("Failed to remove unused keyword arguments: {e}"),
}
}
checker.add_check(check);

View File

@ -193,8 +193,8 @@ pub fn convert_named_tuple_functional_to_class(
return;
};
match match_defaults(keywords) {
Ok(defaults) => {
if let Ok(properties) = create_properties_from_args(args, defaults) {
Ok(defaults) => match create_properties_from_args(args, defaults) {
Ok(properties) => {
let mut check = Check::new(
CheckKind::ConvertNamedTupleFunctionalToClass(typename.to_string()),
Range::from_located(stmt),
@ -209,7 +209,8 @@ pub fn convert_named_tuple_functional_to_class(
}
checker.add_check(check);
}
}
Err(err) => error!("Failed to create properties: {err}"),
},
Err(err) => error!("Failed to parse defaults: {err}"),
}
}

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprKind, Location, Operator};
use crate::ast::helpers::{collect_call_paths, dealias_call_path};
@ -71,13 +72,16 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s
checker.style.line_ending(),
);
generator.unparse_expr(&optional(slice), 0);
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite PEP604 annotation: {e}"),
};
}
checker.add_check(check);
} else if checker.match_typing_call_path(&call_path, "Union") {
@ -94,12 +98,15 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s
checker.style.line_ending(),
);
generator.unparse_expr(&union(elts), 0);
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite PEP604 annotation: {e}"),
}
}
_ => {
@ -110,12 +117,15 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s
checker.style.line_ending(),
);
generator.unparse_expr(slice, 0);
if let Ok(content) = generator.generate() {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite PEP604 annotation: {e}"),
}
}
}