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 PYI010 #4634

Merged
merged 4 commits into from May 24, 2023
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
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