Skip to content

Commit

Permalink
Add autofix for PYI010 (#4634)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdegraaf committed May 24, 2023
1 parent c9b39e3 commit 0503505
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
20 changes: 15 additions & 5 deletions crates/ruff/src/rules/flake8_pyi/rules/non_empty_stub_body.rs
@@ -1,18 +1,23 @@
use rustpython_parser::ast::{self, Constant, Expr, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};

use crate::checkers::ast::Checker;
use crate::registry::Rule;

#[violation]
pub struct NonEmptyStubBody;

impl Violation for NonEmptyStubBody {
impl AlwaysAutofixableViolation for NonEmptyStubBody {
#[derive_message_formats]
fn message(&self) -> String {
format!("Function body must contain only `...`")
}

fn autofix_title(&self) -> String {
format!("Replace function body with `...`")
}
}

/// PYI010
Expand All @@ -27,7 +32,12 @@ pub(crate) fn non_empty_stub_body(checker: &mut Checker, body: &[Stmt]) {
}
}
}
checker
.diagnostics
.push(Diagnostic::new(NonEmptyStubBody, body[0].range()));
let mut diagnostic = Diagnostic::new(NonEmptyStubBody, body[0].range());
if checker.patch(Rule::NonEmptyStubBody) {
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
format!("..."),
body[0].range(),
)));
};
checker.diagnostics.push(diagnostic);
}
@@ -1,29 +1,59 @@
---
source: crates/ruff/src/rules/flake8_pyi/mod.rs
---
PYI010.pyi:6:5: PYI010 Function body must contain only `...`
PYI010.pyi:6:5: PYI010 [*] Function body must contain only `...`
|
6 | def buzz():
7 | print("buzz") # ERROR PYI010
| ^^^^^^^^^^^^^ PYI010
8 |
9 | def foo2():
|
= help: Replace function body with `...`

PYI010.pyi:9:5: PYI010 Function body must contain only `...`
ℹ Fix
3 3 | """foo""" # OK, strings are handled by another rule
4 4 |
5 5 | def buzz():
6 |- print("buzz") # ERROR PYI010
6 |+ ... # ERROR PYI010
7 7 |
8 8 | def foo2():
9 9 | 123 # ERROR PYI010

PYI010.pyi:9:5: PYI010 [*] Function body must contain only `...`
|
9 | def foo2():
10 | 123 # ERROR PYI010
| ^^^ PYI010
11 |
12 | def bizz():
|
= help: Replace function body with `...`

ℹ Fix
6 6 | print("buzz") # ERROR PYI010
7 7 |
8 8 | def foo2():
9 |- 123 # ERROR PYI010
9 |+ ... # ERROR PYI010
10 10 |
11 11 | def bizz():
12 12 | x = 123 # ERROR PYI010

PYI010.pyi:12:5: PYI010 Function body must contain only `...`
PYI010.pyi:12:5: PYI010 [*] Function body must contain only `...`
|
12 | def bizz():
13 | x = 123 # ERROR PYI010
| ^^^^^^^ PYI010
|
= help: Replace function body with `...`

ℹ Fix
9 9 | 123 # ERROR PYI010
10 10 |
11 11 | def bizz():
12 |- x = 123 # ERROR PYI010
12 |+ ... # ERROR PYI010


0 comments on commit 0503505

Please sign in to comment.