mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 22:31:47 +00:00
Avoid generating empty statement bodies (#700)
This commit is contained in:
parent
6bcc11a90f
commit
bbc38fea73
16 changed files with 94 additions and 55 deletions
1
resources/test/fixtures/F401_0.py
vendored
1
resources/test/fixtures/F401_0.py
vendored
|
@ -28,7 +28,6 @@ from blah import ClassA, ClassB, ClassC
|
|||
if TYPE_CHECKING:
|
||||
from models import Fruit, Nut, Vegetable
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import shelve
|
||||
import importlib
|
||||
|
|
1
resources/test/fixtures/U010.py
vendored
1
resources/test/fixtures/U010.py
vendored
|
@ -7,6 +7,7 @@ from __future__ import invalid_module, generators
|
|||
|
||||
if True:
|
||||
from __future__ import generator_stop
|
||||
from __future__ import generators
|
||||
|
||||
if True:
|
||||
from __future__ import generator_stop
|
||||
|
|
|
@ -2411,16 +2411,20 @@ impl<'a> Checker<'a> {
|
|||
.iter()
|
||||
.map(|index| self.parents[*index])
|
||||
.collect();
|
||||
|
||||
let removal_fn = match kind {
|
||||
match match kind {
|
||||
ImportKind::Import => pyflakes::fixes::remove_unused_imports,
|
||||
ImportKind::ImportFrom => pyflakes::fixes::remove_unused_import_froms,
|
||||
};
|
||||
|
||||
match removal_fn(self.locator, &full_names, child, parent, &deleted) {
|
||||
Ok(fix) => Some(fix),
|
||||
}(
|
||||
self.locator, &full_names, child, parent, &deleted
|
||||
) {
|
||||
Ok(fix) => {
|
||||
if fix.patch.content.is_empty() || fix.patch.content == "pass" {
|
||||
self.deletions.insert(defined_by);
|
||||
}
|
||||
Some(fix)
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to fix unused imports: {}", e);
|
||||
error!("Failed to remove unused imports: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use crate::check_ast::Checker;
|
|||
use crate::checks::CheckCode;
|
||||
use crate::flake8_print::checks;
|
||||
|
||||
/// T201, T203
|
||||
pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
|
||||
if let Some(mut check) = checks::print_call(
|
||||
expr,
|
||||
|
@ -26,7 +27,6 @@ pub fn print_call(checker: &mut Checker, expr: &Expr, func: &Expr) {
|
|||
.iter()
|
||||
.map(|index| checker.parents[*index])
|
||||
.collect();
|
||||
|
||||
match helpers::remove_stmt(
|
||||
checker.parents[context.defined_by],
|
||||
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)
|
||||
}
|
||||
Err(e) => error!("Failed to fix unused imports: {}", e),
|
||||
Err(e) => error!("Failed to remove print call: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
if let ExprKind::Attribute { value, attr, .. } = &expr.node {
|
||||
if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) {
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::check_ast::Checker;
|
|||
use crate::pyupgrade;
|
||||
use crate::pyupgrade::checks;
|
||||
|
||||
/// U008
|
||||
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.
|
||||
// (We check this in `check_super_args` too, so this is just an optimization.)
|
||||
|
|
|
@ -6,6 +6,7 @@ use crate::check_ast::Checker;
|
|||
use crate::checks::CheckKind;
|
||||
use crate::pyupgrade::checks;
|
||||
|
||||
/// U003
|
||||
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 checker.patch() {
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::autofix::Fix;
|
|||
use crate::check_ast::Checker;
|
||||
use crate::pyupgrade::checks;
|
||||
|
||||
/// U002
|
||||
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 checker.patch() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use log::error;
|
||||
use rustpython_ast::{AliasData, Located};
|
||||
use rustpython_parser::ast::Stmt;
|
||||
|
||||
|
@ -62,15 +63,21 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo
|
|||
.iter()
|
||||
.map(|index| checker.parents[*index])
|
||||
.collect();
|
||||
if let Ok(fix) = fixes::remove_unnecessary_future_import(
|
||||
match fixes::remove_unnecessary_future_import(
|
||||
checker.locator,
|
||||
&removable_index,
|
||||
checker.parents[context.defined_by],
|
||||
context.defined_in.map(|index| checker.parents[index]),
|
||||
&deleted,
|
||||
) {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use rustpython_parser::ast::Expr;
|
|||
use crate::check_ast::Checker;
|
||||
use crate::pyupgrade::{checks, fixes};
|
||||
|
||||
/// U011
|
||||
pub fn unnecessary_lru_cache_params(checker: &mut Checker, decorator_list: &[Expr]) {
|
||||
if let Some(mut check) = checks::unnecessary_lru_cache_params(
|
||||
decorator_list,
|
||||
|
|
|
@ -5,6 +5,7 @@ use crate::autofix::Fix;
|
|||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckKind};
|
||||
|
||||
/// U006
|
||||
pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr, id: &str) {
|
||||
let mut check = Check::new(
|
||||
CheckKind::UsePEP585Annotation(id.to_string()),
|
||||
|
|
|
@ -41,6 +41,7 @@ fn union(elts: &[Expr]) -> Expr {
|
|||
}
|
||||
}
|
||||
|
||||
/// U007
|
||||
pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) {
|
||||
if checker.match_typing_module(value, "Optional") {
|
||||
let mut check = Check::new(CheckKind::UsePEP604Annotation, Range::from_located(expr));
|
||||
|
|
|
@ -6,6 +6,7 @@ use crate::autofix::helpers;
|
|||
use crate::check_ast::Checker;
|
||||
use crate::pyupgrade::checks;
|
||||
|
||||
/// U001
|
||||
pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr, targets: &[Expr]) {
|
||||
if let Some(mut check) =
|
||||
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)
|
||||
}
|
||||
Err(e) => error!("Failed to fix unused imports: {}", e),
|
||||
Err(e) => error!("Failed to fix remove metaclass type: {}", e),
|
||||
}
|
||||
}
|
||||
checker.add_check(check);
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::check_ast::Checker;
|
|||
use crate::pyupgrade;
|
||||
use crate::pyupgrade::checks;
|
||||
|
||||
/// U004
|
||||
pub fn useless_object_inheritance(
|
||||
checker: &mut Checker,
|
||||
stmt: &Stmt,
|
||||
|
|
|
@ -67,19 +67,19 @@ expression: checks
|
|||
- - shelve
|
||||
- false
|
||||
location:
|
||||
row: 33
|
||||
row: 32
|
||||
column: 4
|
||||
end_location:
|
||||
row: 33
|
||||
row: 32
|
||||
column: 17
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 33
|
||||
row: 32
|
||||
column: 0
|
||||
end_location:
|
||||
row: 34
|
||||
row: 33
|
||||
column: 0
|
||||
applied: false
|
||||
- kind:
|
||||
|
@ -87,39 +87,39 @@ expression: checks
|
|||
- - importlib
|
||||
- false
|
||||
location:
|
||||
row: 34
|
||||
row: 33
|
||||
column: 4
|
||||
end_location:
|
||||
row: 34
|
||||
row: 33
|
||||
column: 20
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
content: pass
|
||||
location:
|
||||
row: 34
|
||||
column: 0
|
||||
row: 33
|
||||
column: 4
|
||||
end_location:
|
||||
row: 35
|
||||
column: 0
|
||||
row: 33
|
||||
column: 20
|
||||
applied: false
|
||||
- kind:
|
||||
UnusedImport:
|
||||
- - pathlib
|
||||
- false
|
||||
location:
|
||||
row: 38
|
||||
row: 37
|
||||
column: 4
|
||||
end_location:
|
||||
row: 38
|
||||
row: 37
|
||||
column: 18
|
||||
fix:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 38
|
||||
row: 37
|
||||
column: 0
|
||||
end_location:
|
||||
row: 39
|
||||
row: 38
|
||||
column: 0
|
||||
applied: false
|
||||
- kind:
|
||||
|
@ -127,19 +127,19 @@ expression: checks
|
|||
- - pickle
|
||||
- false
|
||||
location:
|
||||
row: 53
|
||||
row: 52
|
||||
column: 8
|
||||
end_location:
|
||||
row: 53
|
||||
row: 52
|
||||
column: 21
|
||||
fix:
|
||||
patch:
|
||||
content: pass
|
||||
location:
|
||||
row: 53
|
||||
row: 52
|
||||
column: 8
|
||||
end_location:
|
||||
row: 53
|
||||
row: 52
|
||||
column: 21
|
||||
applied: false
|
||||
|
||||
|
|
|
@ -129,52 +129,71 @@ expression: checks
|
|||
end_location:
|
||||
row: 9
|
||||
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:
|
||||
patch:
|
||||
content: ""
|
||||
location:
|
||||
row: 12
|
||||
row: 9
|
||||
column: 0
|
||||
end_location:
|
||||
row: 13
|
||||
row: 10
|
||||
column: 0
|
||||
applied: false
|
||||
- kind:
|
||||
UnnecessaryFutureImport:
|
||||
- generators
|
||||
location:
|
||||
row: 13
|
||||
row: 10
|
||||
column: 4
|
||||
end_location:
|
||||
row: 13
|
||||
column: 53
|
||||
row: 10
|
||||
column: 37
|
||||
fix:
|
||||
patch:
|
||||
content: from __future__ import invalid_module
|
||||
content: pass
|
||||
location:
|
||||
row: 10
|
||||
column: 4
|
||||
end_location:
|
||||
row: 10
|
||||
column: 37
|
||||
applied: false
|
||||
- kind:
|
||||
UnnecessaryFutureImport:
|
||||
- generator_stop
|
||||
location:
|
||||
row: 13
|
||||
column: 4
|
||||
end_location:
|
||||
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
|
||||
fix:
|
||||
patch:
|
||||
content: from __future__ import invalid_module
|
||||
location:
|
||||
row: 14
|
||||
column: 4
|
||||
end_location:
|
||||
row: 14
|
||||
column: 53
|
||||
applied: false
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue