Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autofix for W605 [InvalidEscapeSequence] #1361

Merged
merged 2 commits into from Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -541,7 +541,7 @@ For more, see [pycodestyle](https://pypi.org/project/pycodestyle/2.9.1/) on PyPI
| E902 | IOError | IOError: `...` | |
| E999 | SyntaxError | SyntaxError: `...` | |
| W292 | NoNewLineAtEndOfFile | No newline at end of file | 🛠 |
| W605 | InvalidEscapeSequence | Invalid escape sequence: '\c' | |
| W605 | InvalidEscapeSequence | Invalid escape sequence: '\c' | 🛠 |

### mccabe (C90)

Expand Down
6 changes: 5 additions & 1 deletion src/checkers/tokens.rs
Expand Up @@ -89,7 +89,11 @@ pub fn check_tokens(
if enforce_invalid_escape_sequence {
if matches!(tok, Tok::String { .. }) {
checks.extend(pycodestyle::checks::invalid_escape_sequence(
locator, start, end,
locator,
start,
end,
matches!(autofix, flags::Autofix::Enabled)
&& settings.fixable.contains(&CheckCode::W605),
));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/checks.rs
Expand Up @@ -2972,6 +2972,7 @@ impl CheckKind {
| CheckKind::GetAttrWithConstant
| CheckKind::ImplicitReturn
| CheckKind::ImplicitReturnValue
| CheckKind::InvalidEscapeSequence(..)
| CheckKind::IsLiteral
| CheckKind::KeyInDict(..)
| CheckKind::MisplacedComparisonConstant(..)
Expand Down
9 changes: 7 additions & 2 deletions src/pycodestyle/checks.rs
Expand Up @@ -188,6 +188,7 @@ pub fn invalid_escape_sequence(
locator: &SourceCodeLocator,
start: Location,
end: Location,
autofix: bool,
) -> Vec<Check> {
let mut checks = vec![];

Expand Down Expand Up @@ -235,13 +236,17 @@ pub fn invalid_escape_sequence(
};
let location = Location::new(start.row() + row_offset, col);
let end_location = Location::new(location.row(), location.column() + 2);
checks.push(Check::new(
let mut check = Check::new(
CheckKind::InvalidEscapeSequence(next_char),
Range {
location,
end_location,
},
));
);
if autofix {
check.amend(Fix::insertion(r"\".to_string(), location));
}
checks.push(check);
}
}
}
Expand Down
Expand Up @@ -10,7 +10,14 @@ expression: checks
end_location:
row: 2
column: 11
fix: ~
fix:
content: "\\"
location:
row: 2
column: 9
end_location:
row: 2
column: 9
- kind:
InvalidEscapeSequence: "."
location:
Expand All @@ -19,7 +26,14 @@ expression: checks
end_location:
row: 6
column: 2
fix: ~
fix:
content: "\\"
location:
row: 6
column: 0
end_location:
row: 6
column: 0
- kind:
InvalidEscapeSequence: _
location:
Expand All @@ -28,7 +42,14 @@ expression: checks
end_location:
row: 11
column: 7
fix: ~
fix:
content: "\\"
location:
row: 11
column: 5
end_location:
row: 11
column: 5
- kind:
InvalidEscapeSequence: _
location:
Expand All @@ -37,5 +58,12 @@ expression: checks
end_location:
row: 18
column: 7
fix: ~
fix:
content: "\\"
location:
row: 18
column: 5
end_location:
row: 18
column: 5

Expand Up @@ -10,7 +10,14 @@ expression: checks
end_location:
row: 2
column: 11
fix: ~
fix:
content: "\\"
location:
row: 2
column: 9
end_location:
row: 2
column: 9
- kind:
InvalidEscapeSequence: "."
location:
Expand All @@ -19,7 +26,14 @@ expression: checks
end_location:
row: 6
column: 2
fix: ~
fix:
content: "\\"
location:
row: 6
column: 0
end_location:
row: 6
column: 0
- kind:
InvalidEscapeSequence: _
location:
Expand All @@ -28,7 +42,14 @@ expression: checks
end_location:
row: 11
column: 7
fix: ~
fix:
content: "\\"
location:
row: 11
column: 5
end_location:
row: 11
column: 5
- kind:
InvalidEscapeSequence: _
location:
Expand All @@ -37,5 +58,12 @@ expression: checks
end_location:
row: 18
column: 7
fix: ~
fix:
content: "\\"
location:
row: 18
column: 5
end_location:
row: 18
column: 5