Avoid fixing D200 for docstrings that end in escapes (#6779)

Appease the fuzzers! Closes
https://github.com/astral-sh/ruff/issues/6755.
This commit is contained in:
Charlie Marsh 2023-08-22 12:25:37 -04:00 committed by GitHub
parent 749da6589a
commit 558b56f8a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 1 deletions

View file

@ -0,0 +1,13 @@
def func():
"""\
"""
def func():
"""\\
"""
def func():
"""\ \
"""

View file

@ -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"))]

View file

@ -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(

View file

@ -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