diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_1.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_1.py new file mode 100644 index 0000000000..68487f8c8f --- /dev/null +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_1.py @@ -0,0 +1,5 @@ +# Docstring followed by a newline + +def foobar(foor, bar={}): + """ + """ diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_2.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_2.py new file mode 100644 index 0000000000..e723231f4c --- /dev/null +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_2.py @@ -0,0 +1,6 @@ +# Docstring followed by whitespace with no newline +# Regression test for https://github.com/astral-sh/ruff/issues/7155 + +def foobar(foor, bar={}): + """ + """ \ No newline at end of file diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_3.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_3.py new file mode 100644 index 0000000000..1106f17036 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_3.py @@ -0,0 +1,6 @@ +# Docstring with no newline + + +def foobar(foor, bar={}): + """ + """ \ No newline at end of file diff --git a/crates/ruff/src/rules/flake8_bugbear/mod.rs b/crates/ruff/src/rules/flake8_bugbear/mod.rs index 717bb1ee3a..71ec3b130a 100644 --- a/crates/ruff/src/rules/flake8_bugbear/mod.rs +++ b/crates/ruff/src/rules/flake8_bugbear/mod.rs @@ -33,6 +33,9 @@ mod tests { #[test_case(Rule::JumpStatementInFinally, Path::new("B012.py"))] #[test_case(Rule::LoopVariableOverridesIterator, Path::new("B020.py"))] #[test_case(Rule::MutableArgumentDefault, Path::new("B006_B008.py"))] + #[test_case(Rule::MutableArgumentDefault, Path::new("B006_1.py"))] + #[test_case(Rule::MutableArgumentDefault, Path::new("B006_2.py"))] + #[test_case(Rule::MutableArgumentDefault, Path::new("B006_3.py"))] #[test_case(Rule::NoExplicitStacklevel, Path::new("B028.py"))] #[test_case(Rule::RaiseLiteral, Path::new("B016.py"))] #[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"))] diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/mutable_argument_default.rs b/crates/ruff/src/rules/flake8_bugbear/rules/mutable_argument_default.rs index d71715eb4c..c1a34be60c 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/mutable_argument_default.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/mutable_argument_default.rs @@ -175,7 +175,7 @@ fn move_initialization( return None; } Edit::insertion(content, locator.line_start(statement.start())) - } else if statement.end() == locator.text_len() { + } else if locator.full_line_end(statement.end()) == locator.text_len() { // If the statement is at the end of the file, without a trailing newline, insert // _after_ it with an extra newline. Edit::insertion( diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_1.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_1.py.snap new file mode 100644 index 0000000000..092572890d --- /dev/null +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_1.py.snap @@ -0,0 +1,26 @@ +--- +source: crates/ruff/src/rules/flake8_bugbear/mod.rs +--- +B006_1.py:3:22: B006 [*] Do not use mutable data structures for argument defaults + | +1 | # Docstring followed by a newline +2 | +3 | def foobar(foor, bar={}): + | ^^ B006 +4 | """ +5 | """ + | + = help: Replace with `None`; initialize within function + +ℹ Possible fix +1 1 | # Docstring followed by a newline +2 2 | +3 |-def foobar(foor, bar={}): + 3 |+def foobar(foor, bar=None): +4 4 | """ +5 5 | """ + 6 |+ + 7 |+ if bar is None: + 8 |+ bar = {} + + diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_2.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_2.py.snap new file mode 100644 index 0000000000..1e34f109fb --- /dev/null +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_2.py.snap @@ -0,0 +1,27 @@ +--- +source: crates/ruff/src/rules/flake8_bugbear/mod.rs +--- +B006_2.py:4:22: B006 [*] Do not use mutable data structures for argument defaults + | +2 | # Regression test for https://github.com/astral-sh/ruff/issues/7155 +3 | +4 | def foobar(foor, bar={}): + | ^^ B006 +5 | """ +6 | """ + | + = help: Replace with `None`; initialize within function + +ℹ Possible fix +1 1 | # Docstring followed by whitespace with no newline +2 2 | # Regression test for https://github.com/astral-sh/ruff/issues/7155 +3 3 | +4 |-def foobar(foor, bar={}): + 4 |+def foobar(foor, bar=None): +5 5 | """ +6 |- """ + 6 |+ """ + 7 |+ if bar is None: + 8 |+ bar = {} + + diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_3.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_3.py.snap new file mode 100644 index 0000000000..037d2d67cf --- /dev/null +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_3.py.snap @@ -0,0 +1,25 @@ +--- +source: crates/ruff/src/rules/flake8_bugbear/mod.rs +--- +B006_3.py:4:22: B006 [*] Do not use mutable data structures for argument defaults + | +4 | def foobar(foor, bar={}): + | ^^ B006 +5 | """ +6 | """ + | + = help: Replace with `None`; initialize within function + +ℹ Possible fix +1 1 | # Docstring with no newline +2 2 | +3 3 | +4 |-def foobar(foor, bar={}): + 4 |+def foobar(foor, bar=None): + 5 |+ """ +5 6 | """ +6 |- """ + 7 |+ if bar is None: + 8 |+ bar = {} + +