Skip to content

Commit

Permalink
Handle when pos is equal to line length
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro committed Sep 25, 2023
1 parent cd17b8c commit d8df26e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Docstring followed by whitespace with no newline
# Regression test for https://github.com/astral-sh/ruff/issues/7155

def foobar(foor, bar={}):
import os
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Docstring with no newline


def foobar(foor, bar={}):
import os
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ mod tests {
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_3.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_4.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_5.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_6.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_7.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"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ fn move_initialization(
} else if statement.is_import_stmt() || statement.is_import_from_stmt() {
// If the statement in the function is an import, insert _after_ it.
pos = locator.full_line_end(statement.end());
if pos == locator.text_len() {
// If the statement is at the end of the file, without a trailing newline, insert
// _after_ it with an extra newline.
content = format!("{}{}", stylist.line_ending().as_str(), content);
break;
}
} else {
break;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ B006_5.py:35:59: B006 [*] Do not use mutable data structures for argument defaul
35 |+def import_docstring_module_wrong(value: dict[str, str] = None):
36 36 | """Docstring"""
37 37 | import os
38 |+ if value is None:
39 |+ value = {}
38 |+
39 |+ if value is None:
40 |+ value = {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B006_6.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 | import os
|
= 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={}):
5 |- import os
4 |+def foobar(foor, bar=None):
5 |+ import os
6 |+ if bar is None:
7 |+ bar = {}


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B006_7.py:4:22: B006 [*] Do not use mutable data structures for argument defaults
|
4 | def foobar(foor, bar={}):
| ^^ B006
5 | import os
|
= help: Replace with `None`; initialize within function

ℹ Possible fix
1 1 | # Docstring with no newline
2 2 |
3 3 |
4 |-def foobar(foor, bar={}):
5 |- import os
4 |+def foobar(foor, bar=None):
5 |+ import os
6 |+ if bar is None:
7 |+ bar = {}


0 comments on commit d8df26e

Please sign in to comment.