diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py index 4b8f8b925b..77ab80b7ee 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py @@ -308,3 +308,7 @@ def single_line_func_wrong(value: dict[str, str] = { def single_line_func_wrong(value: dict[str, str] = {}) \ : \ """Docstring""" + + +def single_line_func_wrong(value: dict[str, str] = {}): + """Docstring without newline""" \ No newline at end of file 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 bf3c0fc531..d71715eb4c 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,8 +175,15 @@ fn move_initialization( return None; } Edit::insertion(content, locator.line_start(statement.start())) + } else if 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( + format!("{}{}", stylist.line_ending().as_str(), content), + locator.full_line_end(statement.end()), + ) } else { - // If the docstring is the only statement, insert _before_ it. + // If the docstring is the only statement, insert _after_ it. Edit::insertion(content, locator.full_line_end(statement.end())) } } else { diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap index b98badffde..05c860d523 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap @@ -476,4 +476,23 @@ B006_B008.py:308:52: B006 Do not use mutable data structures for argument defaul | = help: Replace with `None`; initialize within function +B006_B008.py:313:52: B006 [*] Do not use mutable data structures for argument defaults + | +313 | def single_line_func_wrong(value: dict[str, str] = {}): + | ^^ B006 +314 | """Docstring without newline""" + | + = help: Replace with `None`; initialize within function + +ℹ Possible fix +310 310 | """Docstring""" +311 311 | +312 312 | +313 |-def single_line_func_wrong(value: dict[str, str] = {}): +314 |- """Docstring without newline""" + 313 |+def single_line_func_wrong(value: dict[str, str] = None): + 314 |+ """Docstring without newline""" + 315 |+ if value is None: + 316 |+ value = {} +