Remove check_ prefix from check utilities (#393)

This commit is contained in:
Charlie Marsh 2022-10-10 12:58:40 -04:00 committed by GitHub
parent e1b711d9c6
commit 765db12b84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 93 additions and 114 deletions

View File

@ -1,4 +1,4 @@
pub mod checks; pub mod checkers;
pub mod helpers; pub mod helpers;
pub mod operations; pub mod operations;
pub mod relocate; pub mod relocate;

View File

@ -17,7 +17,7 @@ use crate::checks::{Check, CheckKind, RejectedCmpop};
use crate::python::builtins::BUILTINS; use crate::python::builtins::BUILTINS;
/// Check IfTuple compliance. /// Check IfTuple compliance.
pub fn check_if_tuple(test: &Expr, location: Range) -> Option<Check> { pub fn if_tuple(test: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Tuple { elts, .. } = &test.node { if let ExprKind::Tuple { elts, .. } = &test.node {
if !elts.is_empty() { if !elts.is_empty() {
return Some(Check::new(CheckKind::IfTuple, location)); return Some(Check::new(CheckKind::IfTuple, location));
@ -27,7 +27,7 @@ pub fn check_if_tuple(test: &Expr, location: Range) -> Option<Check> {
} }
/// Check AssertTuple compliance. /// Check AssertTuple compliance.
pub fn check_assert_tuple(test: &Expr, location: Range) -> Option<Check> { pub fn assert_tuple(test: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Tuple { elts, .. } = &test.node { if let ExprKind::Tuple { elts, .. } = &test.node {
if !elts.is_empty() { if !elts.is_empty() {
return Some(Check::new(CheckKind::AssertTuple, location)); return Some(Check::new(CheckKind::AssertTuple, location));
@ -37,7 +37,7 @@ pub fn check_assert_tuple(test: &Expr, location: Range) -> Option<Check> {
} }
/// Check NotInTest and NotIsTest compliance. /// Check NotInTest and NotIsTest compliance.
pub fn check_not_tests( pub fn not_tests(
op: &Unaryop, op: &Unaryop,
operand: &Expr, operand: &Expr,
check_not_in: bool, check_not_in: bool,
@ -76,7 +76,7 @@ pub fn check_not_tests(
} }
/// Check UnusedVariable compliance. /// Check UnusedVariable compliance.
pub fn check_unused_variables( pub fn unused_variables(
scope: &Scope, scope: &Scope,
locator: &dyn CheckLocator, locator: &dyn CheckLocator,
dummy_variable_rgx: &Regex, dummy_variable_rgx: &Regex,
@ -109,7 +109,7 @@ pub fn check_unused_variables(
} }
/// Check DoNotAssignLambda compliance. /// Check DoNotAssignLambda compliance.
pub fn check_do_not_assign_lambda(value: &Expr, location: Range) -> Option<Check> { pub fn do_not_assign_lambda(value: &Expr, location: Range) -> Option<Check> {
if let ExprKind::Lambda { .. } = &value.node { if let ExprKind::Lambda { .. } = &value.node {
Some(Check::new(CheckKind::DoNotAssignLambda, location)) Some(Check::new(CheckKind::DoNotAssignLambda, location))
} else { } else {
@ -118,11 +118,7 @@ pub fn check_do_not_assign_lambda(value: &Expr, location: Range) -> Option<Check
} }
/// Check UselessMetaclassType compliance. /// Check UselessMetaclassType compliance.
pub fn check_useless_metaclass_type( pub fn useless_metaclass_type(targets: &Vec<Expr>, value: &Expr, location: Range) -> Option<Check> {
targets: &Vec<Expr>,
value: &Expr,
location: Range,
) -> Option<Check> {
if targets.len() == 1 { if targets.len() == 1 {
if let ExprKind::Name { id, .. } = targets.first().map(|expr| &expr.node).unwrap() { if let ExprKind::Name { id, .. } = targets.first().map(|expr| &expr.node).unwrap() {
if id == "__metaclass__" { if id == "__metaclass__" {
@ -138,7 +134,7 @@ pub fn check_useless_metaclass_type(
} }
/// Check UnnecessaryAbspath compliance. /// Check UnnecessaryAbspath compliance.
pub fn check_unnecessary_abspath(func: &Expr, args: &Vec<Expr>, location: Range) -> Option<Check> { pub fn unnecessary_abspath(func: &Expr, args: &Vec<Expr>, location: Range) -> Option<Check> {
// Validate the arguments. // Validate the arguments.
if args.len() == 1 { if args.len() == 1 {
if let ExprKind::Name { id, .. } = &args[0].node { if let ExprKind::Name { id, .. } = &args[0].node {
@ -194,7 +190,7 @@ impl Primitive {
} }
/// Check TypeOfPrimitive compliance. /// Check TypeOfPrimitive compliance.
pub fn check_type_of_primitive(func: &Expr, args: &Vec<Expr>, location: Range) -> Option<Check> { pub fn type_of_primitive(func: &Expr, args: &Vec<Expr>, location: Range) -> Option<Check> {
// Validate the arguments. // Validate the arguments.
if args.len() == 1 { if args.len() == 1 {
match &func.node { match &func.node {
@ -222,7 +218,7 @@ fn is_ambiguous_name(name: &str) -> bool {
} }
/// Check AmbiguousVariableName compliance. /// Check AmbiguousVariableName compliance.
pub fn check_ambiguous_variable_name(name: &str, location: Range) -> Option<Check> { pub fn ambiguous_variable_name(name: &str, location: Range) -> Option<Check> {
if is_ambiguous_name(name) { if is_ambiguous_name(name) {
Some(Check::new( Some(Check::new(
CheckKind::AmbiguousVariableName(name.to_string()), CheckKind::AmbiguousVariableName(name.to_string()),
@ -234,7 +230,7 @@ pub fn check_ambiguous_variable_name(name: &str, location: Range) -> Option<Chec
} }
/// Check AmbiguousClassName compliance. /// Check AmbiguousClassName compliance.
pub fn check_ambiguous_class_name(name: &str, location: Range) -> Option<Check> { pub fn ambiguous_class_name(name: &str, location: Range) -> Option<Check> {
if is_ambiguous_name(name) { if is_ambiguous_name(name) {
Some(Check::new( Some(Check::new(
CheckKind::AmbiguousClassName(name.to_string()), CheckKind::AmbiguousClassName(name.to_string()),
@ -246,7 +242,7 @@ pub fn check_ambiguous_class_name(name: &str, location: Range) -> Option<Check>
} }
/// Check AmbiguousFunctionName compliance. /// Check AmbiguousFunctionName compliance.
pub fn check_ambiguous_function_name(name: &str, location: Range) -> Option<Check> { pub fn ambiguous_function_name(name: &str, location: Range) -> Option<Check> {
if is_ambiguous_name(name) { if is_ambiguous_name(name) {
Some(Check::new( Some(Check::new(
CheckKind::AmbiguousFunctionName(name.to_string()), CheckKind::AmbiguousFunctionName(name.to_string()),
@ -258,11 +254,7 @@ pub fn check_ambiguous_function_name(name: &str, location: Range) -> Option<Chec
} }
/// Check UselessObjectInheritance compliance. /// Check UselessObjectInheritance compliance.
pub fn check_useless_object_inheritance( pub fn useless_object_inheritance(name: &str, bases: &[Expr], scope: &Scope) -> Option<Check> {
name: &str,
bases: &[Expr],
scope: &Scope,
) -> Option<Check> {
for expr in bases { for expr in bases {
if let ExprKind::Name { id, .. } = &expr.node { if let ExprKind::Name { id, .. } = &expr.node {
if id == "object" { if id == "object" {
@ -287,7 +279,7 @@ pub fn check_useless_object_inheritance(
} }
/// Check DefaultExceptNotLast compliance. /// Check DefaultExceptNotLast compliance.
pub fn check_default_except_not_last(handlers: &Vec<Excepthandler>) -> Option<Check> { pub fn default_except_not_last(handlers: &Vec<Excepthandler>) -> Option<Check> {
for (idx, handler) in handlers.iter().enumerate() { for (idx, handler) in handlers.iter().enumerate() {
let ExcepthandlerKind::ExceptHandler { type_, .. } = &handler.node; let ExcepthandlerKind::ExceptHandler { type_, .. } = &handler.node;
if type_.is_none() && idx < handlers.len() - 1 { if type_.is_none() && idx < handlers.len() - 1 {
@ -302,7 +294,7 @@ pub fn check_default_except_not_last(handlers: &Vec<Excepthandler>) -> Option<Ch
} }
/// Check RaiseNotImplemented compliance. /// Check RaiseNotImplemented compliance.
pub fn check_raise_not_implemented(expr: &Expr) -> Option<Check> { pub fn raise_not_implemented(expr: &Expr) -> Option<Check> {
match &expr.node { match &expr.node {
ExprKind::Call { func, .. } => { ExprKind::Call { func, .. } => {
if let ExprKind::Name { id, .. } = &func.node { if let ExprKind::Name { id, .. } = &func.node {
@ -329,7 +321,7 @@ pub fn check_raise_not_implemented(expr: &Expr) -> Option<Check> {
} }
/// Check DuplicateArgumentName compliance. /// Check DuplicateArgumentName compliance.
pub fn check_duplicate_arguments(arguments: &Arguments) -> Vec<Check> { pub fn duplicate_arguments(arguments: &Arguments) -> Vec<Check> {
let mut checks: Vec<Check> = vec![]; let mut checks: Vec<Check> = vec![];
// Collect all the arguments into a single vector. // Collect all the arguments into a single vector.
@ -377,7 +369,7 @@ fn convert_to_value(expr: &Expr) -> Option<DictionaryKey> {
} }
/// Check MultiValueRepeatedKeyLiteral and MultiValueRepeatedKeyVariable compliance. /// Check MultiValueRepeatedKeyLiteral and MultiValueRepeatedKeyVariable compliance.
pub fn check_repeated_keys( pub fn repeated_keys(
keys: &Vec<Expr>, keys: &Vec<Expr>,
check_repeated_literals: bool, check_repeated_literals: bool,
check_repeated_variables: bool, check_repeated_variables: bool,
@ -417,7 +409,7 @@ pub fn check_repeated_keys(
} }
/// Check TrueFalseComparison and NoneComparison compliance. /// Check TrueFalseComparison and NoneComparison compliance.
pub fn check_literal_comparisons( pub fn literal_comparisons(
left: &Expr, left: &Expr,
ops: &Vec<Cmpop>, ops: &Vec<Cmpop>,
comparators: &Vec<Expr>, comparators: &Vec<Expr>,
@ -548,7 +540,7 @@ fn is_constant_non_singleton(expr: &Expr) -> bool {
} }
/// Check IsLiteral compliance. /// Check IsLiteral compliance.
pub fn check_is_literal( pub fn is_literal(
left: &Expr, left: &Expr,
ops: &Vec<Cmpop>, ops: &Vec<Cmpop>,
comparators: &Vec<Expr>, comparators: &Vec<Expr>,
@ -570,11 +562,7 @@ pub fn check_is_literal(
} }
/// Check TypeComparison compliance. /// Check TypeComparison compliance.
pub fn check_type_comparison( pub fn type_comparison(ops: &Vec<Cmpop>, comparators: &Vec<Expr>, location: Range) -> Vec<Check> {
ops: &Vec<Cmpop>,
comparators: &Vec<Expr>,
location: Range,
) -> Vec<Check> {
let mut checks: Vec<Check> = vec![]; let mut checks: Vec<Check> = vec![];
for (op, right) in izip!(ops, comparators) { for (op, right) in izip!(ops, comparators) {
@ -610,7 +598,7 @@ pub fn check_type_comparison(
} }
/// Check TwoStarredExpressions and TooManyExpressionsInStarredAssignment compliance. /// Check TwoStarredExpressions and TooManyExpressionsInStarredAssignment compliance.
pub fn check_starred_expressions( pub fn starred_expressions(
elts: &[Expr], elts: &[Expr],
check_too_many_expressions: bool, check_too_many_expressions: bool,
check_two_starred_expressions: bool, check_two_starred_expressions: bool,
@ -640,7 +628,7 @@ pub fn check_starred_expressions(
} }
/// Check BreakOutsideLoop compliance. /// Check BreakOutsideLoop compliance.
pub fn check_break_outside_loop( pub fn break_outside_loop(
stmt: &Stmt, stmt: &Stmt,
parents: &[&Stmt], parents: &[&Stmt],
parent_stack: &[usize], parent_stack: &[usize],
@ -681,7 +669,7 @@ pub fn check_break_outside_loop(
} }
/// Check ContinueOutsideLoop compliance. /// Check ContinueOutsideLoop compliance.
pub fn check_continue_outside_loop( pub fn continue_outside_loop(
stmt: &Stmt, stmt: &Stmt,
parents: &[&Stmt], parents: &[&Stmt],
parent_stack: &[usize], parent_stack: &[usize],
@ -729,11 +717,7 @@ pub enum ShadowingType {
} }
/// Check builtin name shadowing /// Check builtin name shadowing
pub fn check_builtin_shadowing( pub fn builtin_shadowing(name: &str, location: Range, node_type: ShadowingType) -> Option<Check> {
name: &str,
location: Range,
node_type: ShadowingType,
) -> Option<Check> {
if BUILTINS.contains(&name) { if BUILTINS.contains(&name) {
Some(Check::new( Some(Check::new(
match node_type { match node_type {
@ -1060,7 +1044,7 @@ pub fn unnecessary_subscript_reversal(expr: &Expr, func: &Expr, args: &[Expr]) -
// flake8-super // flake8-super
/// Check that `super()` has no args /// Check that `super()` has no args
pub fn check_super_args( pub fn super_args(
scope: &Scope, scope: &Scope,
parents: &[&Stmt], parents: &[&Stmt],
expr: &Expr, expr: &Expr,
@ -1126,7 +1110,7 @@ pub fn check_super_args(
// flake8-print // flake8-print
/// Check whether a function call is a `print` or `pprint` invocation /// Check whether a function call is a `print` or `pprint` invocation
pub fn check_print_call( pub fn print_call(
expr: &Expr, expr: &Expr,
func: &Expr, func: &Expr,
check_print: bool, check_print: bool,

View File

@ -1,10 +1,11 @@
use crate::ast::helpers::match_name_or_attr;
use rustpython_parser::ast::{ use rustpython_parser::ast::{
Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler,
ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern, ExcepthandlerKind, Expr, ExprContext, ExprKind, Keyword, MatchCase, Operator, Pattern,
PatternKind, Stmt, StmtKind, Unaryop, Withitem, PatternKind, Stmt, StmtKind, Unaryop, Withitem,
}; };
use crate::ast::helpers::match_name_or_attr;
pub trait Visitor<'a> { pub trait Visitor<'a> {
fn visit_stmt(&mut self, stmt: &'a Stmt) { fn visit_stmt(&mut self, stmt: &'a Stmt) {
walk_stmt(self, stmt); walk_stmt(self, stmt);

View File

@ -18,7 +18,7 @@ use crate::ast::types::{
ScopeKind, ScopeKind,
}; };
use crate::ast::visitor::{walk_excepthandler, Visitor}; use crate::ast::visitor::{walk_excepthandler, Visitor};
use crate::ast::{checks, helpers, operations, visitor}; use crate::ast::{checkers, helpers, operations, visitor};
use crate::autofix::{fixer, fixes}; use crate::autofix::{fixer, fixes};
use crate::checks::{Check, CheckCode, CheckKind}; use crate::checks::{Check, CheckCode, CheckKind};
use crate::plugins; use crate::plugins;
@ -192,27 +192,24 @@ where
if self.settings.enabled.contains(&CheckCode::E741) { if self.settings.enabled.contains(&CheckCode::E741) {
let location = self.locate_check(Range::from_located(stmt)); let location = self.locate_check(Range::from_located(stmt));
self.checks.extend( self.checks.extend(
names.iter().filter_map(|name| { names
checks::check_ambiguous_variable_name(name, location) .iter()
}), .filter_map(|name| checkers::ambiguous_variable_name(name, location)),
); );
} }
} }
StmtKind::Break => { StmtKind::Break => {
if self.settings.enabled.contains(&CheckCode::F701) { if self.settings.enabled.contains(&CheckCode::F701) {
if let Some(check) = checks::check_break_outside_loop( if let Some(check) =
stmt, checkers::break_outside_loop(stmt, &self.parents, &self.parent_stack, self)
&self.parents, {
&self.parent_stack,
self,
) {
self.checks.push(check); self.checks.push(check);
} }
} }
} }
StmtKind::Continue => { StmtKind::Continue => {
if self.settings.enabled.contains(&CheckCode::F702) { if self.settings.enabled.contains(&CheckCode::F702) {
if let Some(check) = checks::check_continue_outside_loop( if let Some(check) = checkers::continue_outside_loop(
stmt, stmt,
&self.parents, &self.parents,
&self.parent_stack, &self.parent_stack,
@ -237,7 +234,7 @@ where
.. ..
} => { } => {
if self.settings.enabled.contains(&CheckCode::E743) { if self.settings.enabled.contains(&CheckCode::E743) {
if let Some(check) = checks::check_ambiguous_function_name( if let Some(check) = checkers::ambiguous_function_name(
name, name,
self.locate_check(Range::from_located(stmt)), self.locate_check(Range::from_located(stmt)),
) { ) {
@ -325,7 +322,7 @@ where
} }
if self.settings.enabled.contains(&CheckCode::E742) { if self.settings.enabled.contains(&CheckCode::E742) {
if let Some(check) = checks::check_ambiguous_class_name( if let Some(check) = checkers::ambiguous_class_name(
name, name,
self.locate_check(Range::from_located(stmt)), self.locate_check(Range::from_located(stmt)),
) { ) {
@ -534,7 +531,7 @@ where
StmtKind::Raise { exc, .. } => { StmtKind::Raise { exc, .. } => {
if self.settings.enabled.contains(&CheckCode::F901) { if self.settings.enabled.contains(&CheckCode::F901) {
if let Some(expr) = exc { if let Some(expr) = exc {
if let Some(check) = checks::check_raise_not_implemented(expr) { if let Some(check) = checkers::raise_not_implemented(expr) {
self.checks.push(check); self.checks.push(check);
} }
} }
@ -558,7 +555,7 @@ where
} }
StmtKind::Try { handlers, .. } => { StmtKind::Try { handlers, .. } => {
if self.settings.enabled.contains(&CheckCode::F707) { if self.settings.enabled.contains(&CheckCode::F707) {
if let Some(check) = checks::check_default_except_not_last(handlers) { if let Some(check) = checkers::default_except_not_last(handlers) {
self.checks.push(check); self.checks.push(check);
} }
} }
@ -570,7 +567,7 @@ where
} }
StmtKind::Assign { targets, value, .. } => { StmtKind::Assign { targets, value, .. } => {
if self.settings.enabled.contains(&CheckCode::E731) { if self.settings.enabled.contains(&CheckCode::E731) {
if let Some(check) = checks::check_do_not_assign_lambda( if let Some(check) = checkers::do_not_assign_lambda(
value, value,
self.locate_check(Range::from_located(stmt)), self.locate_check(Range::from_located(stmt)),
) { ) {
@ -584,7 +581,7 @@ where
StmtKind::AnnAssign { value, .. } => { StmtKind::AnnAssign { value, .. } => {
if self.settings.enabled.contains(&CheckCode::E731) { if self.settings.enabled.contains(&CheckCode::E731) {
if let Some(value) = value { if let Some(value) = value {
if let Some(check) = checks::check_do_not_assign_lambda( if let Some(check) = checkers::do_not_assign_lambda(
value, value,
self.locate_check(Range::from_located(stmt)), self.locate_check(Range::from_located(stmt)),
) { ) {
@ -701,7 +698,7 @@ where
self.settings.enabled.contains(&CheckCode::F621); self.settings.enabled.contains(&CheckCode::F621);
let check_two_starred_expressions = let check_two_starred_expressions =
self.settings.enabled.contains(&CheckCode::F622); self.settings.enabled.contains(&CheckCode::F622);
if let Some(check) = checks::check_starred_expressions( if let Some(check) = checkers::starred_expressions(
elts, elts,
check_too_many_expressions, check_too_many_expressions,
check_two_starred_expressions, check_two_starred_expressions,
@ -724,7 +721,7 @@ where
} }
ExprContext::Store => { ExprContext::Store => {
if self.settings.enabled.contains(&CheckCode::E741) { if self.settings.enabled.contains(&CheckCode::E741) {
if let Some(check) = checks::check_ambiguous_variable_name( if let Some(check) = checkers::ambiguous_variable_name(
id, id,
self.locate_check(Range::from_located(expr)), self.locate_check(Range::from_located(expr)),
) { ) {
@ -774,26 +771,26 @@ where
// flake8-comprehensions // flake8-comprehensions
if self.settings.enabled.contains(&CheckCode::C400) { if self.settings.enabled.contains(&CheckCode::C400) {
if let Some(check) = checks::unnecessary_generator_list(expr, func, args) { if let Some(check) = checkers::unnecessary_generator_list(expr, func, args) {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C401) { if self.settings.enabled.contains(&CheckCode::C401) {
if let Some(check) = checks::unnecessary_generator_set(expr, func, args) { if let Some(check) = checkers::unnecessary_generator_set(expr, func, args) {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C402) { if self.settings.enabled.contains(&CheckCode::C402) {
if let Some(check) = checks::unnecessary_generator_dict(expr, func, args) { if let Some(check) = checkers::unnecessary_generator_dict(expr, func, args) {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C403) { if self.settings.enabled.contains(&CheckCode::C403) {
if let Some(check) = if let Some(check) =
checks::unnecessary_list_comprehension_set(expr, func, args) checkers::unnecessary_list_comprehension_set(expr, func, args)
{ {
self.checks.push(check); self.checks.push(check);
}; };
@ -801,27 +798,27 @@ where
if self.settings.enabled.contains(&CheckCode::C404) { if self.settings.enabled.contains(&CheckCode::C404) {
if let Some(check) = if let Some(check) =
checks::unnecessary_list_comprehension_dict(expr, func, args) checkers::unnecessary_list_comprehension_dict(expr, func, args)
{ {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C405) { if self.settings.enabled.contains(&CheckCode::C405) {
if let Some(check) = checks::unnecessary_literal_set(expr, func, args) { if let Some(check) = checkers::unnecessary_literal_set(expr, func, args) {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C406) { if self.settings.enabled.contains(&CheckCode::C406) {
if let Some(check) = checks::unnecessary_literal_dict(expr, func, args) { if let Some(check) = checkers::unnecessary_literal_dict(expr, func, args) {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C408) { if self.settings.enabled.contains(&CheckCode::C408) {
if let Some(check) = if let Some(check) =
checks::unnecessary_collection_call(expr, func, args, keywords) checkers::unnecessary_collection_call(expr, func, args, keywords)
{ {
self.checks.push(check); self.checks.push(check);
}; };
@ -829,7 +826,7 @@ where
if self.settings.enabled.contains(&CheckCode::C409) { if self.settings.enabled.contains(&CheckCode::C409) {
if let Some(check) = if let Some(check) =
checks::unnecessary_literal_within_tuple_call(expr, func, args) checkers::unnecessary_literal_within_tuple_call(expr, func, args)
{ {
self.checks.push(check); self.checks.push(check);
}; };
@ -837,13 +834,14 @@ where
if self.settings.enabled.contains(&CheckCode::C410) { if self.settings.enabled.contains(&CheckCode::C410) {
if let Some(check) = if let Some(check) =
checks::unnecessary_literal_within_list_call(expr, func, args) checkers::unnecessary_literal_within_list_call(expr, func, args)
{ {
self.checks.push(check); self.checks.push(check);
}; };
} }
if self.settings.enabled.contains(&CheckCode::C415) { if self.settings.enabled.contains(&CheckCode::C415) {
if let Some(check) = checks::unnecessary_subscript_reversal(expr, func, args) { if let Some(check) = checkers::unnecessary_subscript_reversal(expr, func, args)
{
self.checks.push(check); self.checks.push(check);
}; };
} }
@ -878,7 +876,7 @@ where
let check_repeated_literals = self.settings.enabled.contains(&CheckCode::F601); let check_repeated_literals = self.settings.enabled.contains(&CheckCode::F601);
let check_repeated_variables = self.settings.enabled.contains(&CheckCode::F602); let check_repeated_variables = self.settings.enabled.contains(&CheckCode::F602);
if check_repeated_literals || check_repeated_variables { if check_repeated_literals || check_repeated_variables {
self.checks.extend(checks::check_repeated_keys( self.checks.extend(checkers::repeated_keys(
keys, keys,
check_repeated_literals, check_repeated_literals,
check_repeated_variables, check_repeated_variables,
@ -930,7 +928,7 @@ where
let check_not_in = self.settings.enabled.contains(&CheckCode::E713); let check_not_in = self.settings.enabled.contains(&CheckCode::E713);
let check_not_is = self.settings.enabled.contains(&CheckCode::E714); let check_not_is = self.settings.enabled.contains(&CheckCode::E714);
if check_not_in || check_not_is { if check_not_in || check_not_is {
self.checks.extend(checks::check_not_tests( self.checks.extend(checkers::not_tests(
op, op,
operand, operand,
check_not_in, check_not_in,
@ -947,7 +945,7 @@ where
let check_none_comparisons = self.settings.enabled.contains(&CheckCode::E711); let check_none_comparisons = self.settings.enabled.contains(&CheckCode::E711);
let check_true_false_comparisons = self.settings.enabled.contains(&CheckCode::E712); let check_true_false_comparisons = self.settings.enabled.contains(&CheckCode::E712);
if check_none_comparisons || check_true_false_comparisons { if check_none_comparisons || check_true_false_comparisons {
self.checks.extend(checks::check_literal_comparisons( self.checks.extend(checkers::literal_comparisons(
left, left,
ops, ops,
comparators, comparators,
@ -958,7 +956,7 @@ where
} }
if self.settings.enabled.contains(&CheckCode::F632) { if self.settings.enabled.contains(&CheckCode::F632) {
self.checks.extend(checks::check_is_literal( self.checks.extend(checkers::is_literal(
left, left,
ops, ops,
comparators, comparators,
@ -967,7 +965,7 @@ where
} }
if self.settings.enabled.contains(&CheckCode::E721) { if self.settings.enabled.contains(&CheckCode::E721) {
self.checks.extend(checks::check_type_comparison( self.checks.extend(checkers::type_comparison(
ops, ops,
comparators, comparators,
self.locate_check(Range::from_located(expr)), self.locate_check(Range::from_located(expr)),
@ -1194,7 +1192,7 @@ where
match name { match name {
Some(name) => { Some(name) => {
if self.settings.enabled.contains(&CheckCode::E741) { if self.settings.enabled.contains(&CheckCode::E741) {
if let Some(check) = checks::check_ambiguous_variable_name( if let Some(check) = checkers::ambiguous_variable_name(
name, name,
self.locate_check(Range::from_located(excepthandler)), self.locate_check(Range::from_located(excepthandler)),
) { ) {
@ -1262,8 +1260,7 @@ where
fn visit_arguments(&mut self, arguments: &'b Arguments) { fn visit_arguments(&mut self, arguments: &'b Arguments) {
if self.settings.enabled.contains(&CheckCode::F831) { if self.settings.enabled.contains(&CheckCode::F831) {
self.checks self.checks.extend(checkers::duplicate_arguments(arguments));
.extend(checks::check_duplicate_arguments(arguments));
} }
// Bind, but intentionally avoid walking default expressions, as we handle them upstream. // Bind, but intentionally avoid walking default expressions, as we handle them upstream.
@ -1296,7 +1293,7 @@ where
); );
if self.settings.enabled.contains(&CheckCode::E741) { if self.settings.enabled.contains(&CheckCode::E741) {
if let Some(check) = checks::check_ambiguous_variable_name( if let Some(check) = checkers::ambiguous_variable_name(
&arg.node.arg, &arg.node.arg,
self.locate_check(Range::from_located(arg)), self.locate_check(Range::from_located(arg)),
) { ) {
@ -1744,7 +1741,7 @@ impl<'a> Checker<'a> {
fn check_deferred_assignments(&mut self) { fn check_deferred_assignments(&mut self) {
if self.settings.enabled.contains(&CheckCode::F841) { if self.settings.enabled.contains(&CheckCode::F841) {
while let Some(index) = self.deferred_assignments.pop() { while let Some(index) = self.deferred_assignments.pop() {
self.checks.extend(checks::check_unused_variables( self.checks.extend(checkers::unused_variables(
&self.scopes[index], &self.scopes[index],
self, self,
&self.settings.dummy_variable_rgx, &self.settings.dummy_variable_rgx,
@ -1899,19 +1896,19 @@ impl<'a> Checker<'a> {
// flake8-builtins // flake8-builtins
if is_attribute && matches!(scope.kind, ScopeKind::Class) { if is_attribute && matches!(scope.kind, ScopeKind::Class) {
if self.settings.enabled.contains(&CheckCode::A003) { if self.settings.enabled.contains(&CheckCode::A003) {
if let Some(check) = checks::check_builtin_shadowing( if let Some(check) = checkers::builtin_shadowing(
name, name,
self.locate_check(location), self.locate_check(location),
checks::ShadowingType::Attribute, checkers::ShadowingType::Attribute,
) { ) {
self.checks.push(check); self.checks.push(check);
} }
} }
} else if self.settings.enabled.contains(&CheckCode::A001) { } else if self.settings.enabled.contains(&CheckCode::A001) {
if let Some(check) = checks::check_builtin_shadowing( if let Some(check) = checkers::builtin_shadowing(
name, name,
self.locate_check(location), self.locate_check(location),
checks::ShadowingType::Variable, checkers::ShadowingType::Variable,
) { ) {
self.checks.push(check); self.checks.push(check);
} }
@ -1920,10 +1917,10 @@ impl<'a> Checker<'a> {
fn check_builtin_arg_shadowing(&mut self, name: &str, location: Range) { fn check_builtin_arg_shadowing(&mut self, name: &str, location: Range) {
if self.settings.enabled.contains(&CheckCode::A002) { if self.settings.enabled.contains(&CheckCode::A002) {
if let Some(check) = checks::check_builtin_shadowing( if let Some(check) = checkers::builtin_shadowing(
name, name,
self.locate_check(location), self.locate_check(location),
checks::ShadowingType::Argument, checkers::ShadowingType::Argument,
) { ) {
self.checks.push(check); self.checks.push(check);
} }

View File

@ -1,10 +1,11 @@
use std::str::FromStr;
use itertools::Itertools; use itertools::Itertools;
use rustpython_parser::ast::Location; use rustpython_parser::ast::Location;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::str::FromStr;
use strum_macros::{AsRefStr, EnumIter, EnumString}; use strum_macros::{AsRefStr, EnumIter, EnumString};
use crate::ast::checks::Primitive; use crate::ast::checkers::Primitive;
use crate::ast::types::Range; use crate::ast::types::Range;
pub const DEFAULT_CHECK_CODES: [CheckCode; 43] = [ pub const DEFAULT_CHECK_CODES: [CheckCode; 43] = [

View File

@ -1,12 +1,12 @@
use rustpython_ast::{Expr, Stmt}; use rustpython_ast::{Expr, Stmt};
use crate::ast::checks; use crate::ast::checkers;
use crate::ast::types::{CheckLocator, Range}; use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker; use crate::check_ast::Checker;
pub fn assert_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) { pub fn assert_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
if let Some(check) = if let Some(check) =
checks::check_assert_tuple(test, checker.locate_check(Range::from_located(stmt))) checkers::assert_tuple(test, checker.locate_check(Range::from_located(stmt)))
{ {
checker.add_check(check); checker.add_check(check);
} }

View File

@ -1,6 +1,6 @@
use itertools::Itertools;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use itertools::Itertools;
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt}; use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt};
use crate::ast::helpers; use crate::ast::helpers;

View File

@ -1,13 +1,11 @@
use rustpython_ast::{Expr, Stmt}; use rustpython_ast::{Expr, Stmt};
use crate::ast::checks; use crate::ast::checkers;
use crate::ast::types::{CheckLocator, Range}; use crate::ast::types::{CheckLocator, Range};
use crate::check_ast::Checker; use crate::check_ast::Checker;
pub fn if_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) { pub fn if_tuple(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
if let Some(check) = if let Some(check) = checkers::if_tuple(test, checker.locate_check(Range::from_located(stmt))) {
checks::check_if_tuple(test, checker.locate_check(Range::from_located(stmt)))
{
checker.add_check(check); checker.add_check(check);
} }
} }

View File

@ -1,13 +1,13 @@
use log::error; use log::error;
use rustpython_ast::{Expr, Stmt, StmtKind}; use rustpython_ast::{Expr, Stmt, StmtKind};
use crate::ast::checks; use crate::ast::checkers;
use crate::autofix::{fixer, fixes}; use crate::autofix::{fixer, fixes};
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::checks::CheckCode; use crate::checks::CheckCode;
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::check_print_call( if let Some(mut check) = checkers::print_call(
expr, expr,
func, func,
checker.settings.enabled.contains(&CheckCode::T201), checker.settings.enabled.contains(&CheckCode::T201),

View File

@ -1,6 +1,6 @@
use rustpython_ast::{Expr, Stmt}; use rustpython_ast::{Expr, Stmt};
use crate::ast::{checks, helpers}; use crate::ast::{checkers, helpers};
use crate::autofix::{fixer, fixes}; use crate::autofix::{fixer, fixes};
use crate::check_ast::Checker; use crate::check_ast::Checker;
@ -19,7 +19,7 @@ pub fn super_call_with_parameters(
.iter() .iter()
.map(|index| checker.parents[*index]) .map(|index| checker.parents[*index])
.collect(); .collect();
if let Some(mut check) = checks::check_super_args(scope, &parents, expr, func, args) { if let Some(mut check) = checkers::super_args(scope, &parents, expr, func, args) {
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
if let Some(fix) = fixes::remove_super_arguments(&mut checker.locator, expr) { if let Some(fix) = fixes::remove_super_arguments(&mut checker.locator, expr) {
check.amend(fix); check.amend(fix);

View File

@ -1,6 +1,6 @@
use rustpython_ast::Expr; use rustpython_ast::Expr;
use crate::ast::checks; use crate::ast::checkers;
use crate::ast::types::{CheckLocator, Range}; use crate::ast::types::{CheckLocator, Range};
use crate::autofix::fixer; use crate::autofix::fixer;
use crate::check_ast::Checker; use crate::check_ast::Checker;
@ -8,7 +8,7 @@ use crate::checks::{CheckKind, Fix};
pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: &Vec<Expr>) { pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: &Vec<Expr>) {
if let Some(mut check) = if let Some(mut check) =
checks::check_type_of_primitive(func, args, checker.locate_check(Range::from_located(expr))) checkers::type_of_primitive(func, args, checker.locate_check(Range::from_located(expr)))
{ {
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
if let CheckKind::TypeOfPrimitive(primitive) = &check.kind { if let CheckKind::TypeOfPrimitive(primitive) = &check.kind {

View File

@ -1,17 +1,15 @@
use rustpython_ast::Expr; use rustpython_ast::Expr;
use crate::ast::checks; use crate::ast::checkers;
use crate::ast::types::{CheckLocator, Range}; use crate::ast::types::{CheckLocator, Range};
use crate::autofix::fixer; use crate::autofix::fixer;
use crate::check_ast::Checker; use crate::check_ast::Checker;
use crate::checks::Fix; use crate::checks::Fix;
pub fn unnecessary_abspath(checker: &mut Checker, expr: &Expr, func: &Expr, args: &Vec<Expr>) { pub fn unnecessary_abspath(checker: &mut Checker, expr: &Expr, func: &Expr, args: &Vec<Expr>) {
if let Some(mut check) = checks::check_unnecessary_abspath( if let Some(mut check) =
func, checkers::unnecessary_abspath(func, args, checker.locate_check(Range::from_located(expr)))
args, {
checker.locate_check(Range::from_located(expr)),
) {
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
check.amend(Fix { check.amend(Fix {
content: "__file__".to_string(), content: "__file__".to_string(),

View File

@ -1,7 +1,7 @@
use log::error; use log::error;
use rustpython_ast::{Expr, Stmt}; use rustpython_ast::{Expr, Stmt};
use crate::ast::checks; use crate::ast::checkers;
use crate::ast::types::{CheckLocator, Range}; use crate::ast::types::{CheckLocator, Range};
use crate::autofix::{fixer, fixes}; use crate::autofix::{fixer, fixes};
use crate::check_ast::Checker; use crate::check_ast::Checker;
@ -12,7 +12,7 @@ pub fn useless_metaclass_type(
value: &Expr, value: &Expr,
targets: &Vec<Expr>, targets: &Vec<Expr>,
) { ) {
if let Some(mut check) = checks::check_useless_metaclass_type( if let Some(mut check) = checkers::useless_metaclass_type(
targets, targets,
value, value,
checker.locate_check(Range::from_located(stmt)), checker.locate_check(Range::from_located(stmt)),

View File

@ -1,6 +1,6 @@
use rustpython_ast::{Expr, Keyword, Stmt}; use rustpython_ast::{Expr, Keyword, Stmt};
use crate::ast::checks; use crate::ast::checkers;
use crate::autofix::{fixer, fixes}; use crate::autofix::{fixer, fixes};
use crate::check_ast::Checker; use crate::check_ast::Checker;
@ -12,7 +12,7 @@ pub fn useless_object_inheritance(
keywords: &[Keyword], keywords: &[Keyword],
) { ) {
let scope = checker.current_scope(); let scope = checker.current_scope();
if let Some(mut check) = checks::check_useless_object_inheritance(name, bases, scope) { if let Some(mut check) = checkers::useless_object_inheritance(name, bases, scope) {
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) { if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
if let Some(fix) = fixes::remove_class_def_base( if let Some(fix) = fixes::remove_class_def_base(
&mut checker.locator, &mut checker.locator,