Don't autofix D210 by introducing a syntax error (#1093)

This commit is contained in:
Charlie Marsh
2022-12-05 19:13:22 -05:00
committed by GitHub
parent 995994be3e
commit b94169a8bb
5 changed files with 51 additions and 11 deletions

View File

@@ -633,17 +633,25 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, definition: &Definition)
.chain(constants::SINGLE_QUOTE_PREFIXES)
{
if first_line.starts_with(pattern) {
check.amend(Fix::replacement(
trimmed.to_string(),
Location::new(
docstring.location.row(),
docstring.location.column() + pattern.len(),
),
Location::new(
docstring.location.row(),
docstring.location.column() + pattern.len() + line.chars().count(),
),
));
if let Some(quote) = pattern.chars().last() {
// If removing whitespace would lead to an invalid string of quote
// characters, avoid applying the fix.
if !trimmed.ends_with(quote) {
check.amend(Fix::replacement(
trimmed.to_string(),
Location::new(
docstring.location.row(),
docstring.location.column() + pattern.len(),
),
Location::new(
docstring.location.row(),
docstring.location.column()
+ pattern.len()
+ line.chars().count(),
),
));
}
}
break;
}
}

View File

@@ -47,4 +47,12 @@ expression: checks
end_location:
row: 299
column: 36
- kind: NoSurroundingWhitespace
location:
row: 581
column: 4
end_location:
row: 581
column: 51
fix: ~

View File

@@ -106,4 +106,12 @@ expression: checks
row: 520
column: 32
fix: ~
- kind: EndsInPeriod
location:
row: 581
column: 4
end_location:
row: 581
column: 51
fix: ~

View File

@@ -98,4 +98,12 @@ expression: checks
row: 520
column: 32
fix: ~
- kind: EndsInPunctuation
location:
row: 581
column: 4
end_location:
row: 581
column: 51
fix: ~