From 3810250bb6072aebc662580f9849f45d86dce6b5 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 18 Jan 2023 00:35:09 +0100 Subject: [PATCH] rule 2/8: Rename DiagnosticKind::code to rule --- ruff_cli/src/printer.rs | 20 +++---- ruff_dev/src/generate_rules_table.rs | 2 +- ruff_macros/src/define_rule_mapping.rs | 4 +- src/checkers/noqa.rs | 16 +++--- src/checkers/tokens.rs | 6 +- src/lib_wasm.rs | 2 +- src/linter.rs | 2 +- src/noqa.rs | 2 +- src/rules/flake8_annotations/rules.rs | 2 +- .../flake8_bugbear/rules/assert_false.rs | 2 +- .../rules/duplicate_exceptions.rs | 2 +- .../rules/getattr_with_constant.rs | 2 +- .../redundant_tuple_in_exception_handler.rs | 2 +- .../rules/setattr_with_constant.rs | 2 +- .../rules/unused_loop_control_variable.rs | 2 +- src/rules/flake8_print/rules/print_call.rs | 4 +- .../flake8_pytest_style/rules/assertion.rs | 2 +- .../flake8_pytest_style/rules/fixture.rs | 8 +-- src/rules/flake8_pytest_style/rules/marks.rs | 4 +- .../flake8_pytest_style/rules/parametrize.rs | 14 ++--- src/rules/flake8_simplify/rules/ast_ifexp.rs | 6 +- .../flake8_simplify/rules/ast_unary_op.rs | 6 +- .../flake8_simplify/rules/key_in_dict.rs | 2 +- .../flake8_simplify/rules/yoda_conditions.rs | 2 +- src/rules/isort/rules/organize_imports.rs | 2 +- src/rules/pandas_vet/mod.rs | 2 +- src/rules/pycodestyle/rules.rs | 22 ++++---- src/rules/pydocstyle/rules.rs | 50 ++++++++--------- src/rules/pyflakes/mod.rs | 2 +- .../rules/invalid_literal_comparisons.rs | 2 +- .../pyflakes/rules/raise_not_implemented.rs | 2 +- src/rules/pyflakes/rules/strings.rs | 4 +- .../rules/misplaced_comparison_constant.rs | 56 +++++++++++++++++++ src/rules/pylint/rules/use_sys_exit.rs | 2 +- .../pylint/rules/useless_import_alias.rs | 2 +- ...convert_named_tuple_functional_to_class.rs | 2 +- .../convert_typed_dict_functional_to_class.rs | 2 +- .../rules/deprecated_unittest_alias.rs | 2 +- src/rules/pyupgrade/rules/format_literals.rs | 2 +- src/rules/pyupgrade/rules/os_error_alias.rs | 2 +- .../pyupgrade/rules/replace_stdout_stderr.rs | 2 +- .../rules/replace_universal_newlines.rs | 2 +- .../pyupgrade/rules/rewrite_c_element_tree.rs | 2 +- .../rules/rewrite_unicode_literal.rs | 2 +- .../pyupgrade/rules/rewrite_yield_from.rs | 2 +- .../rules/super_call_with_parameters.rs | 2 +- .../pyupgrade/rules/type_of_primitive.rs | 2 +- .../pyupgrade/rules/typing_text_str_alias.rs | 2 +- .../rules/unnecessary_builtin_import.rs | 2 +- .../rules/unnecessary_future_import.rs | 2 +- .../pyupgrade/rules/use_pep585_annotation.rs | 2 +- .../pyupgrade/rules/use_pep604_annotation.rs | 4 +- .../pyupgrade/rules/useless_metaclass_type.rs | 2 +- .../rules/useless_object_inheritance.rs | 2 +- src/rules/ruff/rules.rs | 4 +- 55 files changed, 181 insertions(+), 125 deletions(-) create mode 100644 src/rules/pylint/rules/misplaced_comparison_constant.rs diff --git a/ruff_cli/src/printer.rs b/ruff_cli/src/printer.rs index 1ffe493f95..a2ee2d785f 100644 --- a/ruff_cli/src/printer.rs +++ b/ruff_cli/src/printer.rs @@ -143,7 +143,7 @@ impl<'a> Printer<'a> { .messages .iter() .map(|message| ExpandedMessage { - code: message.kind.code(), + code: message.kind.rule(), message: message.kind.body(), fix: message.fix.as_ref().map(|fix| ExpandedFix { content: &fix.content, @@ -178,7 +178,7 @@ impl<'a> Printer<'a> { message.kind.body() )); let mut case = - TestCase::new(format!("org.ruff.{}", message.kind.code()), status); + TestCase::new(format!("org.ruff.{}", message.kind.rule()), status); let file_path = Path::new(filename); let file_stem = file_path.file_stem().unwrap().to_str().unwrap(); let classname = file_path.parent().unwrap().join(file_stem); @@ -248,14 +248,14 @@ impl<'a> Printer<'a> { ":", message.location.column(), ":", - message.kind.code().as_ref(), + message.kind.rule().as_ref(), message.kind.body(), ); writeln!( stdout, "::error title=Ruff \ ({}),file={},line={},col={},endLine={},endColumn={}::{}", - message.kind.code(), + message.kind.rule(), message.filename, message.location.row(), message.location.column(), @@ -276,9 +276,9 @@ impl<'a> Printer<'a> { .iter() .map(|message| { json!({ - "description": format!("({}) {}", message.kind.code(), message.kind.body()), + "description": format!("({}) {}", message.kind.rule(), message.kind.body()), "severity": "major", - "fingerprint": message.kind.code(), + "fingerprint": message.kind.rule(), "location": { "path": message.filename, "lines": { @@ -361,7 +361,7 @@ fn print_message(stdout: &mut T, message: &Message) -> Result<()> { ":".cyan(), message.location.column(), ":".cyan(), - message.kind.code().as_ref().red().bold(), + message.kind.rule().as_ref().red().bold(), message.kind.body(), ); writeln!(stdout, "{label}")?; @@ -388,7 +388,7 @@ fn print_message(stdout: &mut T, message: &Message) -> Result<()> { source: &source.contents, line_start: message.location.row(), annotations: vec![SourceAnnotation { - label: message.kind.code().as_ref(), + label: message.kind.rule().as_ref(), annotation_type: AnnotationType::Error, range: source.range, }], @@ -425,7 +425,7 @@ fn print_grouped_message( ":".cyan(), message.location.column(), " ".repeat(column_length - num_digits(message.location.column())), - message.kind.code().as_ref().red().bold(), + message.kind.rule().as_ref().red().bold(), message.kind.body(), ); writeln!(stdout, "{label}")?; @@ -452,7 +452,7 @@ fn print_grouped_message( source: &source.contents, line_start: message.location.row(), annotations: vec![SourceAnnotation { - label: message.kind.code().as_ref(), + label: message.kind.rule().as_ref(), annotation_type: AnnotationType::Error, range: source.range, }], diff --git a/ruff_dev/src/generate_rules_table.rs b/ruff_dev/src/generate_rules_table.rs index ff8eba4ef1..50b725381d 100644 --- a/ruff_dev/src/generate_rules_table.rs +++ b/ruff_dev/src/generate_rules_table.rs @@ -30,7 +30,7 @@ fn generate_table(table_out: &mut String, prefix: &RuleCodePrefix) { let fix_token = if kind.fixable() { "🛠" } else { "" }; table_out.push_str(&format!( "| {} | {} | {} | {} |", - kind.code().as_ref(), + kind.rule().as_ref(), kind.as_ref(), kind.summary().replace('|', r"\|"), fix_token diff --git a/ruff_macros/src/define_rule_mapping.rs b/ruff_macros/src/define_rule_mapping.rs index 1555d595e2..66911ec8d0 100644 --- a/ruff_macros/src/define_rule_mapping.rs +++ b/ruff_macros/src/define_rule_mapping.rs @@ -80,8 +80,8 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream { impl DiagnosticKind { - /// A four-letter shorthand code for the diagnostic. - pub fn code(&self) -> &'static RuleCode { + /// The rule of the diagnostic. + pub fn rule(&self) -> &'static Rule { match self { #diagkind_code_match_arms } } diff --git a/src/checkers/noqa.rs b/src/checkers/noqa.rs index e2cadacb7f..2a9d9c7da4 100644 --- a/src/checkers/noqa.rs +++ b/src/checkers/noqa.rs @@ -56,13 +56,13 @@ pub fn check_noqa( }); match noqa { (Directive::All(..), matches) => { - matches.push(diagnostic.kind.code().as_ref()); + matches.push(diagnostic.kind.rule().as_ref()); ignored.push(index); continue; } (Directive::Codes(.., codes), matches) => { - if noqa::includes(diagnostic.kind.code(), codes) { - matches.push(diagnostic.kind.code().as_ref()); + if noqa::includes(diagnostic.kind.rule(), codes) { + matches.push(diagnostic.kind.rule().as_ref()); ignored.push(index); continue; } @@ -83,12 +83,12 @@ pub fn check_noqa( .or_insert_with(|| (noqa::extract_noqa_directive(lines[noqa_lineno - 1]), vec![])); match noqa { (Directive::All(..), matches) => { - matches.push(diagnostic.kind.code().as_ref()); + matches.push(diagnostic.kind.rule().as_ref()); ignored.push(index); } (Directive::Codes(.., codes), matches) => { - if noqa::includes(diagnostic.kind.code(), codes) { - matches.push(diagnostic.kind.code().as_ref()); + if noqa::includes(diagnostic.kind.rule(), codes) { + matches.push(diagnostic.kind.rule().as_ref()); ignored.push(index); } } @@ -108,7 +108,7 @@ pub fn check_noqa( Range::new(Location::new(row + 1, start), Location::new(row + 1, end)), ); if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.code()) + && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::deletion( Location::new(row + 1, start - spaces), @@ -172,7 +172,7 @@ pub fn check_noqa( Range::new(Location::new(row + 1, start), Location::new(row + 1, end)), ); if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.code()) + && settings.rules.should_fix(diagnostic.kind.rule()) { if valid_codes.is_empty() { diagnostic.amend(Fix::deletion( diff --git a/src/checkers/tokens.rs b/src/checkers/tokens.rs index 804aefda0f..706cc1b90c 100644 --- a/src/checkers/tokens.rs +++ b/src/checkers/tokens.rs @@ -75,7 +75,7 @@ pub fn check_tokens( settings, autofix, ) { - if settings.rules.enabled(diagnostic.kind.code()) { + if settings.rules.enabled(diagnostic.kind.rule()) { diagnostics.push(diagnostic); } } @@ -112,7 +112,7 @@ pub fn check_tokens( diagnostics.extend( flake8_implicit_str_concat::rules::implicit(tokens) .into_iter() - .filter(|diagnostic| settings.rules.enabled(diagnostic.kind.code())), + .filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())), ); } @@ -121,7 +121,7 @@ pub fn check_tokens( diagnostics.extend( flake8_commas::rules::trailing_commas(tokens, settings, autofix) .into_iter() - .filter(|diagnostic| settings.rules.enabled(diagnostic.kind.code())), + .filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())), ); } diff --git a/src/lib_wasm.rs b/src/lib_wasm.rs index 2bac397c78..0d76cafc06 100644 --- a/src/lib_wasm.rs +++ b/src/lib_wasm.rs @@ -182,7 +182,7 @@ pub fn check(contents: &str, options: JsValue) -> Result { let messages: Vec = diagnostics .into_iter() .map(|diagnostic| ExpandedMessage { - code: diagnostic.kind.code().clone(), + code: diagnostic.kind.rule().clone(), message: diagnostic.kind.body(), location: diagnostic.location, end_location: diagnostic.end_location, diff --git a/src/linter.rs b/src/linter.rs index 0d71f3194b..25ea55ac09 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -170,7 +170,7 @@ pub fn check_path( if !ignores.is_empty() { return Ok(diagnostics .into_iter() - .filter(|diagnostic| !ignores.contains(&diagnostic.kind.code())) + .filter(|diagnostic| !ignores.contains(&diagnostic.kind.rule())) .collect()); } } diff --git a/src/noqa.rs b/src/noqa.rs index f164598f00..5835b3cf34 100644 --- a/src/noqa.rs +++ b/src/noqa.rs @@ -115,7 +115,7 @@ fn add_noqa_inner( // duplication, whereby some parent `noqa` directives become // redundant. if diagnostic.location.row() == lineno + 1 { - codes.insert(diagnostic.kind.code()); + codes.insert(diagnostic.kind.rule()); } } diff --git a/src/rules/flake8_annotations/rules.rs b/src/rules/flake8_annotations/rules.rs index 212c749872..0c5f27226b 100644 --- a/src/rules/flake8_annotations/rules.rs +++ b/src/rules/flake8_annotations/rules.rs @@ -330,7 +330,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V violations::MissingReturnTypeSpecialMethod(name.to_string()), helpers::identifier_range(stmt, checker.locator), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match fixes::add_return_none_annotation(checker.locator, stmt) { Ok(fix) => { diagnostic.amend(fix); diff --git a/src/rules/flake8_bugbear/rules/assert_false.rs b/src/rules/flake8_bugbear/rules/assert_false.rs index 9bfb9a2e5e..f356d9592d 100644 --- a/src/rules/flake8_bugbear/rules/assert_false.rs +++ b/src/rules/flake8_bugbear/rules/assert_false.rs @@ -47,7 +47,7 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option }; let mut diagnostic = Diagnostic::new(violations::DoNotAssertFalse, Range::from_located(test)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_stmt(&assertion_error(msg)); diagnostic.amend(Fix::replacement( diff --git a/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs b/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs index c4691569d2..fb3b583174 100644 --- a/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs +++ b/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs @@ -54,7 +54,7 @@ fn duplicate_handler_exceptions<'a>( ), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); if unique_elts.len() == 1 { generator.unparse_expr(unique_elts[0], 0); diff --git a/src/rules/flake8_bugbear/rules/getattr_with_constant.rs b/src/rules/flake8_bugbear/rules/getattr_with_constant.rs index 79d9952497..e7a9e9501c 100644 --- a/src/rules/flake8_bugbear/rules/getattr_with_constant.rs +++ b/src/rules/flake8_bugbear/rules/getattr_with_constant.rs @@ -47,7 +47,7 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar let mut diagnostic = Diagnostic::new(violations::GetAttrWithConstant, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr(&attribute(obj, value), 0); diagnostic.amend(Fix::replacement( diff --git a/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs b/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs index 776ff1870f..91d23be6de 100644 --- a/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs +++ b/src/rules/flake8_bugbear/rules/redundant_tuple_in_exception_handler.rs @@ -23,7 +23,7 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E violations::RedundantTupleInExceptionHandler(elt.to_string()), Range::from_located(type_), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr(elt, 0); diagnostic.amend(Fix::replacement( diff --git a/src/rules/flake8_bugbear/rules/setattr_with_constant.rs b/src/rules/flake8_bugbear/rules/setattr_with_constant.rs index 2ffaf2279d..5111994638 100644 --- a/src/rules/flake8_bugbear/rules/setattr_with_constant.rs +++ b/src/rules/flake8_bugbear/rules/setattr_with_constant.rs @@ -62,7 +62,7 @@ pub fn setattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar if expr == child.as_ref() { let mut diagnostic = Diagnostic::new(violations::SetAttrWithConstant, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( assignment(obj, name, value, checker.stylist), expr.location, diff --git a/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs b/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs index dea062c702..7e0389a9ee 100644 --- a/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs +++ b/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs @@ -66,7 +66,7 @@ pub fn unused_loop_control_variable(checker: &mut Checker, target: &Expr, body: violations::UnusedLoopControlVariable(name.to_string()), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Prefix the variable name with an underscore. diagnostic.amend(Fix::replacement( format!("_{name}"), diff --git a/src/rules/flake8_print/rules/print_call.rs b/src/rules/flake8_print/rules/print_call.rs index b368c868e9..44153e2a4d 100644 --- a/src/rules/flake8_print/rules/print_call.rs +++ b/src/rules/flake8_print/rules/print_call.rs @@ -44,11 +44,11 @@ pub fn print_call(checker: &mut Checker, func: &Expr, keywords: &[Keyword]) { } }; - if !checker.settings.rules.enabled(diagnostic.kind.code()) { + if !checker.settings.rules.enabled(diagnostic.kind.rule()) { return; } - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let defined_by = checker.current_stmt(); let defined_in = checker.current_stmt_parent(); if matches!(defined_by.node, StmtKind::Expr { .. }) { diff --git a/src/rules/flake8_pytest_style/rules/assertion.rs b/src/rules/flake8_pytest_style/rules/assertion.rs index 88f2bfb5b5..748e1ae149 100644 --- a/src/rules/flake8_pytest_style/rules/assertion.rs +++ b/src/rules/flake8_pytest_style/rules/assertion.rs @@ -103,7 +103,7 @@ pub fn unittest_assertion( violations::UnittestAssertion(unittest_assert.to_string()), Range::from_located(func), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Ok(stmt) = unittest_assert.generate_assert(args, keywords) { diagnostic.amend(Fix::replacement( unparse_stmt(&stmt, checker.stylist), diff --git a/src/rules/flake8_pytest_style/rules/fixture.rs b/src/rules/flake8_pytest_style/rules/fixture.rs index 3b63be080a..653bb397bc 100644 --- a/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/src/rules/flake8_pytest_style/rules/fixture.rs @@ -83,7 +83,7 @@ fn pytest_fixture_parentheses( violations::IncorrectFixtureParenthesesStyle(preferred.to_string(), actual.to_string()), Range::from_located(decorator), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(fix); } checker.diagnostics.push(diagnostic); @@ -180,7 +180,7 @@ fn check_fixture_returns(checker: &mut Checker, func: &Stmt, func_name: &str, bo violations::UselessYieldFixture(func_name.to_string()), Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( "return".to_string(), stmt.location, @@ -252,7 +252,7 @@ fn check_fixture_marks(checker: &mut Checker, decorators: &[Expr]) { violations::UnnecessaryAsyncioMarkOnFixture, Range::from_located(mark), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let start = Location::new(mark.location.row(), 0); let end = Location::new(mark.end_location.unwrap().row() + 1, 0); diagnostic.amend(Fix::deletion(start, end)); @@ -267,7 +267,7 @@ fn check_fixture_marks(checker: &mut Checker, decorators: &[Expr]) { violations::ErroneousUseFixturesOnFixture, Range::from_located(mark), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let start = Location::new(mark.location.row(), 0); let end = Location::new(mark.end_location.unwrap().row() + 1, 0); diagnostic.amend(Fix::deletion(start, end)); diff --git a/src/rules/flake8_pytest_style/rules/marks.rs b/src/rules/flake8_pytest_style/rules/marks.rs index 4e46554377..e939d638a7 100644 --- a/src/rules/flake8_pytest_style/rules/marks.rs +++ b/src/rules/flake8_pytest_style/rules/marks.rs @@ -22,7 +22,7 @@ fn pytest_mark_parentheses( ), Range::from_located(decorator), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(fix); } checker.diagnostics.push(diagnostic); @@ -75,7 +75,7 @@ fn check_useless_usefixtures(checker: &mut Checker, decorator: &Expr) { violations::UseFixturesWithoutParameters, Range::from_located(decorator), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let at_start = Location::new(decorator.location.row(), decorator.location.column() - 1); diagnostic.amend(Fix::deletion(at_start, decorator.end_location.unwrap())); } diff --git a/src/rules/flake8_pytest_style/rules/parametrize.rs b/src/rules/flake8_pytest_style/rules/parametrize.rs index ceddbdf6b7..ae9f302dd4 100644 --- a/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -84,7 +84,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr( &create_expr(ExprKind::Tuple { @@ -114,7 +114,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr( &create_expr(ExprKind::List { @@ -156,7 +156,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr( &create_expr(ExprKind::List { @@ -178,7 +178,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(content) = elts_to_csv(elts, checker) { diagnostic.amend(Fix::replacement( content, @@ -205,7 +205,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr( &create_expr(ExprKind::Tuple { @@ -227,7 +227,7 @@ fn check_names(checker: &mut Checker, expr: &Expr) { violations::ParametrizeNamesWrongType(names_type), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(content) = elts_to_csv(elts, checker) { diagnostic.amend(Fix::replacement( content, @@ -283,7 +283,7 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) { Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr(&create_expr(value.node.clone()), 0); diagnostic.amend(Fix::replacement( diff --git a/src/rules/flake8_simplify/rules/ast_ifexp.rs b/src/rules/flake8_simplify/rules/ast_ifexp.rs index 9b77f947de..1b642b43cf 100644 --- a/src/rules/flake8_simplify/rules/ast_ifexp.rs +++ b/src/rules/flake8_simplify/rules/ast_ifexp.rs @@ -32,7 +32,7 @@ pub fn explicit_true_false_in_ifexpr( violations::IfExprWithTrueFalse(unparse_expr(test, checker.stylist)), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr( &create_expr(ExprKind::Call { @@ -77,7 +77,7 @@ pub fn explicit_false_true_in_ifexpr( violations::IfExprWithFalseTrue(unparse_expr(test, checker.stylist)), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr( &create_expr(ExprKind::UnaryOp { @@ -126,7 +126,7 @@ pub fn twisted_arms_in_ifexpr( ), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr( &create_expr(ExprKind::IfExp { diff --git a/src/rules/flake8_simplify/rules/ast_unary_op.rs b/src/rules/flake8_simplify/rules/ast_unary_op.rs index 88fae394d8..44d71d767c 100644 --- a/src/rules/flake8_simplify/rules/ast_unary_op.rs +++ b/src/rules/flake8_simplify/rules/ast_unary_op.rs @@ -42,7 +42,7 @@ pub fn negation_with_equal_op(checker: &mut Checker, expr: &Expr, op: &Unaryop, ), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr( &create_expr(ExprKind::Compare { @@ -86,7 +86,7 @@ pub fn negation_with_not_equal_op( ), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr( &create_expr(ExprKind::Compare { @@ -119,7 +119,7 @@ pub fn double_negation(checker: &mut Checker, expr: &Expr, op: &Unaryop, operand violations::DoubleNegation(operand.to_string()), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( unparse_expr(operand, checker.stylist), expr.location, diff --git a/src/rules/flake8_simplify/rules/key_in_dict.rs b/src/rules/flake8_simplify/rules/key_in_dict.rs index d8d21d438e..4848aab9ea 100644 --- a/src/rules/flake8_simplify/rules/key_in_dict.rs +++ b/src/rules/flake8_simplify/rules/key_in_dict.rs @@ -38,7 +38,7 @@ fn key_in_dict(checker: &mut Checker, left: &Expr, right: &Expr, range: Range) { violations::KeyInDict(left_content.to_string(), value_content.to_string()), range, ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( value_content.to_string(), right.location, diff --git a/src/rules/flake8_simplify/rules/yoda_conditions.rs b/src/rules/flake8_simplify/rules/yoda_conditions.rs index 72763ace36..bab8795843 100644 --- a/src/rules/flake8_simplify/rules/yoda_conditions.rs +++ b/src/rules/flake8_simplify/rules/yoda_conditions.rs @@ -57,7 +57,7 @@ pub fn yoda_conditions( }, Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( suggestion, left.location, diff --git a/src/rules/isort/rules/organize_imports.rs b/src/rules/isort/rules/organize_imports.rs index 23408c71a9..01423d36b5 100644 --- a/src/rules/isort/rules/organize_imports.rs +++ b/src/rules/isort/rules/organize_imports.rs @@ -102,7 +102,7 @@ pub fn organize_imports( } else { let mut diagnostic = Diagnostic::new(violations::UnsortedImports, range); if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.code()) + && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( indent(&expected, indentation), diff --git a/src/rules/pandas_vet/mod.rs b/src/rules/pandas_vet/mod.rs index eb09d68acc..df663e8453 100644 --- a/src/rules/pandas_vet/mod.rs +++ b/src/rules/pandas_vet/mod.rs @@ -41,7 +41,7 @@ mod tests { )?; let actual = diagnostics .iter() - .map(|diagnostic| diagnostic.kind.code().clone()) + .map(|diagnostic| diagnostic.kind.rule().clone()) .collect::>(); assert_eq!(actual, expected); Ok(()) diff --git a/src/rules/pycodestyle/rules.rs b/src/rules/pycodestyle/rules.rs index 60d39e4646..bba5f51892 100644 --- a/src/rules/pycodestyle/rules.rs +++ b/src/rules/pycodestyle/rules.rs @@ -151,7 +151,7 @@ pub fn literal_comparisons( violations::NoneComparison(op.into()), Range::from_located(comparator), ); - if checker.patch(diagnostic.kind.code()) && !helpers::is_constant_non_singleton(next) { + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) { bad_ops.insert(0, Cmpop::Is); } diagnostics.push(diagnostic); @@ -161,7 +161,7 @@ pub fn literal_comparisons( violations::NoneComparison(op.into()), Range::from_located(comparator), ); - if checker.patch(diagnostic.kind.code()) && !helpers::is_constant_non_singleton(next) { + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) { bad_ops.insert(0, Cmpop::IsNot); } diagnostics.push(diagnostic); @@ -179,7 +179,7 @@ pub fn literal_comparisons( violations::TrueFalseComparison(value, op.into()), Range::from_located(comparator), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) { bad_ops.insert(0, Cmpop::Is); @@ -191,7 +191,7 @@ pub fn literal_comparisons( violations::TrueFalseComparison(value, op.into()), Range::from_located(comparator), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(next) { bad_ops.insert(0, Cmpop::IsNot); @@ -217,7 +217,7 @@ pub fn literal_comparisons( violations::NoneComparison(op.into()), Range::from_located(next), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(comparator) { bad_ops.insert(idx, Cmpop::Is); @@ -229,7 +229,7 @@ pub fn literal_comparisons( violations::NoneComparison(op.into()), Range::from_located(next), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(comparator) { bad_ops.insert(idx, Cmpop::IsNot); @@ -249,7 +249,7 @@ pub fn literal_comparisons( violations::TrueFalseComparison(value, op.into()), Range::from_located(next), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(comparator) { bad_ops.insert(idx, Cmpop::Is); @@ -261,7 +261,7 @@ pub fn literal_comparisons( violations::TrueFalseComparison(value, op.into()), Range::from_located(next), ); - if checker.patch(diagnostic.kind.code()) + if checker.patch(diagnostic.kind.rule()) && !helpers::is_constant_non_singleton(comparator) { bad_ops.insert(idx, Cmpop::IsNot); @@ -323,7 +323,7 @@ pub fn not_tests( violations::NotInTest, Range::from_located(operand), ); - if checker.patch(diagnostic.kind.code()) && should_fix { + if checker.patch(diagnostic.kind.rule()) && should_fix { diagnostic.amend(Fix::replacement( compare(left, &[Cmpop::NotIn], comparators, checker.stylist), expr.location, @@ -339,7 +339,7 @@ pub fn not_tests( violations::NotIsTest, Range::from_located(operand), ); - if checker.patch(diagnostic.kind.code()) && should_fix { + if checker.patch(diagnostic.kind.rule()) && should_fix { diagnostic.amend(Fix::replacement( compare(left, &[Cmpop::IsNot], comparators, checker.stylist), expr.location, @@ -455,7 +455,7 @@ pub fn do_not_assign_lambda(checker: &mut Checker, target: &Expr, value: &Expr, violations::DoNotAssignLambda(id.to_string()), Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if !match_leading_content(stmt, checker.locator) && !match_trailing_content(stmt, checker.locator) { diff --git a/src/rules/pydocstyle/rules.rs b/src/rules/pydocstyle/rules.rs index 040305efca..85c170f73f 100644 --- a/src/rules/pydocstyle/rules.rs +++ b/src/rules/pydocstyle/rules.rs @@ -180,7 +180,7 @@ pub fn blank_before_after_function(checker: &mut Checker, docstring: &Docstring) violations::NoBlankLineBeforeFunction(blank_lines_before), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete the blank line before the docstring. diagnostic.amend(Fix::deletion( Location::new(docstring.expr.location.row() - blank_lines_before, 0), @@ -221,7 +221,7 @@ pub fn blank_before_after_function(checker: &mut Checker, docstring: &Docstring) violations::NoBlankLineAfterFunction(blank_lines_after), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete the blank line after the docstring. diagnostic.amend(Fix::deletion( Location::new(docstring.expr.end_location.unwrap().row() + 1, 0), @@ -262,7 +262,7 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { violations::NoBlankLineBeforeClass(blank_lines_before), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete the blank line before the class. diagnostic.amend(Fix::deletion( Location::new(docstring.expr.location.row() - blank_lines_before, 0), @@ -278,7 +278,7 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { violations::OneBlankLineBeforeClass(blank_lines_before), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Insert one blank line before the class. diagnostic.amend(Fix::replacement( "\n".to_string(), @@ -315,7 +315,7 @@ pub fn blank_before_after_class(checker: &mut Checker, docstring: &Docstring) { violations::OneBlankLineAfterClass(blank_lines_after), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Insert a blank line before the class (replacing any existing lines). diagnostic.amend(Fix::replacement( "\n".to_string(), @@ -350,7 +350,7 @@ pub fn blank_after_summary(checker: &mut Checker, docstring: &Docstring) { violations::BlankLineAfterSummary(blanks_count), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if blanks_count > 1 { // Find the "summary" line (defined as the first non-blank line). let mut summary_line = 0; @@ -422,7 +422,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { Location::new(docstring.expr.location.row() + i, 0), ), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( whitespace::clean(docstring.indentation), Location::new(docstring.expr.location.row() + i, 0), @@ -472,7 +472,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { Location::new(docstring.expr.location.row() + i, 0), ), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( whitespace::clean(docstring.indentation), Location::new(docstring.expr.location.row() + i, 0), @@ -496,7 +496,7 @@ pub fn indent(checker: &mut Checker, docstring: &Docstring) { Location::new(docstring.expr.location.row() + i, 0), ), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( whitespace::clean(docstring.indentation), Location::new(docstring.expr.location.row() + i, 0), @@ -526,7 +526,7 @@ pub fn newline_after_last_paragraph(checker: &mut Checker, docstring: &Docstring violations::NewLineAfterLastParagraph, Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Insert a newline just before the end-quote(s). let num_trailing_quotes = "'''".len(); let num_trailing_spaces = last_line @@ -578,7 +578,7 @@ pub fn no_surrounding_whitespace(checker: &mut Checker, docstring: &Docstring) { violations::NoSurroundingWhitespace, Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(pattern) = leading_quote(contents) { if let Some(quote) = pattern.chars().last() { // If removing whitespace would lead to an invalid string of quote @@ -988,7 +988,7 @@ fn blanks_and_section_underline( violations::DashedUnderlineAfterSection(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Add a dashed line (of the appropriate length) under the section header. let content = format!( "{}{}\n", @@ -1026,7 +1026,7 @@ fn blanks_and_section_underline( violations::SectionUnderlineAfterName(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete any blank lines between the header and the underline. diagnostic.amend(Fix::deletion( Location::new( @@ -1060,7 +1060,7 @@ fn blanks_and_section_underline( ), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Replace the existing underline with a line of the appropriate length. let content = format!( "{}{}\n", @@ -1097,7 +1097,7 @@ fn blanks_and_section_underline( violations::SectionUnderlineNotOverIndented(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Replace the existing indentation with whitespace of the appropriate length. diagnostic.amend(Fix::replacement( whitespace::clean(docstring.indentation), @@ -1146,7 +1146,7 @@ fn blanks_and_section_underline( ), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete any blank lines between the header and content. diagnostic.amend(Fix::deletion( Location::new( @@ -1184,7 +1184,7 @@ fn blanks_and_section_underline( violations::DashedUnderlineAfterSection(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Add a dashed line (of the appropriate length) under the section header. let content = format!( "{}{}\n", @@ -1209,7 +1209,7 @@ fn blanks_and_section_underline( ), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete any blank lines between the header and content. diagnostic.amend(Fix::deletion( Location::new( @@ -1248,7 +1248,7 @@ fn common_section( violations::CapitalizeSectionName(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Replace the section title with the capitalized variant. This requires // locating the start and end of the section name. if let Some(index) = context.line.find(context.section_name) { @@ -1280,7 +1280,7 @@ fn common_section( violations::SectionNotOverIndented(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Replace the existing indentation with whitespace of the appropriate length. diagnostic.amend(Fix::replacement( whitespace::clean(docstring.indentation), @@ -1306,7 +1306,7 @@ fn common_section( violations::BlankLineAfterLastSection(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Add a newline after the section. diagnostic.amend(Fix::insertion( "\n".to_string(), @@ -1327,7 +1327,7 @@ fn common_section( violations::BlankLineAfterSection(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Add a newline after the section. diagnostic.amend(Fix::insertion( "\n".to_string(), @@ -1351,7 +1351,7 @@ fn common_section( violations::BlankLineBeforeSection(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Add a blank line before the section. diagnostic.amend(Fix::insertion( "\n".to_string(), @@ -1540,7 +1540,7 @@ fn numpy_section(checker: &mut Checker, docstring: &Docstring, context: &Section violations::NewLineAfterSectionName(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Delete the suffix. This requires locating the end of the section name. if let Some(index) = context.line.find(context.section_name) { // Map from bytes to characters. @@ -1586,7 +1586,7 @@ fn google_section(checker: &mut Checker, docstring: &Docstring, context: &Sectio violations::SectionNameEndsInColon(context.section_name.to_string()), Range::from_located(docstring.expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Replace the suffix. This requires locating the end of the section name. if let Some(index) = context.line.find(context.section_name) { // Map from bytes to characters. diff --git a/src/rules/pyflakes/mod.rs b/src/rules/pyflakes/mod.rs index f9da6af6fe..1c9f56c298 100644 --- a/src/rules/pyflakes/mod.rs +++ b/src/rules/pyflakes/mod.rs @@ -233,7 +233,7 @@ mod tests { diagnostics.sort_by_key(|diagnostic| diagnostic.location); let actual = diagnostics .iter() - .map(|diagnostic| diagnostic.kind.code().clone()) + .map(|diagnostic| diagnostic.kind.rule().clone()) .collect::>(); assert_eq!(actual, expected); Ok(()) diff --git a/src/rules/pyflakes/rules/invalid_literal_comparisons.rs b/src/rules/pyflakes/rules/invalid_literal_comparisons.rs index d77be3f153..ba0650b9ee 100644 --- a/src/rules/pyflakes/rules/invalid_literal_comparisons.rs +++ b/src/rules/pyflakes/rules/invalid_literal_comparisons.rs @@ -26,7 +26,7 @@ pub fn invalid_literal_comparison( || helpers::is_constant_non_singleton(right)) { let mut diagnostic = Diagnostic::new(violations::IsLiteral(op.into()), location); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(located_op) = &located.get(index) { assert_eq!(&located_op.node, op); if let Some(content) = match &located_op.node { diff --git a/src/rules/pyflakes/rules/raise_not_implemented.rs b/src/rules/pyflakes/rules/raise_not_implemented.rs index 405a425779..48f4cb9f0e 100644 --- a/src/rules/pyflakes/rules/raise_not_implemented.rs +++ b/src/rules/pyflakes/rules/raise_not_implemented.rs @@ -32,7 +32,7 @@ pub fn raise_not_implemented(checker: &mut Checker, expr: &Expr) { }; let mut diagnostic = Diagnostic::new(violations::RaiseNotImplemented, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( "NotImplementedError".to_string(), expr.location, diff --git a/src/rules/pyflakes/rules/strings.rs b/src/rules/pyflakes/rules/strings.rs index e7b6ad58ab..0e0e6a0b05 100644 --- a/src/rules/pyflakes/rules/strings.rs +++ b/src/rules/pyflakes/rules/strings.rs @@ -116,7 +116,7 @@ pub(crate) fn percent_format_extra_named_arguments( ), location, ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match remove_unused_format_arguments_from_dict(&missing, right, checker.locator) { Ok(fix) => { diagnostic.amend(fix); @@ -274,7 +274,7 @@ pub(crate) fn string_dot_format_extra_named_arguments( ), location, ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match remove_unused_keyword_arguments_from_format_call(&missing, location, checker.locator) { Ok(fix) => { diff --git a/src/rules/pylint/rules/misplaced_comparison_constant.rs b/src/rules/pylint/rules/misplaced_comparison_constant.rs new file mode 100644 index 0000000000..efbea5bea0 --- /dev/null +++ b/src/rules/pylint/rules/misplaced_comparison_constant.rs @@ -0,0 +1,56 @@ +use rustpython_ast::{Cmpop, Expr, ExprKind}; + +use crate::ast::types::Range; +use crate::checkers::ast::Checker; +use crate::fix::Fix; +use crate::registry::Diagnostic; +use crate::violations; + +/// PLC2201 +pub fn misplaced_comparison_constant( + checker: &mut Checker, + expr: &Expr, + left: &Expr, + ops: &[Cmpop], + comparators: &[Expr], +) { + let ([op], [right]) = (ops, comparators) else { + return; + }; + + if !matches!( + op, + Cmpop::Eq | Cmpop::NotEq | Cmpop::Lt | Cmpop::LtE | Cmpop::Gt | Cmpop::GtE, + ) { + return; + } + if !matches!(&left.node, &ExprKind::Constant { .. }) { + return; + } + if matches!(&right.node, &ExprKind::Constant { .. }) { + return; + } + + let reversed_op = match op { + Cmpop::Eq => "==", + Cmpop::NotEq => "!=", + Cmpop::Lt => ">", + Cmpop::LtE => ">=", + Cmpop::Gt => "<", + Cmpop::GtE => "<=", + _ => unreachable!("Expected comparison operator"), + }; + let suggestion = format!("{right} {reversed_op} {left}"); + let mut diagnostic = Diagnostic::new( + violations::MisplacedComparisonConstant(suggestion.clone()), + Range::from_located(expr), + ); + if checker.patch(diagnostic.kind.rule()) { + diagnostic.amend(Fix::replacement( + suggestion, + expr.location, + expr.end_location.unwrap(), + )); + } + checker.diagnostics.push(diagnostic); +} diff --git a/src/rules/pylint/rules/use_sys_exit.rs b/src/rules/pylint/rules/use_sys_exit.rs index ccb5731a07..b935f7411d 100644 --- a/src/rules/pylint/rules/use_sys_exit.rs +++ b/src/rules/pylint/rules/use_sys_exit.rs @@ -95,7 +95,7 @@ pub fn use_sys_exit(checker: &mut Checker, func: &Expr) { violations::UseSysExit(name.to_string()), Range::from_located(func), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(content) = get_member_import_name_alias(checker, "sys", "exit") { diagnostic.amend(Fix::replacement( content, diff --git a/src/rules/pylint/rules/useless_import_alias.rs b/src/rules/pylint/rules/useless_import_alias.rs index 864da7149f..54a9435275 100644 --- a/src/rules/pylint/rules/useless_import_alias.rs +++ b/src/rules/pylint/rules/useless_import_alias.rs @@ -20,7 +20,7 @@ pub fn useless_import_alias(checker: &mut Checker, alias: &Alias) { let mut diagnostic = Diagnostic::new(violations::UselessImportAlias, Range::from_located(alias)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( asname.to_string(), alias.location, diff --git a/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs b/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs index 034148cbb6..c61afccf3a 100644 --- a/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs +++ b/src/rules/pyupgrade/rules/convert_named_tuple_functional_to_class.rs @@ -157,7 +157,7 @@ pub fn convert_named_tuple_functional_to_class( violations::ConvertNamedTupleFunctionalToClass(typename.to_string()), Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match match_defaults(keywords) .and_then(|defaults| create_properties_from_args(args, defaults)) { diff --git a/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs b/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs index 6aa1458001..2d0b0889c8 100644 --- a/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs +++ b/src/rules/pyupgrade/rules/convert_typed_dict_functional_to_class.rs @@ -200,7 +200,7 @@ pub fn convert_typed_dict_functional_to_class( violations::ConvertTypedDictFunctionalToClass(class_name.to_string()), Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match match_properties_and_total(args, keywords) { Ok((body, total_keyword)) => { diagnostic.amend(convert_to_class( diff --git a/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs b/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs index 42e35336fc..c58725152a 100644 --- a/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs +++ b/src/rules/pyupgrade/rules/deprecated_unittest_alias.rs @@ -46,7 +46,7 @@ pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) { violations::DeprecatedUnittestAlias(attr.to_string(), target.to_string()), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( format!("self.{target}"), expr.location, diff --git a/src/rules/pyupgrade/rules/format_literals.rs b/src/rules/pyupgrade/rules/format_literals.rs index 95c0779222..1f5c402ae6 100644 --- a/src/rules/pyupgrade/rules/format_literals.rs +++ b/src/rules/pyupgrade/rules/format_literals.rs @@ -98,7 +98,7 @@ pub(crate) fn format_literals(checker: &mut Checker, summary: &FormatSummary, ex } let mut diagnostic = Diagnostic::new(violations::FormatLiterals, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { // Currently, the only issue we know of is in LibCST: // https://github.com/Instagram/LibCST/issues/846 if let Ok(contents) = generate_call( diff --git a/src/rules/pyupgrade/rules/os_error_alias.rs b/src/rules/pyupgrade/rules/os_error_alias.rs index 42bf035d8e..e3e7b0778d 100644 --- a/src/rules/pyupgrade/rules/os_error_alias.rs +++ b/src/rules/pyupgrade/rules/os_error_alias.rs @@ -148,7 +148,7 @@ fn handle_making_changes( violations::OSErrorAlias(compose_call_path(target)), Range::from_located(target), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( final_str, target.location, diff --git a/src/rules/pyupgrade/rules/replace_stdout_stderr.rs b/src/rules/pyupgrade/rules/replace_stdout_stderr.rs index 9e8444ed21..0337aaa776 100644 --- a/src/rules/pyupgrade/rules/replace_stdout_stderr.rs +++ b/src/rules/pyupgrade/rules/replace_stdout_stderr.rs @@ -108,7 +108,7 @@ pub fn replace_stdout_stderr(checker: &mut Checker, expr: &Expr, kwargs: &[Keywo let mut diagnostic = Diagnostic::new(violations::ReplaceStdoutStderr, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(fix) = generate_fix(checker.locator, stdout, stderr) { diagnostic.amend(fix); }; diff --git a/src/rules/pyupgrade/rules/replace_universal_newlines.rs b/src/rules/pyupgrade/rules/replace_universal_newlines.rs index 4f99597a6a..8b4579dd36 100644 --- a/src/rules/pyupgrade/rules/replace_universal_newlines.rs +++ b/src/rules/pyupgrade/rules/replace_universal_newlines.rs @@ -21,7 +21,7 @@ pub fn replace_universal_newlines(checker: &mut Checker, expr: &Expr, kwargs: &[ ), ); let mut diagnostic = Diagnostic::new(violations::ReplaceUniversalNewlines, range); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( "text".to_string(), range.location, diff --git a/src/rules/pyupgrade/rules/rewrite_c_element_tree.rs b/src/rules/pyupgrade/rules/rewrite_c_element_tree.rs index f5fd33aa3a..7d4ac401a2 100644 --- a/src/rules/pyupgrade/rules/rewrite_c_element_tree.rs +++ b/src/rules/pyupgrade/rules/rewrite_c_element_tree.rs @@ -9,7 +9,7 @@ use crate::violations; fn add_check_for_node(checker: &mut Checker, node: &Located) { let mut diagnostic = Diagnostic::new(violations::RewriteCElementTree, Range::from_located(node)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let contents = checker .locator .slice_source_code_range(&Range::from_located(node)); diff --git a/src/rules/pyupgrade/rules/rewrite_unicode_literal.rs b/src/rules/pyupgrade/rules/rewrite_unicode_literal.rs index 893dac2fa0..d31fd4a64c 100644 --- a/src/rules/pyupgrade/rules/rewrite_unicode_literal.rs +++ b/src/rules/pyupgrade/rules/rewrite_unicode_literal.rs @@ -12,7 +12,7 @@ pub fn rewrite_unicode_literal(checker: &mut Checker, expr: &Expr, kind: Option< if const_kind.to_lowercase() == "u" { let mut diagnostic = Diagnostic::new(violations::RewriteUnicodeLiteral, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::deletion( expr.location, Location::new(expr.location.row(), expr.location.column() + 1), diff --git a/src/rules/pyupgrade/rules/rewrite_yield_from.rs b/src/rules/pyupgrade/rules/rewrite_yield_from.rs index 5513eeb9fc..19820a40aa 100644 --- a/src/rules/pyupgrade/rules/rewrite_yield_from.rs +++ b/src/rules/pyupgrade/rules/rewrite_yield_from.rs @@ -159,7 +159,7 @@ pub fn rewrite_yield_from(checker: &mut Checker, stmt: &Stmt) { let mut diagnostic = Diagnostic::new(violations::RewriteYieldFrom, Range::from_located(item.stmt)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let contents = checker .locator .slice_source_code_range(&Range::from_located(item.iter)); diff --git a/src/rules/pyupgrade/rules/super_call_with_parameters.rs b/src/rules/pyupgrade/rules/super_call_with_parameters.rs index bbab60bb05..37296c29cf 100644 --- a/src/rules/pyupgrade/rules/super_call_with_parameters.rs +++ b/src/rules/pyupgrade/rules/super_call_with_parameters.rs @@ -20,7 +20,7 @@ pub fn super_call_with_parameters(checker: &mut Checker, expr: &Expr, func: &Exp let Some(mut diagnostic) = super::super_args(scope, &parents, expr, func, args) else { return; }; - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(fix) = fixes::remove_super_arguments(checker.locator, expr) { diagnostic.amend(fix); } diff --git a/src/rules/pyupgrade/rules/type_of_primitive.rs b/src/rules/pyupgrade/rules/type_of_primitive.rs index 07a05e88a6..9bec82201f 100644 --- a/src/rules/pyupgrade/rules/type_of_primitive.rs +++ b/src/rules/pyupgrade/rules/type_of_primitive.rs @@ -28,7 +28,7 @@ pub fn type_of_primitive(checker: &mut Checker, expr: &Expr, func: &Expr, args: violations::TypeOfPrimitive(primitive), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( primitive.builtin(), expr.location, diff --git a/src/rules/pyupgrade/rules/typing_text_str_alias.rs b/src/rules/pyupgrade/rules/typing_text_str_alias.rs index b4804d4628..10cc11aa63 100644 --- a/src/rules/pyupgrade/rules/typing_text_str_alias.rs +++ b/src/rules/pyupgrade/rules/typing_text_str_alias.rs @@ -13,7 +13,7 @@ pub fn typing_text_str_alias(checker: &mut Checker, expr: &Expr) { }) { let mut diagnostic = Diagnostic::new(violations::TypingTextStrAlias, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( "str".to_string(), expr.location, diff --git a/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs b/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs index a89230af09..c7376ec5f7 100644 --- a/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs +++ b/src/rules/pyupgrade/rules/unnecessary_builtin_import.rs @@ -79,7 +79,7 @@ pub fn unnecessary_builtin_import( Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let deleted: Vec<&Stmt> = checker .deletions .iter() diff --git a/src/rules/pyupgrade/rules/unnecessary_future_import.rs b/src/rules/pyupgrade/rules/unnecessary_future_import.rs index 87544a3f04..68d9fd4bb3 100644 --- a/src/rules/pyupgrade/rules/unnecessary_future_import.rs +++ b/src/rules/pyupgrade/rules/unnecessary_future_import.rs @@ -64,7 +64,7 @@ pub fn unnecessary_future_import(checker: &mut Checker, stmt: &Stmt, names: &[Lo Range::from_located(stmt), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let deleted: Vec<&Stmt> = checker .deletions .iter() diff --git a/src/rules/pyupgrade/rules/use_pep585_annotation.rs b/src/rules/pyupgrade/rules/use_pep585_annotation.rs index afb2856519..57783298fe 100644 --- a/src/rules/pyupgrade/rules/use_pep585_annotation.rs +++ b/src/rules/pyupgrade/rules/use_pep585_annotation.rs @@ -16,7 +16,7 @@ pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr) { violations::UsePEP585Annotation(binding.to_string()), Range::from_located(expr), ); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( binding.to_lowercase(), expr.location, diff --git a/src/rules/pyupgrade/rules/use_pep604_annotation.rs b/src/rules/pyupgrade/rules/use_pep604_annotation.rs index a572e8f55a..689e8ab26d 100644 --- a/src/rules/pyupgrade/rules/use_pep604_annotation.rs +++ b/src/rules/pyupgrade/rules/use_pep604_annotation.rs @@ -82,7 +82,7 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s TypingMember::Optional => { let mut diagnostic = Diagnostic::new(violations::UsePEP604Annotation, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let mut generator: Generator = checker.stylist.into(); generator.unparse_expr(&optional(slice), 0); diagnostic.amend(Fix::replacement( @@ -96,7 +96,7 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s TypingMember::Union => { let mut diagnostic = Diagnostic::new(violations::UsePEP604Annotation, Range::from_located(expr)); - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { match &slice.node { ExprKind::Slice { .. } => { // Invalid type annotation. diff --git a/src/rules/pyupgrade/rules/useless_metaclass_type.rs b/src/rules/pyupgrade/rules/useless_metaclass_type.rs index c1cce6373b..be04633881 100644 --- a/src/rules/pyupgrade/rules/useless_metaclass_type.rs +++ b/src/rules/pyupgrade/rules/useless_metaclass_type.rs @@ -32,7 +32,7 @@ pub fn useless_metaclass_type(checker: &mut Checker, stmt: &Stmt, value: &Expr, rule(targets, value, Range::from_located(stmt)) else { return; }; - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { let deleted: Vec<&Stmt> = checker .deletions .iter() diff --git a/src/rules/pyupgrade/rules/useless_object_inheritance.rs b/src/rules/pyupgrade/rules/useless_object_inheritance.rs index bd5f589b4c..00cdee46d3 100644 --- a/src/rules/pyupgrade/rules/useless_object_inheritance.rs +++ b/src/rules/pyupgrade/rules/useless_object_inheritance.rs @@ -46,7 +46,7 @@ pub fn useless_object_inheritance( let Some(mut diagnostic) = rule(name, bases, checker.current_scope(), &checker.bindings) else { return; }; - if checker.patch(diagnostic.kind.code()) { + if checker.patch(diagnostic.kind.rule()) { if let Some(fix) = fixes::remove_class_def_base( checker.locator, stmt.location, diff --git a/src/rules/ruff/rules.rs b/src/rules/ruff/rules.rs index 6078f75a9b..4f9528cb66 100644 --- a/src/rules/ruff/rules.rs +++ b/src/rules/ruff/rules.rs @@ -1650,9 +1650,9 @@ pub fn ambiguous_unicode_character( }, Range::new(location, end_location), ); - if settings.rules.enabled(diagnostic.kind.code()) { + if settings.rules.enabled(diagnostic.kind.rule()) { if matches!(autofix, flags::Autofix::Enabled) - && settings.rules.should_fix(diagnostic.kind.code()) + && settings.rules.should_fix(diagnostic.kind.rule()) { diagnostic.amend(Fix::replacement( representant.to_string(),