Automatically remove empty type-checking blocks (#2598)

This commit is contained in:
Charlie Marsh 2023-02-05 18:46:07 -05:00 committed by GitHub
parent f6864a96f6
commit 7fa5ce8b63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 16 deletions

View file

@ -1232,7 +1232,7 @@ For more, see [flake8-type-checking](https://pypi.org/project/flake8-type-checki
| TCH002 | typing-only-third-party-import | Move third-party import `{}` into a type-checking block | |
| TCH003 | typing-only-standard-library-import | Move standard library import `{}` into a type-checking block | |
| TCH004 | runtime-import-in-type-checking-block | Move import `{}` out of type-checking block. Import is used for more than type hinting. | |
| TCH005 | empty-type-checking-block | Found empty type-checking block | |
| TCH005 | empty-type-checking-block | Found empty type-checking block | 🛠 |
### flake8-unused-arguments (ARG)

View file

@ -1937,7 +1937,7 @@ where
if flake8_type_checking::helpers::is_type_checking_block(self, test) {
if self.settings.rules.enabled(&Rule::EmptyTypeCheckingBlock) {
flake8_type_checking::rules::empty_type_checking_block(self, body);
flake8_type_checking::rules::empty_type_checking_block(self, stmt, body);
}
let prev_in_type_checking_block = self.in_type_checking_block;

View file

@ -1,29 +1,69 @@
use log::error;
use rustpython_ast::{Stmt, StmtKind};
use ruff_macros::derive_message_formats;
use crate::ast::types::Range;
use crate::ast::types::{Range, RefEquality};
use crate::autofix::helpers::delete_stmt;
use crate::checkers::ast::Checker;
use crate::define_violation;
use crate::registry::Diagnostic;
use crate::violation::Violation;
use crate::violation::AlwaysAutofixableViolation;
define_violation!(
pub struct EmptyTypeCheckingBlock;
);
impl Violation for EmptyTypeCheckingBlock {
impl AlwaysAutofixableViolation for EmptyTypeCheckingBlock {
#[derive_message_formats]
fn message(&self) -> String {
format!("Found empty type-checking block")
}
fn autofix_title(&self) -> String {
format!("Delete empty type-checking block")
}
}
/// TCH005
pub fn empty_type_checking_block(checker: &mut Checker, body: &[Stmt]) {
pub fn empty_type_checking_block<'a, 'b>(
checker: &mut Checker<'a>,
stmt: &'a Stmt,
body: &'a [Stmt],
) where
'b: 'a,
{
if body.len() == 1 && matches!(body[0].node, StmtKind::Pass) {
checker.diagnostics.push(Diagnostic::new(
EmptyTypeCheckingBlock,
Range::from_located(&body[0]),
));
let mut diagnostic = Diagnostic::new(EmptyTypeCheckingBlock, Range::from_located(&body[0]));
// Delete the entire type-checking block.
if checker.patch(diagnostic.kind.rule()) {
let parent = checker
.child_to_parent
.get(&RefEquality(stmt))
.map(std::convert::Into::into);
let deleted: Vec<&Stmt> = checker
.deletions
.iter()
.map(std::convert::Into::into)
.collect();
match delete_stmt(
stmt,
parent,
&deleted,
checker.locator,
checker.indexer,
checker.stylist,
) {
Ok(fix) => {
if fix.content.is_empty() || fix.content == "pass" {
checker.deletions.insert(RefEquality(stmt));
}
diagnostic.amend(fix);
}
Err(e) => error!("Failed to remove empty type-checking block: {e}"),
}
}
checker.diagnostics.push(diagnostic);
}
}

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_type_checking/mod.rs
source: crates/ruff/src/rules/flake8_type_checking/mod.rs
expression: diagnostics
---
- kind:
@ -10,7 +10,15 @@ expression: diagnostics
end_location:
row: 4
column: 8
fix: ~
fix:
content:
- ""
location:
row: 3
column: 0
end_location:
row: 5
column: 0
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
@ -20,7 +28,15 @@ expression: diagnostics
end_location:
row: 8
column: 8
fix: ~
fix:
content:
- ""
location:
row: 7
column: 0
end_location:
row: 9
column: 0
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
@ -30,7 +46,15 @@ expression: diagnostics
end_location:
row: 11
column: 8
fix: ~
fix:
content:
- ""
location:
row: 10
column: 0
end_location:
row: 12
column: 0
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
@ -40,7 +64,15 @@ expression: diagnostics
end_location:
row: 16
column: 12
fix: ~
fix:
content:
- ""
location:
row: 15
column: 0
end_location:
row: 17
column: 0
parent: ~
- kind:
EmptyTypeCheckingBlock: ~
@ -50,6 +82,14 @@ expression: diagnostics
end_location:
row: 22
column: 12
fix: ~
fix:
content:
- ""
location:
row: 21
column: 0
end_location:
row: 23
column: 0
parent: ~