diff --git a/src/flake8_simplify/rules/ast_for.rs b/src/flake8_simplify/rules/ast_for.rs index faae275ead..e10c7c84e2 100644 --- a/src/flake8_simplify/rules/ast_for.rs +++ b/src/flake8_simplify/rules/ast_for.rs @@ -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(), )); diff --git a/src/flake8_simplify/rules/ast_if.rs b/src/flake8_simplify/rules/ast_if.rs index 08aa58e7a5..2e8776c558 100644 --- a/src/flake8_simplify/rules/ast_if.rs +++ b/src/flake8_simplify/rules/ast_if.rs @@ -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; }