Add autofix for PYI009 (#4583)

This commit is contained in:
qdegraaf 2023-05-22 18:41:18 +02:00 committed by GitHub
parent b613460fe5
commit 5ba47c3302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 7 deletions

View File

@ -1,18 +1,23 @@
use rustpython_parser::ast::{Ranged, Stmt}; use rustpython_parser::ast::{Ranged, Stmt};
use ruff_diagnostics::{Diagnostic, Violation}; use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::Rule;
#[violation] #[violation]
pub struct PassStatementStubBody; pub struct PassStatementStubBody;
impl Violation for PassStatementStubBody { impl AlwaysAutofixableViolation for PassStatementStubBody {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Empty body should contain `...`, not `pass`") format!("Empty body should contain `...`, not `pass`")
} }
fn autofix_title(&self) -> String {
format!("Replace `pass` with `...`")
}
} }
/// PYI009 /// PYI009
@ -21,8 +26,13 @@ pub(crate) fn pass_statement_stub_body(checker: &mut Checker, body: &[Stmt]) {
return; return;
} }
if body[0].is_pass_stmt() { if body[0].is_pass_stmt() {
checker let mut diagnostic = Diagnostic::new(PassStatementStubBody, body[0].range());
.diagnostics if checker.patch(Rule::PassStatementStubBody) {
.push(Diagnostic::new(PassStatementStubBody, body[0].range())); diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
format!("..."),
body[0].range(),
)));
};
checker.diagnostics.push(diagnostic);
} }
} }

View File

@ -1,7 +1,7 @@
--- ---
source: crates/ruff/src/rules/flake8_pyi/mod.rs source: crates/ruff/src/rules/flake8_pyi/mod.rs
--- ---
PYI009.pyi:3:5: PYI009 Empty body should contain `...`, not `pass` PYI009.pyi:3:5: PYI009 [*] Empty body should contain `...`, not `pass`
| |
3 | def bar(): ... # OK 3 | def bar(): ... # OK
4 | def foo(): 4 | def foo():
@ -10,12 +10,30 @@ PYI009.pyi:3:5: PYI009 Empty body should contain `...`, not `pass`
6 | 6 |
7 | class Bar: ... # OK 7 | class Bar: ... # OK
| |
= help: Replace `pass` with `...`
PYI009.pyi:8:5: PYI009 Empty body should contain `...`, not `pass` Fix
1 1 | def bar(): ... # OK
2 2 | def foo():
3 |- pass # ERROR PYI009, since we're in a stub file
3 |+ ... # ERROR PYI009, since we're in a stub file
4 4 |
5 5 | class Bar: ... # OK
6 6 |
PYI009.pyi:8:5: PYI009 [*] Empty body should contain `...`, not `pass`
| |
8 | class Foo: 8 | class Foo:
9 | pass # ERROR PYI009, since we're in a stub file 9 | pass # ERROR PYI009, since we're in a stub file
| ^^^^ PYI009 | ^^^^ PYI009
| |
= help: Replace `pass` with `...`
Fix
5 5 | class Bar: ... # OK
6 6 |
7 7 | class Foo:
8 |- pass # ERROR PYI009, since we're in a stub file
8 |+ ... # ERROR PYI009, since we're in a stub file