From f039bf36a2dbe3753f195580981fb99ae47b352d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 20 Mar 2023 17:17:42 -0400 Subject: [PATCH] Avoid trimming escaped whitespace in D210 (#3635) --- crates/ruff/src/rules/pydocstyle/helpers.rs | 5 +++++ .../src/rules/pydocstyle/rules/no_surrounding_whitespace.rs | 2 ++ 2 files changed, 7 insertions(+) diff --git a/crates/ruff/src/rules/pydocstyle/helpers.rs b/crates/ruff/src/rules/pydocstyle/helpers.rs index e9db7482a6..aa3aabf544 100644 --- a/crates/ruff/src/rules/pydocstyle/helpers.rs +++ b/crates/ruff/src/rules/pydocstyle/helpers.rs @@ -62,6 +62,11 @@ pub(crate) fn should_ignore_definition( false } +/// Return true if a line ends with an odd number of backslashes (i.e., ends with an escape). +pub(crate) fn ends_with_backslash(line: &str) -> bool { + line.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1 +} + /// Check if a docstring should be ignored. pub(crate) fn should_ignore_docstring(contents: &str) -> bool { // Avoid analyzing docstrings that contain implicit string concatenations. diff --git a/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs b/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs index 2172f59b0b..4aaaf1db84 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/no_surrounding_whitespace.rs @@ -8,6 +8,7 @@ use crate::checkers::ast::Checker; use crate::docstrings::definition::Docstring; use crate::message::Location; use crate::registry::AsRule; +use crate::rules::pydocstyle::helpers::ends_with_backslash; #[violation] pub struct SurroundingWhitespace; @@ -46,6 +47,7 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) { // characters, avoid applying the fix. if !trimmed.ends_with(pattern.chars().last().unwrap()) && !trimmed.starts_with(pattern.chars().last().unwrap()) + && !ends_with_backslash(trimmed) { diagnostic.amend(Fix::replacement( trimmed.to_string(),