[flake8-bugbear] Treat return as equivalent to break (B909) (#12646)

Closes https://github.com/astral-sh/ruff/issues/12640.
This commit is contained in:
Charlie Marsh 2024-08-02 18:14:17 -04:00 committed by GitHub
parent 3c1c3199d0
commit c858afe03a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 5 deletions

View file

@ -172,4 +172,10 @@ for i, elem in enumerate(some_list):
# should not error (dict)
for i, elem in enumerate(some_list):
some_list[elem] = 1
some_list[elem] = 1
# should not error
def func():
for elem in some_list:
if some_list.pop() == 2:
return

View file

@ -1,3 +1,6 @@
use std::collections::HashMap;
use std::fmt::Debug;
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};
@ -6,11 +9,9 @@ use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{
visitor::{self, Visitor},
Expr, ExprAttribute, ExprCall, ExprSubscript, ExprTuple, Stmt, StmtAssign, StmtAugAssign,
StmtBreak, StmtDelete, StmtFor, StmtIf,
StmtDelete, StmtFor, StmtIf,
};
use ruff_text_size::TextRange;
use std::collections::HashMap;
use std::fmt::Debug;
use crate::checkers::ast::Checker;
use crate::fix::snippet::SourceCodeSnippet;
@ -285,7 +286,7 @@ impl<'a> Visitor<'a> for LoopMutationsVisitor<'a> {
}
// On break, clear the mutations for the current branch.
Stmt::Break(StmtBreak { range: _ }) => {
Stmt::Break(_) | Stmt::Return(_) => {
if let Some(mutations) = self.mutations.get_mut(&self.branch) {
mutations.clear();
}