Skip SIM110/SIM111 fixes that create long lines

This commit is contained in:
Charlie Marsh 2023-01-12 16:21:54 -05:00
parent e0fdc4c5e8
commit eaed08ae79
2 changed files with 22 additions and 14 deletions

View file

@ -112,20 +112,26 @@ pub fn convert_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling: &Stm
if let Some(loop_info) = return_values(stmt, sibling) { if let Some(loop_info) = return_values(stmt, sibling) {
if loop_info.return_value && !loop_info.next_return_value { if loop_info.return_value && !loop_info.next_return_value {
if checker.settings.enabled.contains(&RuleCode::SIM110) { if checker.settings.enabled.contains(&RuleCode::SIM110) {
let content = return_stmt( let contents = return_stmt(
"any", "any",
loop_info.test, loop_info.test,
loop_info.target, loop_info.target,
loop_info.iter, loop_info.iter,
checker.style, checker.style,
); );
// Don't flag if the resulting expression would exceed the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length {
return;
}
let mut diagnostic = Diagnostic::new( let mut diagnostic = Diagnostic::new(
violations::ConvertLoopToAny(content.clone()), violations::ConvertLoopToAny(contents.clone()),
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&RuleCode::SIM110) { if checker.patch(&RuleCode::SIM110) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
content, contents,
stmt.location, stmt.location,
sibling.end_location.unwrap(), sibling.end_location.unwrap(),
)); ));
@ -151,20 +157,26 @@ pub fn convert_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling: &Stm
}) })
} }
}; };
let content = return_stmt( let contents = return_stmt(
"all", "all",
&test, &test,
loop_info.target, loop_info.target,
loop_info.iter, loop_info.iter,
checker.style, checker.style,
); );
// Don't flag if the resulting expression would exceed the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length {
return;
}
let mut diagnostic = Diagnostic::new( let mut diagnostic = Diagnostic::new(
violations::ConvertLoopToAll(content.clone()), violations::ConvertLoopToAll(contents.clone()),
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&RuleCode::SIM111) { if checker.patch(&RuleCode::SIM111) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
content, contents,
stmt.location, stmt.location,
sibling.end_location.unwrap(), sibling.end_location.unwrap(),
)); ));

View file

@ -205,14 +205,12 @@ pub fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: Option<&
let ternary = ternary(target_var, body_value, test, orelse_value); let ternary = ternary(target_var, body_value, test, orelse_value);
let contents = unparse_stmt(&ternary, checker.style); let contents = unparse_stmt(&ternary, checker.style);
// Don't flag for simplified ternaries if the resulting expression would exceed // Don't flag if the resulting expression would exceed the maximum line length.
// the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length { if stmt.location.column() + contents.len() > checker.settings.line_length {
return; return;
} }
// Don't flag for simplified ternaries if the if-expression contains any // Don't flag if the statement expression contains any comments.
// comments.
if has_comments(stmt, checker.locator) { if has_comments(stmt, checker.locator) {
return; return;
} }
@ -322,14 +320,12 @@ pub fn use_dict_get_with_default(
checker.style, checker.style,
); );
// Don't flag for simplified `dict.get` if the resulting expression would exceed // Don't flag if the resulting expression would exceed the maximum line length.
// the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length { if stmt.location.column() + contents.len() > checker.settings.line_length {
return; return;
} }
// Don't flag for simplified `dict.get` if the if-expression contains any // Don't flag if the statement expression contains any comments.
// comments.
if has_comments(stmt, checker.locator) { if has_comments(stmt, checker.locator) {
return; return;
} }