Skip BOM when inserting start-of-file imports (#7622)

See:
https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387485.
This commit is contained in:
Charlie Marsh 2023-09-23 15:36:50 -04:00 committed by GitHub
parent b194f59aab
commit 8ba8896a7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 4 deletions

View file

@ -64,7 +64,7 @@ impl<'a> Insertion<'a> {
// Otherwise, advance to the next row.
locator.full_line_end(location)
} else {
TextSize::default()
locator.contents_start()
};
// Skip over commented lines, with whitespace separation.

View file

@ -19,6 +19,7 @@ mod tests {
#[test_case(Rule::SuppressibleException, Path::new("SIM105_1.py"))]
#[test_case(Rule::SuppressibleException, Path::new("SIM105_2.py"))]
#[test_case(Rule::SuppressibleException, Path::new("SIM105_3.py"))]
#[test_case(Rule::SuppressibleException, Path::new("SIM105_4.py"))]
#[test_case(Rule::ReturnInTryExceptFinally, Path::new("SIM107.py"))]
#[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"))]
#[test_case(Rule::CompareWithTuple, Path::new("SIM109.py"))]

View file

@ -0,0 +1,22 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM105_4.py:2:1: SIM105 [*] Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pass`
|
1 | #!/usr/bin/env python
2 | / try:
3 | | from __builtin__ import bytes, str, open, super, range, zip, round, int, pow, object, input
4 | | except ImportError: pass
| |___________________________^ SIM105
|
= help: Replace with `contextlib.suppress(ImportError)`
Suggested fix
1 1 | #!/usr/bin/env python
2 |-try:
2 |+import contextlib
3 |+with contextlib.suppress(ImportError):
3 4 | from __builtin__ import bytes, str, open, super, range, zip, round, int, pow, object, input
4 |-except ImportError: pass