From 558b56f8a8a1f5a4b6b742630f577840ab044fe1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 22 Aug 2023 12:25:37 -0400 Subject: [PATCH] Avoid fixing D200 for docstrings that end in escapes (#6779) Appease the fuzzers! Closes https://github.com/astral-sh/ruff/issues/6755. --- .../test/fixtures/pydocstyle/D200.py | 13 ++++++ crates/ruff/src/rules/pydocstyle/mod.rs | 1 + .../src/rules/pydocstyle/rules/one_liner.rs | 3 +- ...ules__pydocstyle__tests__D200_D200.py.snap | 45 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 crates/ruff/resources/test/fixtures/pydocstyle/D200.py create mode 100644 crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D200_D200.py.snap diff --git a/crates/ruff/resources/test/fixtures/pydocstyle/D200.py b/crates/ruff/resources/test/fixtures/pydocstyle/D200.py new file mode 100644 index 0000000000000..f76c66e631596 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pydocstyle/D200.py @@ -0,0 +1,13 @@ +def func(): + """\ + """ + + +def func(): + """\\ + """ + + +def func(): + """\ \ + """ diff --git a/crates/ruff/src/rules/pydocstyle/mod.rs b/crates/ruff/src/rules/pydocstyle/mod.rs index 41c6a1fc6f943..c8a525466665b 100644 --- a/crates/ruff/src/rules/pydocstyle/mod.rs +++ b/crates/ruff/src/rules/pydocstyle/mod.rs @@ -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"))] diff --git a/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs b/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs index 6610c41754c4e..97d79244ed271 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/one_liner.rs @@ -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( diff --git a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D200_D200.py.snap b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D200_D200.py.snap new file mode 100644 index 0000000000000..d81013659998b --- /dev/null +++ b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D200_D200.py.snap @@ -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 + +