Skip to content

Commit

Permalink
Avoid trimming escaped whitespace in D210 (#3635)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 20, 2023
1 parent 169dd72 commit f039bf3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/src/rules/pydocstyle/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit f039bf3

Please sign in to comment.