Avoid trimming escaped whitespace in D210 (#3635)

This commit is contained in:
Charlie Marsh
2023-03-20 17:17:42 -04:00
committed by GitHub
parent 169dd72328
commit f039bf36a2
2 changed files with 7 additions and 0 deletions

View File

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

View File

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