Skip to content

Commit

Permalink
Avoid fixing D200 for docstrings that end in escapes (#6779)
Browse files Browse the repository at this point in the history
Appease the fuzzers! Closes
#6755.
  • Loading branch information
charliermarsh authored Aug 22, 2023
1 parent 749da65 commit 558b56f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
13 changes: 13 additions & 0 deletions crates/ruff/resources/test/fixtures/pydocstyle/D200.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def func():
"""\
"""


def func():
"""\\
"""


def func():
"""\ \
"""
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pydocstyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod tests {
#[test_case(Rule::NewLineAfterLastParagraph, Path::new("D.py"))]
#[test_case(Rule::NewLineAfterSectionName, Path::new("sections.py"))]
#[test_case(Rule::NoBlankLineAfterFunction, Path::new("D.py"))]
#[test_case(Rule::FitsOnOneLine, Path::new("D200.py"))]
#[test_case(Rule::NoBlankLineAfterFunction, Path::new("D202.py"))]
#[test_case(Rule::BlankLineBeforeClass, Path::new("D.py"))]
#[test_case(Rule::NoBlankLineBeforeFunction, Path::new("D.py"))]
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff/src/rules/pydocstyle/rules/one_liner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub(crate) fn one_liner(checker: &mut Checker, docstring: &Docstring) {
// characters, avoid applying the fix.
let body = docstring.body();
let trimmed = body.trim();
if !trimmed.ends_with(trailing.chars().last().unwrap())
if trimmed.chars().rev().take_while(|c| *c == '\\').count() % 2 == 0
&& !trimmed.ends_with(trailing.chars().last().unwrap())
&& !trimmed.starts_with(leading.chars().last().unwrap())
{
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: crates/ruff/src/rules/pydocstyle/mod.rs
---
D200.py:2:5: D200 One-line docstring should fit on one line
|
1 | def func():
2 | """\
| _____^
3 | | """
| |_______^ D200
|
= help: Reformat to one line

D200.py:7:5: D200 [*] One-line docstring should fit on one line
|
6 | def func():
7 | """\\
| _____^
8 | | """
| |_______^ D200
|
= help: Reformat to one line

Suggested fix
4 4 |
5 5 |
6 6 | def func():
7 |- """\\
8 |- """
7 |+ """\\"""
9 8 |
10 9 |
11 10 | def func():

D200.py:12:5: D200 One-line docstring should fit on one line
|
11 | def func():
12 | """\ \
| _____^
13 | | """
| |_______^ D200
|
= help: Reformat to one line


0 comments on commit 558b56f

Please sign in to comment.