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 loop_info.return_value && !loop_info.next_return_value {
if checker.settings.enabled.contains(&RuleCode::SIM110) {
let content = return_stmt(
let contents = return_stmt(
"any",
loop_info.test,
loop_info.target,
loop_info.iter,
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(
violations::ConvertLoopToAny(content.clone()),
violations::ConvertLoopToAny(contents.clone()),
Range::from_located(stmt),
);
if checker.patch(&RuleCode::SIM110) {
diagnostic.amend(Fix::replacement(
content,
contents,
stmt.location,
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",
&test,
loop_info.target,
loop_info.iter,
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(
violations::ConvertLoopToAll(content.clone()),
violations::ConvertLoopToAll(contents.clone()),
Range::from_located(stmt),
);
if checker.patch(&RuleCode::SIM111) {
diagnostic.amend(Fix::replacement(
content,
contents,
stmt.location,
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 contents = unparse_stmt(&ternary, checker.style);
// Don't flag for simplified ternaries if the resulting expression would exceed
// the maximum line length.
// Don't flag if the resulting expression would exceed the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length {
return;
}
// Don't flag for simplified ternaries if the if-expression contains any
// comments.
// Don't flag if the statement expression contains any comments.
if has_comments(stmt, checker.locator) {
return;
}
@ -322,14 +320,12 @@ pub fn use_dict_get_with_default(
checker.style,
);
// Don't flag for simplified `dict.get` if the resulting expression would exceed
// the maximum line length.
// Don't flag if the resulting expression would exceed the maximum line length.
if stmt.location.column() + contents.len() > checker.settings.line_length {
return;
}
// Don't flag for simplified `dict.get` if the if-expression contains any
// comments.
// Don't flag if the statement expression contains any comments.
if has_comments(stmt, checker.locator) {
return;
}