Avoid generating empty statement bodies (#700)

This commit is contained in:
Charlie Marsh 2022-11-12 11:39:09 -05:00 committed by GitHub
parent 6bcc11a90f
commit bbc38fea73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 94 additions and 55 deletions

View file

@ -28,7 +28,6 @@ from blah import ClassA, ClassB, ClassC
if TYPE_CHECKING: if TYPE_CHECKING:
from models import Fruit, Nut, Vegetable from models import Fruit, Nut, Vegetable
if TYPE_CHECKING: if TYPE_CHECKING:
import shelve import shelve
import importlib import importlib

View file

@ -7,6 +7,7 @@ from __future__ import invalid_module, generators
if True: if True:
from __future__ import generator_stop from __future__ import generator_stop
from __future__ import generators
if True: if True:
from __future__ import generator_stop from __future__ import generator_stop

View file

@ -2411,16 +2411,20 @@ impl<'a> Checker<'a> {
.iter() .iter()
.map(|index| self.parents[*index]) .map(|index| self.parents[*index])
.collect(); .collect();
match match kind {
let removal_fn = match kind {
ImportKind::Import => pyflakes::fixes::remove_unused_imports, ImportKind::Import => pyflakes::fixes::remove_unused_imports,
ImportKind::ImportFrom => pyflakes::fixes::remove_unused_import_froms, ImportKind::ImportFrom => pyflakes::fixes::remove_unused_import_froms,
}; }(
self.locator, &full_names, child, parent, &deleted
match removal_fn(self.locator, &full_names, child, parent, &deleted) { ) {
Ok(fix) => Some(fix), Ok(fix) => {
if fix.patch.content.is_empty() || fix.patch.content == "pass" {
self.deletions.insert(defined_by);
}
Some(fix)
}
Err(e) => { Err(e) => {
error!("Failed to fix unused imports: {}", e); error!("Failed to remove unused imports: {}", e);
None None
} }
} }

View file

@ -7,6 +7,7 @@ use crate::check_ast::Checker;
use crate::checks::CheckCode; use crate::checks::CheckCode;
use crate::flake8_print::checks; use crate::flake8_print::checks;
/// T201, T203
pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) { pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
if let Some(mut check) = checks::print_call( if let Some(mut check) = checks::print_call(
expr, expr,
@ -26,7 +27,6 @@ pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
.iter() .iter()
.map(|index| checker.parents[*index]) .map(|index| checker.parents[*index])
.collect(); .collect();
match helpers::remove_stmt( match helpers::remove_stmt(
checker.parents[context.defined_by], checker.parents[context.defined_by],
context.defined_in.map(|index| checker.parents[index]), context.defined_in.map(|index| checker.parents[index]),
@ -38,7 +38,7 @@ pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
} }
check.amend(fix) check.amend(fix)
} }
Err(e) => error!("Failed to fix unused imports: {}", e), Err(e) => error!("Failed to remove print call: {}", e),
} }
} }
} }

View file

