refactor: use `patch(diagnostic.kind.rule())` (#2336)

This commit is contained in:
Simon Brugman 2023-01-30 03:15:09 +01:00 committed by GitHub
parent 5165b703d9
commit e66fb42d0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 149 additions and 105 deletions

View File

@ -5,7 +5,7 @@ use rustpython_ast::{Comprehension, Constant, Expr, ExprKind, Keyword, Unaryop};
use super::fixes; use super::fixes;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
fn function_name(func: &Expr) -> Option<&str> { fn function_name(func: &Expr) -> Option<&str> {
@ -65,7 +65,7 @@ pub fn unnecessary_generator_list(
violations::UnnecessaryGeneratorList, violations::UnnecessaryGeneratorList,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryGeneratorList) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_generator_list(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -96,7 +96,7 @@ pub fn unnecessary_generator_set(
violations::UnnecessaryGeneratorSet, violations::UnnecessaryGeneratorSet,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryGeneratorSet) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_generator_set(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -126,7 +126,7 @@ pub fn unnecessary_generator_dict(
violations::UnnecessaryGeneratorDict, violations::UnnecessaryGeneratorDict,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryGeneratorDict) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_generator_dict( match fixes::fix_unnecessary_generator_dict(
checker.locator, checker.locator,
checker.stylist, checker.stylist,
@ -164,7 +164,7 @@ pub fn unnecessary_list_comprehension_set(
violations::UnnecessaryListComprehensionSet, violations::UnnecessaryListComprehensionSet,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryListComprehensionSet) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_list_comprehension_set( match fixes::fix_unnecessary_list_comprehension_set(
checker.locator, checker.locator,
checker.stylist, checker.stylist,
@ -207,7 +207,7 @@ pub fn unnecessary_list_comprehension_dict(
violations::UnnecessaryListComprehensionDict, violations::UnnecessaryListComprehensionDict,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryListComprehensionDict) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr) match fixes::fix_unnecessary_list_comprehension_dict(checker.locator, checker.stylist, expr)
{ {
Ok(fix) => { Ok(fix) => {
@ -244,7 +244,7 @@ pub fn unnecessary_literal_set(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryLiteralSet) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_literal_set(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -287,7 +287,7 @@ pub fn unnecessary_literal_dict(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryLiteralDict) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_literal_dict(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -330,7 +330,7 @@ pub fn unnecessary_collection_call(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryCollectionCall) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_collection_call(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -365,7 +365,7 @@ pub fn unnecessary_literal_within_tuple_call(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryLiteralWithinTupleCall) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_literal_within_tuple_call( match fixes::fix_unnecessary_literal_within_tuple_call(
checker.locator, checker.locator,
checker.stylist, checker.stylist,
@ -404,7 +404,7 @@ pub fn unnecessary_literal_within_list_call(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryLiteralWithinListCall) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_literal_within_list_call( match fixes::fix_unnecessary_literal_within_list_call(
checker.locator, checker.locator,
checker.stylist, checker.stylist,
@ -432,7 +432,7 @@ pub fn unnecessary_list_call(checker: &mut Checker, expr: &Expr, func: &Expr, ar
} }
let mut diagnostic = let mut diagnostic =
Diagnostic::new(violations::UnnecessaryListCall, Range::from_located(expr)); Diagnostic::new(violations::UnnecessaryListCall, Range::from_located(expr));
if checker.patch(&Rule::UnnecessaryListCall) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_list_call(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -477,7 +477,7 @@ pub fn unnecessary_call_around_sorted(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryCallAroundSorted) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_call_around_sorted(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);
@ -647,7 +647,7 @@ pub fn unnecessary_comprehension(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::UnnecessaryComprehension) { if checker.patch(diagnostic.kind.rule()) {
match fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr) { match fixes::fix_unnecessary_comprehension(checker.locator, checker.stylist, expr) {
Ok(fix) => { Ok(fix) => {
diagnostic.amend(fix); diagnostic.amend(fix);

View File

@ -12,7 +12,7 @@ use crate::define_violation;
use crate::fix::Fix; use crate::fix::Fix;
use crate::message::Location; use crate::message::Location;
use crate::python::identifiers::is_identifier; use crate::python::identifiers::is_identifier;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violation::{AlwaysAutofixableViolation, Violation}; use crate::violation::{AlwaysAutofixableViolation, Violation};
define_violation!( define_violation!(
@ -112,7 +112,7 @@ pub fn no_unnecessary_pass(checker: &mut Checker, body: &[Stmt]) {
if matches!(pass_stmt.node, StmtKind::Pass) { if matches!(pass_stmt.node, StmtKind::Pass) {
let mut diagnostic = let mut diagnostic =
Diagnostic::new(NoUnnecessaryPass, Range::from_located(pass_stmt)); Diagnostic::new(NoUnnecessaryPass, Range::from_located(pass_stmt));
if checker.patch(&Rule::NoUnnecessaryPass) { if checker.patch(diagnostic.kind.rule()) {
if let Some(index) = match_trailing_comment(pass_stmt, checker.locator) { if let Some(index) = match_trailing_comment(pass_stmt, checker.locator) {
diagnostic.amend(Fix::deletion( diagnostic.amend(Fix::deletion(
pass_stmt.location, pass_stmt.location,
@ -182,7 +182,7 @@ pub fn dupe_class_field_definitions<'a, 'b>(
DupeClassFieldDefinitions(target.to_string()), DupeClassFieldDefinitions(target.to_string()),
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::DupeClassFieldDefinitions) { if checker.patch(diagnostic.kind.rule()) {
let deleted: Vec<&Stmt> = checker let deleted: Vec<&Stmt> = checker
.deletions .deletions
.iter() .iter()
@ -311,7 +311,7 @@ pub fn prefer_list_builtin(checker: &mut Checker, expr: &Expr) {
if let ExprKind::List { elts, .. } = &body.node { if let ExprKind::List { elts, .. } = &body.node {
if elts.is_empty() { if elts.is_empty() {
let mut diagnostic = Diagnostic::new(PreferListBuiltin, Range::from_located(expr)); let mut diagnostic = Diagnostic::new(PreferListBuiltin, Range::from_located(expr));
if checker.patch(&Rule::PreferListBuiltin) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"list".to_string(), "list".to_string(),
expr.location, expr.location,

View File

@ -30,7 +30,7 @@ fn unnecessary_return_none(checker: &mut Checker, stack: &Stack) {
} }
let mut diagnostic = let mut diagnostic =
Diagnostic::new(violations::UnnecessaryReturnNone, Range::from_located(stmt)); Diagnostic::new(violations::UnnecessaryReturnNone, Range::from_located(stmt));
if checker.patch(&Rule::UnnecessaryReturnNone) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"return".to_string(), "return".to_string(),
stmt.location, stmt.location,
@ -49,7 +49,7 @@ fn implicit_return_value(checker: &mut Checker, stack: &Stack) {
} }
let mut diagnostic = let mut diagnostic =
Diagnostic::new(violations::ImplicitReturnValue, Range::from_located(stmt)); Diagnostic::new(violations::ImplicitReturnValue, Range::from_located(stmt));
if checker.patch(&Rule::ImplicitReturnValue) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"return None".to_string(), "return None".to_string(),
stmt.location, stmt.location,
@ -106,7 +106,7 @@ fn implicit_return(checker: &mut Checker, last_stmt: &Stmt) {
_ => { _ => {
let mut diagnostic = let mut diagnostic =
Diagnostic::new(violations::ImplicitReturn, Range::from_located(last_stmt)); Diagnostic::new(violations::ImplicitReturn, Range::from_located(last_stmt));
if checker.patch(&Rule::ImplicitReturn) { if checker.patch(diagnostic.kind.rule()) {
if let Some(indent) = indentation(checker.locator, last_stmt) { if let Some(indent) = indentation(checker.locator, last_stmt) {
let mut content = String::new(); let mut content = String::new();
content.push_str(indent); content.push_str(indent);
@ -228,46 +228,42 @@ fn superfluous_else_node(checker: &mut Checker, stmt: &Stmt, branch: Branch) ->
}; };
for child in body { for child in body {
if matches!(child.node, StmtKind::Return { .. }) { if matches!(child.node, StmtKind::Return { .. }) {
if checker.settings.rules.enabled(&Rule::SuperfluousElseReturn) { let diagnostic = Diagnostic::new(
checker.diagnostics.push(Diagnostic::new( violations::SuperfluousElseReturn { branch },
violations::SuperfluousElseReturn { branch }, elif_else_range(stmt, checker.locator).unwrap_or_else(|| Range::from_located(stmt)),
elif_else_range(stmt, checker.locator) );
.unwrap_or_else(|| Range::from_located(stmt)), if checker.settings.rules.enabled(diagnostic.kind.rule()) {
)); checker.diagnostics.push(diagnostic);
} }
return true; return true;
} }
if matches!(child.node, StmtKind::Break) { if matches!(child.node, StmtKind::Break) {
if checker.settings.rules.enabled(&Rule::SuperfluousElseBreak) { let diagnostic = Diagnostic::new(
checker.diagnostics.push(Diagnostic::new( violations::SuperfluousElseBreak { branch },
violations::SuperfluousElseBreak { branch }, elif_else_range(stmt, checker.locator).unwrap_or_else(|| Range::from_located(stmt)),
elif_else_range(stmt, checker.locator) );
.unwrap_or_else(|| Range::from_located(stmt)), if checker.settings.rules.enabled(diagnostic.kind.rule()) {
)); checker.diagnostics.push(diagnostic);
} }
return true; return true;
} }
if matches!(child.node, StmtKind::Raise { .. }) { if matches!(child.node, StmtKind::Raise { .. }) {
if checker.settings.rules.enabled(&Rule::SuperfluousElseRaise) { let diagnostic = Diagnostic::new(
checker.diagnostics.push(Diagnostic::new( violations::SuperfluousElseRaise { branch },
violations::SuperfluousElseRaise { branch }, elif_else_range(stmt, checker.locator).unwrap_or_else(|| Range::from_located(stmt)),
elif_else_range(stmt, checker.locator) );
.unwrap_or_else(|| Range::from_located(stmt)), if checker.settings.rules.enabled(diagnostic.kind.rule()) {
)); checker.diagnostics.push(diagnostic);
} }
return true; return true;
} }
if matches!(child.node, StmtKind::Continue) { if matches!(child.node, StmtKind::Continue) {
if checker let diagnostic = Diagnostic::new(
.settings violations::SuperfluousElseContinue { branch },
.rules elif_else_range(stmt, checker.locator).unwrap_or_else(|| Range::from_located(stmt)),
.enabled(&Rule::SuperfluousElseContinue) );
{ if checker.settings.rules.enabled(diagnostic.kind.rule()) {
checker.diagnostics.push(Diagnostic::new( checker.diagnostics.push(diagnostic);
violations::SuperfluousElseContinue { branch },
elif_else_range(stmt, checker.locator)
.unwrap_or_else(|| Range::from_located(stmt)),
));
} }
return true; return true;
} }

View File

@ -8,7 +8,7 @@ use crate::ast::helpers::{contains_effect, create_expr, has_comments_in, unparse
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// Return `true` if two `Expr` instances are equivalent names. /// Return `true` if two `Expr` instances are equivalent names.
@ -67,7 +67,7 @@ pub fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::DuplicateIsinstanceCall) { if checker.patch(diagnostic.kind.rule()) {
// Grab the types used in each duplicate `isinstance` call. // Grab the types used in each duplicate `isinstance` call.
let types: Vec<&Expr> = indices let types: Vec<&Expr> = indices
.iter() .iter()
@ -213,7 +213,7 @@ pub fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::CompareWithTuple) { if checker.patch(diagnostic.kind.rule()) {
let unmatched: Vec<Expr> = values let unmatched: Vec<Expr> = values
.iter() .iter()
.enumerate() .enumerate()
@ -280,7 +280,7 @@ pub fn a_and_not_a(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::AAndNotA) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"False".to_string(), "False".to_string(),
expr.location, expr.location,
@ -334,7 +334,7 @@ pub fn a_or_not_a(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::AAndNotA) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"True".to_string(), "True".to_string(),
expr.location, expr.location,
@ -362,7 +362,7 @@ pub fn or_true(checker: &mut Checker, expr: &Expr) {
} = &value.node } = &value.node
{ {
let mut diagnostic = Diagnostic::new(violations::OrTrue, Range::from_located(value)); let mut diagnostic = Diagnostic::new(violations::OrTrue, Range::from_located(value));
if checker.patch(&Rule::AndFalse) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"True".to_string(), "True".to_string(),
expr.location, expr.location,
@ -389,7 +389,7 @@ pub fn and_false(checker: &mut Checker, expr: &Expr) {
} = &value.node } = &value.node
{ {
let mut diagnostic = Diagnostic::new(violations::AndFalse, Range::from_located(value)); let mut diagnostic = Diagnostic::new(violations::AndFalse, Range::from_located(value));
if checker.patch(&Rule::AndFalse) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"False".to_string(), "False".to_string(),
expr.location, expr.location,

View File

@ -4,7 +4,7 @@ use crate::ast::helpers::{create_expr, unparse_expr};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// SIM112 /// SIM112
@ -43,7 +43,7 @@ pub fn use_capital_environment_variables(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(arg), Range::from_located(arg),
); );
if checker.patch(&Rule::UseCapitalEnvironmentVariables) { if checker.patch(diagnostic.kind.rule()) {
let new_env_var = create_expr(ExprKind::Constant { let new_env_var = create_expr(ExprKind::Constant {
value: capital_env_var.into(), value: capital_env_var.into(),
kind: kind.clone(), kind: kind.clone(),
@ -85,7 +85,7 @@ fn check_os_environ_subscript(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(slice), Range::from_located(slice),
); );
if checker.patch(&Rule::UseCapitalEnvironmentVariables) { if checker.patch(diagnostic.kind.rule()) {
let new_env_var = create_expr(ExprKind::Constant { let new_env_var = create_expr(ExprKind::Constant {
value: capital_env_var.into(), value: capital_env_var.into(),
kind: kind.clone(), kind: kind.clone(),

View File

@ -199,7 +199,7 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling:
}, },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::ConvertLoopToAny) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
stmt.location, stmt.location,
@ -249,7 +249,7 @@ pub fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt, sibling:
}, },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::ConvertLoopToAll) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
stmt.location, stmt.location,

View File

@ -9,7 +9,7 @@ use crate::ast::helpers::{
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::flake8_simplify::rules::fix_if; use crate::rules::flake8_simplify::rules::fix_if;
use crate::violations; use crate::violations;
@ -103,7 +103,7 @@ pub fn nested_if_statements(
|colon| Range::new(stmt.location, colon.end_location), |colon| Range::new(stmt.location, colon.end_location),
), ),
); );
if checker.patch(&Rule::NestedIfStatements) { if checker.patch(diagnostic.kind.rule()) {
// The fixer preserves comments in the nested body, but removes comments between // The fixer preserves comments in the nested body, but removes comments between
// the outer and inner if statements. // the outer and inner if statements.
let nested_if = &body[0]; let nested_if = &body[0];
@ -172,7 +172,7 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
violations::ReturnBoolConditionDirectly { cond: condition }, violations::ReturnBoolConditionDirectly { cond: condition },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::ReturnBoolConditionDirectly) if checker.patch(diagnostic.kind.rule())
&& matches!(if_return, Bool::True) && matches!(if_return, Bool::True)
&& matches!(else_return, Bool::False) && matches!(else_return, Bool::False)
&& !has_comments_in(Range::from_located(stmt), checker.locator) && !has_comments_in(Range::from_located(stmt), checker.locator)
@ -296,7 +296,7 @@ pub fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: Option<&
}, },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::UseTernaryOperator) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
stmt.location, stmt.location,
@ -432,7 +432,7 @@ pub fn use_dict_get_with_default(
}, },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::DictGetWithDefault) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
stmt.location, stmt.location,

View File

@ -5,7 +5,7 @@ use super::fix_with;
use crate::ast::helpers::{first_colon_range, has_comments_in}; use crate::ast::helpers::{first_colon_range, has_comments_in};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
fn find_last_with(body: &[Stmt]) -> Option<(&Vec<Withitem>, &Vec<Stmt>)> { fn find_last_with(body: &[Stmt]) -> Option<(&Vec<Withitem>, &Vec<Stmt>)> {
@ -49,7 +49,7 @@ pub fn multiple_with_statements(
|colon| Range::new(with_stmt.location, colon.end_location), |colon| Range::new(with_stmt.location, colon.end_location),
), ),
); );
if checker.patch(&Rule::MultipleWithStatements) { if checker.patch(diagnostic.kind.rule()) {
let nested_with = &with_body[0]; let nested_with = &with_body[0];
if !has_comments_in( if !has_comments_in(
Range::new(with_stmt.location, nested_with.location), Range::new(with_stmt.location, nested_with.location),

View File

@ -11,7 +11,15 @@ expression: diagnostics
end_location: end_location:
row: 1 row: 1
column: 13 column: 13
fix: ~ fix:
content:
- "True"
location:
row: 1
column: 3
end_location:
row: 1
column: 13
parent: ~ parent: ~
- kind: - kind:
AOrNotA: AOrNotA:
@ -22,7 +30,15 @@ expression: diagnostics
end_location: end_location:
row: 4 row: 4
column: 14 column: 14
fix: ~ fix:
content:
- "True"
location:
row: 4
column: 4
end_location:
row: 4
column: 14
parent: ~ parent: ~
- kind: - kind:
AOrNotA: AOrNotA:
@ -33,6 +49,14 @@ expression: diagnostics
end_location: end_location:
row: 7 row: 7
column: 14 column: 14
fix: ~ fix:
content:
- "True"
location:
row: 7
column: 4
end_location:
row: 7
column: 14
parent: ~ parent: ~

View File

@ -10,7 +10,15 @@ expression: diagnostics
end_location: end_location:
row: 1 row: 1
column: 12 column: 12
fix: ~ fix:
content:
- "True"
location:
row: 1
column: 3
end_location:
row: 1
column: 12
parent: ~ parent: ~
- kind: - kind:
OrTrue: ~ OrTrue: ~
@ -20,7 +28,15 @@ expression: diagnostics
end_location: end_location:
row: 4 row: 4
column: 19 column: 19
fix: ~ fix:
content:
- "True"
location:
row: 4
column: 3
end_location:
row: 4
column: 19
parent: ~ parent: ~
- kind: - kind:
OrTrue: ~ OrTrue: ~
@ -30,6 +46,14 @@ expression: diagnostics
end_location: end_location:
row: 7 row: 7
column: 18 column: 18
fix: ~ fix:
content:
- "True"
location:
row: 7
column: 9
end_location:
row: 7
column: 18
parent: ~ parent: ~

View File

@ -4,7 +4,7 @@ use crate::docstrings::definition::Docstring;
use crate::docstrings::styles::SectionStyle; use crate::docstrings::styles::SectionStyle;
use crate::fix::Fix; use crate::fix::Fix;
use crate::message::Location; use crate::message::Location;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line}; use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
use crate::violations; use crate::violations;
@ -48,7 +48,7 @@ pub fn ends_with_period(checker: &mut Checker, docstring: &Docstring) {
Range::from_located(docstring.expr), Range::from_located(docstring.expr),
); );
// Best-effort autofix: avoid adding a period after other punctuation marks. // Best-effort autofix: avoid adding a period after other punctuation marks.
if checker.patch(&Rule::EndsInPeriod) if checker.patch(diagnostic.kind.rule())
&& !trimmed.ends_with(':') && !trimmed.ends_with(':')
&& !trimmed.ends_with(';') && !trimmed.ends_with(';')
{ {

View File

@ -4,7 +4,7 @@ use crate::docstrings::definition::Docstring;
use crate::docstrings::styles::SectionStyle; use crate::docstrings::styles::SectionStyle;
use crate::fix::Fix; use crate::fix::Fix;
use crate::message::Location; use crate::message::Location;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, logical_line}; use crate::rules::pydocstyle::helpers::{leading_quote, logical_line};
use crate::violations; use crate::violations;
@ -47,7 +47,7 @@ pub fn ends_with_punctuation(checker: &mut Checker, docstring: &Docstring) {
Range::from_located(docstring.expr), Range::from_located(docstring.expr),
); );
// Best-effort autofix: avoid adding a period after other punctuation marks. // Best-effort autofix: avoid adding a period after other punctuation marks.
if checker.patch(&Rule::EndsInPunctuation) if checker.patch(diagnostic.kind.rule())
&& !trimmed.ends_with(':') && !trimmed.ends_with(':')
&& !trimmed.ends_with(';') && !trimmed.ends_with(';')
{ {

View File

@ -3,7 +3,7 @@ use crate::ast::whitespace::LinesWithTrailingNewline;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::docstrings::definition::Docstring; use crate::docstrings::definition::Docstring;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers; use crate::rules::pydocstyle::helpers;
use crate::violations; use crate::violations;
@ -26,7 +26,7 @@ pub fn one_liner(checker: &mut Checker, docstring: &Docstring) {
violations::FitsOnOneLine, violations::FitsOnOneLine,
Range::from_located(docstring.expr), Range::from_located(docstring.expr),
); );
if checker.patch(&Rule::FitsOnOneLine) { if checker.patch(diagnostic.kind.rule()) {
if let (Some(leading), Some(trailing)) = ( if let (Some(leading), Some(trailing)) = (
helpers::leading_quote(docstring.contents), helpers::leading_quote(docstring.contents),
helpers::trailing_quote(docstring.contents), helpers::trailing_quote(docstring.contents),

View File

@ -3,7 +3,7 @@ use rustpython_ast::{Expr, ExprKind};
use crate::ast::helpers::find_useless_f_strings; use crate::ast::helpers::find_useless_f_strings;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// F541 /// F541
@ -14,7 +14,7 @@ pub fn f_string_missing_placeholders(expr: &Expr, values: &[Expr], checker: &mut
{ {
for (prefix_range, tok_range) in find_useless_f_strings(expr, checker.locator) { for (prefix_range, tok_range) in find_useless_f_strings(expr, checker.locator) {
let mut diagnostic = Diagnostic::new(violations::FStringMissingPlaceholders, tok_range); let mut diagnostic = Diagnostic::new(violations::FStringMissingPlaceholders, tok_range);
if checker.patch(&Rule::FStringMissingPlaceholders) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::deletion( diagnostic.amend(Fix::deletion(
prefix_range.location, prefix_range.location,
prefix_range.end_location, prefix_range.end_location,

View File

@ -55,7 +55,7 @@ pub fn repeated_keys(checker: &mut Checker, keys: &[Option<Expr>], values: &[Exp
Range::from_located(key), Range::from_located(key),
); );
if is_duplicate_value { if is_duplicate_value {
if checker.patch(&Rule::MultiValueRepeatedKeyLiteral) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::deletion( diagnostic.amend(Fix::deletion(
values[i - 1].end_location.unwrap(), values[i - 1].end_location.unwrap(),
values[i].end_location.unwrap(), values[i].end_location.unwrap(),
@ -83,7 +83,7 @@ pub fn repeated_keys(checker: &mut Checker, keys: &[Option<Expr>], values: &[Exp
Range::from_located(key), Range::from_located(key),
); );
if is_duplicate_value { if is_duplicate_value {
if checker.patch(&Rule::MultiValueRepeatedKeyVariable) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::deletion( diagnostic.amend(Fix::deletion(
values[i - 1].end_location.unwrap(), values[i - 1].end_location.unwrap(),
values[i].end_location.unwrap(), values[i].end_location.unwrap(),

View File

@ -9,7 +9,7 @@ use crate::ast::types::{BindingKind, Range, RefEquality, ScopeKind};
use crate::autofix::helpers::delete_stmt; use crate::autofix::helpers::delete_stmt;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::source_code::Locator; use crate::source_code::Locator;
use crate::violations; use crate::violations;
@ -179,7 +179,7 @@ pub fn unused_variable(checker: &mut Checker, scope: usize) {
}, },
binding.range, binding.range,
); );
if checker.patch(&Rule::UnusedVariable) { if checker.patch(diagnostic.kind.rule()) {
if let Some(stmt) = binding.source.as_ref().map(std::convert::Into::into) { if let Some(stmt) = binding.source.as_ref().map(std::convert::Into::into) {
if let Some((kind, fix)) = remove_unused_variable(stmt, &binding.range, checker) if let Some((kind, fix)) = remove_unused_variable(stmt, &binding.range, checker)
{ {

View File

@ -4,7 +4,7 @@ use crate::ast::helpers::collect_call_path;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// UP017 /// UP017
@ -17,7 +17,7 @@ pub fn datetime_utc_alias(checker: &mut Checker, expr: &Expr) {
violations::DatetimeTimezoneUTC { straight_import }, violations::DatetimeTimezoneUTC { straight_import },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::DatetimeTimezoneUTC) { if checker.patch(diagnostic.kind.rule()) {
if straight_import { if straight_import {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"datetime.UTC".to_string(), "datetime.UTC".to_string(),

View File

@ -9,7 +9,7 @@ use rustpython_parser::lexer::Tok;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote}; use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
use crate::rules::pyflakes::format::FormatSummary; use crate::rules::pyflakes::format::FormatSummary;
use crate::rules::pyupgrade::helpers::curly_escape; use crate::rules::pyupgrade::helpers::curly_escape;
@ -258,7 +258,7 @@ pub(crate) fn f_strings(checker: &mut Checker, summary: &FormatSummary, expr: &E
} }
let mut diagnostic = Diagnostic::new(violations::FString, Range::from_located(expr)); let mut diagnostic = Diagnostic::new(violations::FString, Range::from_located(expr));
if checker.patch(&Rule::FString) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
expr.location, expr.location,

View File

@ -5,7 +5,7 @@ use crate::ast::helpers::{create_expr, unparse_expr};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// UP033 /// UP033
@ -40,7 +40,7 @@ pub fn functools_cache(checker: &mut Checker, decorator_list: &[Expr]) {
violations::FunctoolsCache, violations::FunctoolsCache,
Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), Range::new(func.end_location.unwrap(), expr.end_location.unwrap()),
); );
if checker.patch(&Rule::FunctoolsCache) { if checker.patch(diagnostic.kind.rule()) {
if let ExprKind::Attribute { value, ctx, .. } = &func.node { if let ExprKind::Attribute { value, ctx, .. } = &func.node {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
unparse_expr( unparse_expr(

View File

@ -5,7 +5,7 @@ use crate::ast::helpers::unparse_expr;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// UP011 /// UP011
@ -30,7 +30,7 @@ pub fn lru_cache_without_parameters(checker: &mut Checker, decorator_list: &[Exp
violations::LRUCacheWithoutParameters, violations::LRUCacheWithoutParameters,
Range::new(func.end_location.unwrap(), expr.end_location.unwrap()), Range::new(func.end_location.unwrap(), expr.end_location.unwrap()),
); );
if checker.patch(&Rule::LRUCacheWithoutParameters) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
unparse_expr(func, checker.stylist), unparse_expr(func, checker.stylist),
expr.location, expr.location,

View File

@ -5,7 +5,7 @@ use rustpython_parser::lexer::Tok;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
use crate::violations::LiteralType; use crate::violations::LiteralType;
@ -30,7 +30,7 @@ pub fn native_literals(
} else { } else {
LiteralType::Bytes LiteralType::Bytes
}}, Range::from_located(expr)); }}, Range::from_located(expr));
if checker.patch(&Rule::NativeLiterals) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
if id == "bytes" { if id == "bytes" {
let mut content = String::with_capacity(3); let mut content = String::with_capacity(3);
@ -103,7 +103,7 @@ pub fn native_literals(
}, },
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::NativeLiterals) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
arg_code.to_string(), arg_code.to_string(),
expr.location, expr.location,

View File

@ -3,7 +3,7 @@ use rustpython_ast::Expr;
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// UP020 /// UP020
@ -13,7 +13,7 @@ pub fn open_alias(checker: &mut Checker, expr: &Expr, func: &Expr) {
.map_or(false, |call_path| call_path.as_slice() == ["io", "open"]) .map_or(false, |call_path| call_path.as_slice() == ["io", "open"])
{ {
let mut diagnostic = Diagnostic::new(violations::OpenAlias, Range::from_located(expr)); let mut diagnostic = Diagnostic::new(violations::OpenAlias, Range::from_located(expr));
if checker.patch(&Rule::OpenAlias) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"open".to_string(), "open".to_string(),
func.location, func.location,

View File

@ -14,7 +14,7 @@ use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::python::identifiers::is_identifier; use crate::python::identifiers::is_identifier;
use crate::python::keyword::KWLIST; use crate::python::keyword::KWLIST;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote}; use crate::rules::pydocstyle::helpers::{leading_quote, trailing_quote};
use crate::rules::pyupgrade::helpers::curly_escape; use crate::rules::pyupgrade::helpers::curly_escape;
use crate::violations; use crate::violations;
@ -410,7 +410,7 @@ pub(crate) fn printf_string_formatting(
violations::PrintfStringFormatting, violations::PrintfStringFormatting,
Range::from_located(expr), Range::from_located(expr),
); );
if checker.patch(&Rule::PrintfStringFormatting) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
contents, contents,
expr.location, expr.location,

View File

@ -214,7 +214,7 @@ pub fn rewrite_mock_attribute(checker: &mut Checker, expr: &Expr) {
}, },
Range::from_located(value), Range::from_located(value),
); );
if checker.patch(&Rule::RewriteMockImport) { if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement( diagnostic.amend(Fix::replacement(
"mock".to_string(), "mock".to_string(),
value.location, value.location,
@ -289,7 +289,7 @@ pub fn rewrite_mock_import(checker: &mut Checker, stmt: &Stmt) {
}, },
Range::from_located(stmt), Range::from_located(stmt),
); );
if checker.patch(&Rule::RewriteMockImport) { if checker.patch(diagnostic.kind.rule()) {
if let Some(indent) = indentation(checker.locator, stmt) { if let Some(indent) = indentation(checker.locator, stmt) {
match format_import_from(stmt, indent, checker.locator, checker.stylist) { match format_import_from(stmt, indent, checker.locator, checker.stylist) {
Ok(content) => { Ok(content) => {

View File

@ -3,7 +3,7 @@ use rustpython_ast::{Expr, ExprKind};
use crate::ast::types::Range; use crate::ast::types::Range;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::fix::Fix; use crate::fix::Fix;
use crate::registry::{Diagnostic, Rule}; use crate::registry::Diagnostic;
use crate::violations; use crate::violations;
/// Returns `true` if `expr` contains an `ExprKind::Await`. /// Returns `true` if `expr` contains an `ExprKind::Await`.
@ -83,7 +83,7 @@ pub fn unpack_list_comprehension(checker: &mut Checker, targets: &[Expr], value:
violations::RewriteListComprehension, violations::RewriteListComprehension,
Range::from_located(value), Range::from_located(value),
); );
if checker.patch(&Rule::RewriteListComprehension) { if checker.patch(diagnostic.kind.rule()) {
let existing = checker let existing = checker
.locator .locator
.slice_source_code_range(&Range::from_located(value)); .slice_source_code_range(&Range::from_located(value));