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 rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind};
use crate::ast::types::Range; 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(), checker.style.line_ending(),
); );
generator.unparse_stmt(&assertion_error(msg)); generator.unparse_stmt(&assertion_error(msg));
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
stmt.location, content,
stmt.end_location.unwrap(), stmt.location,
)); stmt.end_location.unwrap(),
} ));
}
Err(e) => error!("Failed to rewrite `assert False`: {e}"),
};
} }
checker.add_check(check); checker.add_check(check);
} }

View File

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

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location}; use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location};
use crate::ast::types::Range; 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(), checker.style.line_ending(),
); );
generator.unparse_expr(&attribute(obj, value), 0); generator.unparse_expr(&attribute(obj, value), 0);
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
expr.location, content,
expr.end_location.unwrap(), expr.location,
)); expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite `getattr`: {e}"),
} }
} }
checker.add_check(check); checker.add_check(check);

View File

@ -1,3 +1,4 @@
use log::error;
use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind}; use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind};
use crate::ast::types::Range; 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(), checker.style.line_ending(),
); );
generator.unparse_expr(elt, 0); generator.unparse_expr(elt, 0);
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
type_.location, content,
type_.end_location.unwrap(), type_.location,
)); type_.end_location.unwrap(),
));
}
Err(e) => error!("Failed to remove redundant tuple: {e}"),
} }
} }
checker.add_check(check); checker.add_check(check);

View File

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

View File

@ -193,8 +193,8 @@ pub fn convert_named_tuple_functional_to_class(
return; return;
}; };
match match_defaults(keywords) { match match_defaults(keywords) {
Ok(defaults) => { Ok(defaults) => match create_properties_from_args(args, defaults) {
if let Ok(properties) = create_properties_from_args(args, defaults) { Ok(properties) => {
let mut check = Check::new( let mut check = Check::new(
CheckKind::ConvertNamedTupleFunctionalToClass(typename.to_string()), CheckKind::ConvertNamedTupleFunctionalToClass(typename.to_string()),
Range::from_located(stmt), Range::from_located(stmt),
@ -209,7 +209,8 @@ pub fn convert_named_tuple_functional_to_class(
} }
checker.add_check(check); checker.add_check(check);
} }
} Err(err) => error!("Failed to create properties: {err}"),
},
Err(err) => error!("Failed to parse defaults: {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 rustpython_ast::{Constant, Expr, ExprKind, Location, Operator};
use crate::ast::helpers::{collect_call_paths, dealias_call_path}; 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(), checker.style.line_ending(),
); );
generator.unparse_expr(&optional(slice), 0); generator.unparse_expr(&optional(slice), 0);
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
expr.location, content,
expr.end_location.unwrap(), expr.location,
)); expr.end_location.unwrap(),
} ));
}
Err(e) => error!("Failed to rewrite PEP604 annotation: {e}"),
};
} }
checker.add_check(check); checker.add_check(check);
} else if checker.match_typing_call_path(&call_path, "Union") { } 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(), checker.style.line_ending(),
); );
generator.unparse_expr(&union(elts), 0); generator.unparse_expr(&union(elts), 0);
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
expr.location, content,
expr.end_location.unwrap(), 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(), checker.style.line_ending(),
); );
generator.unparse_expr(slice, 0); generator.unparse_expr(slice, 0);
if let Ok(content) = generator.generate() { match generator.generate() {
check.amend(Fix::replacement( Ok(content) => {
content, check.amend(Fix::replacement(
expr.location, content,
expr.end_location.unwrap(), expr.location,
)); expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite PEP604 annotation: {e}"),
} }
} }
} }