@ -28,6 +28,7 @@ static DEPRECATED_ALIASES: Lazy<BTreeMap<&'static str, &'static str>> = Lazy::ne
]) ])
}); });
/// U005
pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) { pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) {
if let ExprKind::Attribute { value, attr, .. } = &expr.node { if let ExprKind::Attribute { value, attr, .. } = &expr.node {
if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) { if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) {

View file

@ -5,6 +5,7 @@ use crate::check_ast::Checker;
use crate::pyupgrade; use crate::pyupgrade;
use crate::pyupgrade::checks; use crate::pyupgrade::checks;
/// U008
pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) { pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) {
// Only bother going through the super check at all if we're in a `super` call. // Only bother going through the super check at all if we're in a `super` call.
// (We check this in `check_super_args` too, so this is just an optimization.) // (We check this in `check_super_args` too, so this is just an optimization.)

View file

@ -6,6 +6,7 @@ use crate::check_ast::Checker;
use crate::checks::CheckKind; use crate::checks::CheckKind;
use crate::pyupgrade::checks; use crate::pyupgrade::checks;
/// U003
pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) { pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) {
if let Some(mut check) = checks::type_of_primitive(func, args, Range::from_located(expr)) { if let Some(mut check) = checks::type_of_primitive(func, args, Range::from_located(expr)) {
if checker.patch() { if checker.patch() {

View file

@ -5,6 +5,7 @@ use crate::autofix::Fix;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::pyupgrade::checks; use crate::pyupgrade::checks;
/// U002
pub fn unnecessary_abspath(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) { pub fn unnecessary_abspath(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) {
if let Some(mut check) = checks::unnecessary_abspath(func, args, Range::from_located(expr)) { if let Some(mut check) = checks::unnecessary_abspath(func, args, Range::from_located(expr)) {
if checker.patch() { if checker.patch() {

View file

@ -1,5 +1,6 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use log::error;
use rustpython_ast::{AliasData, Located}; use rustpython_ast::{AliasData, Located};
use rustpython_parser::ast::Stmt; use rustpython_parser::ast::Stmt;
@ -62,14 +63,20 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo
.iter() .iter()
.map(|index| checker.parents[*index]) .map(|index| checker.parents[*index])
.collect(); .collect();
if let Ok(fix) = fixes::remove_unnecessary_future_import( match fixes::remove_unnecessary_future_import(
checker.locator, checker.locator,
&removable_index, &removable_index,
checker.parents[context.defined_by], checker.parents[context.defined_by],
context.defined_in.map(|index| checker.parents[index]), context.defined_in.map(|index| checker.parents[index]),
&deleted, &deleted,
) { ) {
check.amend(fix); Ok(fix) => {
if fix.patch.content.is_empty() || fix.patch.content == "pass" {
checker.deletions.insert(context.defined_by);
}
check.amend(fix);
}
Err(e) => error!("Failed to remove __future__ import: {}", e),
} }
} }
checker.add_check(check); checker.add_check(check);

View file

@ -3,6 +3,7 @@ use rustpython_parser::ast::Expr;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::pyupgrade::{checks, fixes}; use crate::pyupgrade::{checks, fixes};
/// U011
pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) { pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) {
if let Some(mut check) = checks::unnecessary_lru_cache_params( if let Some(mut check) = checks::unnecessary_lru_cache_params(
decorator_list, decorator_list,

View file

@ -5,6 +5,7 @@ use crate::autofix::Fix;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind}; use crate::checks::{Check, CheckKind};
/// U006
pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr, id: &str) { pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr, id: &str) {
let mut check = Check::new( let mut check = Check::new(
CheckKind::UsePEP585Annotation(id.to_string()), CheckKind::UsePEP585Annotation(id.to_string()),

View file

@ -41,6 +41,7 @@ fn union(elts: &[Expr]) -> Expr {
} }
} }
/// U007
pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) { pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) {
if checker.match_typing_module(value, "Optional") { if checker.match_typing_module(value, "Optional") {
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr)); let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));

View file

@ -6,6 +6,7 @@ use crate::autofix::helpers;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::pyupgrade::checks; use crate::pyupgrade::checks;
/// U001
pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr, targets: &[Expr]) { pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr, targets: &[Expr]) {
if let Some(mut check) = if let Some(mut check) =
checks::useless_metaclass_type(targets, value, Range::from_located(stmt)) checks::useless_metaclass_type(targets, value, Range::from_located(stmt))
@ -29,7 +30,7 @@ pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr,
} }
check.amend(fix) check.amend(fix)
} }
Err(e) => error!("Failed to fix unused imports: {}", e), Err(e) => error!("Failed to fix remove metaclass type: {}", e),
} }
} }
checker.add_check(check); checker.add_check(check);

View file

@ -4,6 +4,7 @@ use crate::check_ast::Checker;
use crate::pyupgrade; use crate::pyupgrade;
use crate::pyupgrade::checks; use crate::pyupgrade::checks;
/// U004
pub fn useless_object_inheritance( pub fn useless_object_inheritance(
checker: &mut Checker, checker: &mut Checker,
stmt: &Stmt, stmt: &Stmt,

View file

@ -67,19 +67,19 @@ expression: checks
- - shelve - - shelve
- false - false
location: location:
row: 33 row: 32
column: 4 column: 4
end_location: end_location:
row: 33 row: 32
column: 17 column: 17
fix: fix:
patch: patch:
content: "" content: ""
location: location:
row: 33 row: 32
column: 0 column: 0
end_location: end_location:
row: 34 row: 33
column: 0 column: 0
applied: false applied: false
- kind: - kind:
@ -87,39 +87,39 @@ expression: checks
- - importlib - - importlib
- false - false
location: location:
row: 34 row: 33
column: 4 column: 4
end_location: end_location:
row: 34 row: 33
column: 20 column: 20
fix: fix:
patch: patch:
content: "" content: pass
location: location:
row: 34 row: 33
column: 0 column: 4
end_location: end_location:
row: 35 row: 33
column: 0 column: 20
applied: false applied: false
- kind: - kind:
UnusedImport: UnusedImport:
- - pathlib - - pathlib
- false - false
location: location:
row: 38 row: 37
column: 4 column: 4
end_location: end_location:
row: 38 row: 37
column: 18 column: 18
fix: fix:
patch: patch:
content: "" content: ""
location: location:
row: 38 row: 37
column: 0 column: 0
end_location: end_location:
row: 39 row: 38
column: 0 column: 0
applied: false applied: false
- kind: - kind:
@ -127,19 +127,19 @@ expression: checks
- - pickle - - pickle
- false - false
location: location:
row: 53 row: 52
column: 8 column: 8
end_location: end_location:
row: 53 row: 52
column: 21 column: 21
fix: fix:
patch: patch:
content: pass content: pass
location: location:
row: 53 row: 52
column: 8 column: 8
end_location: end_location:
row: 53 row: 52
column: 21 column: 21
applied: false applied: false

View file

@ -129,52 +129,71 @@ expression: checks
end_location: end_location:
row: 9 row: 9
column: 41 column: 41
fix:
patch:
content: pass
location:
row: 9
column: 4
end_location:
row: 9
column: 41
applied: false
- kind:
UnnecessaryFutureImport:
- generator_stop
location:
row: 12
column: 4
end_location:
row: 12
column: 41
fix: fix:
patch: patch:
content: "" content: ""
location: location:
row: 12 row: 9
column: 0 column: 0
end_location: end_location:
row: 13 row: 10
column: 0 column: 0
applied: false applied: false
- kind: - kind:
UnnecessaryFutureImport: UnnecessaryFutureImport:
- generators - generators
location:
row: 10
column: 4
end_location:
row: 10
column: 37
fix:
patch:
content: pass
location:
row: 10
column: 4
end_location:
row: 10
column: 37
applied: false
- kind:
UnnecessaryFutureImport:
- generator_stop
location: location:
row: 13 row: 13
column: 4 column: 4
end_location: end_location:
row: 13 row: 13
column: 41
fix:
patch:
content: ""
location:
row: 13
column: 0
end_location:
row: 14
column: 0
applied: false
- kind:
UnnecessaryFutureImport:
- generators
location:
row: 14
column: 4
end_location:
row: 14
column: 53 column: 53
fix: fix:
patch: patch:
content: from __future__ import invalid_module content: from __future__ import invalid_module
location: location:
row: 13 row: 14
column: 4 column: 4
end_location: end_location:
row: 13 row: 14
column: 53 column: 53
applied: false applied: false