diff --git a/crates/ruff/src/rules/flake8_pyi/rules/non_empty_stub_body.rs b/crates/ruff/src/rules/flake8_pyi/rules/non_empty_stub_body.rs index a347eea129bce..0b4642b28ccf8 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/non_empty_stub_body.rs +++ b/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 @@ -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); } diff --git a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI010_PYI010.pyi.snap b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI010_PYI010.pyi.snap index 965f11e0812ae..3d103886ac591 100644 --- a/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI010_PYI010.pyi.snap +++ b/crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI010_PYI010.pyi.snap @@ -1,7 +1,7 @@ --- 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 @@ -9,8 +9,19 @@ PYI010.pyi:6:5: PYI010 Function body must contain only `...` 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 @@ -18,12 +29,31 @@ PYI010.pyi:9:5: PYI010 Function body must contain only `...` 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