diff --git a/crates/ruff/resources/test/disallowed_rule_names.txt b/crates/ruff/resources/test/disallowed_rule_names.txt index 20cbe78efb..411b228939 100644 --- a/crates/ruff/resources/test/disallowed_rule_names.txt +++ b/crates/ruff/resources/test/disallowed_rule_names.txt @@ -5,3 +5,4 @@ uses-* rewrite-* prefer-* consider-* +use-* diff --git a/crates/ruff/resources/test/fixtures/pylint/use_prior_to_global_declaration.py b/crates/ruff/resources/test/fixtures/pylint/load_before_global_declaration.py similarity index 100% rename from crates/ruff/resources/test/fixtures/pylint/use_prior_to_global_declaration.py rename to crates/ruff/resources/test/fixtures/pylint/load_before_global_declaration.py diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index ae33ecd8c1..89f17c128d 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -1718,8 +1718,8 @@ where flake8_pytest_style::rules::assert_in_exception_handler(handlers), ); } - if self.settings.rules.enabled(Rule::UseContextlibSuppress) { - flake8_simplify::rules::use_contextlib_suppress( + if self.settings.rules.enabled(Rule::SuppressibleException) { + flake8_simplify::rules::suppressible_exception( self, stmt, body, handlers, orelse, finalbody, ); } @@ -2267,9 +2267,9 @@ where if self .settings .rules - .enabled(Rule::UsePriorToGlobalDeclaration) + .enabled(Rule::LoadBeforeGlobalDeclaration) { - pylint::rules::use_prior_to_global_declaration(self, id, expr); + pylint::rules::load_before_global_declaration(self, id, expr); } } ExprKind::Attribute { attr, value, .. } => { diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index 6e33011c65..56c175b210 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -168,7 +168,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option { (Pylint, "E0101") => Rule::ReturnInInit, (Pylint, "E0116") => Rule::ContinueInFinally, (Pylint, "E0117") => Rule::NonlocalWithoutBinding, - (Pylint, "E0118") => Rule::UsePriorToGlobalDeclaration, + (Pylint, "E0118") => Rule::LoadBeforeGlobalDeclaration, (Pylint, "E0604") => Rule::InvalidAllObject, (Pylint, "E0605") => Rule::InvalidAllFormat, (Pylint, "E1142") => Rule::AwaitOutsideAsync, @@ -327,7 +327,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option { (Flake8Simplify, "101") => Rule::DuplicateIsinstanceCall, (Flake8Simplify, "102") => Rule::CollapsibleIf, (Flake8Simplify, "103") => Rule::NeedlessBool, - (Flake8Simplify, "105") => Rule::UseContextlibSuppress, + (Flake8Simplify, "105") => Rule::SuppressibleException, (Flake8Simplify, "107") => Rule::ReturnInTryExceptFinally, (Flake8Simplify, "108") => Rule::IfElseBlockInsteadOfIfExp, (Flake8Simplify, "109") => Rule::CompareWithTuple, diff --git a/crates/ruff/src/registry.rs b/crates/ruff/src/registry.rs index 3beec4f22d..dc193d57ec 100644 --- a/crates/ruff/src/registry.rs +++ b/crates/ruff/src/registry.rs @@ -167,7 +167,7 @@ ruff_macros::register_rules!( rules::pylint::rules::UselessImportAlias, rules::pylint::rules::UnnecessaryDirectLambdaCall, rules::pylint::rules::NonlocalWithoutBinding, - rules::pylint::rules::UsePriorToGlobalDeclaration, + rules::pylint::rules::LoadBeforeGlobalDeclaration, rules::pylint::rules::AwaitOutsideAsync, rules::pylint::rules::PropertyWithParameters, rules::pylint::rules::ReturnInInit, @@ -299,7 +299,7 @@ ruff_macros::register_rules!( rules::flake8_simplify::rules::DuplicateIsinstanceCall, rules::flake8_simplify::rules::CollapsibleIf, rules::flake8_simplify::rules::NeedlessBool, - rules::flake8_simplify::rules::UseContextlibSuppress, + rules::flake8_simplify::rules::SuppressibleException, rules::flake8_simplify::rules::ReturnInTryExceptFinally, rules::flake8_simplify::rules::IfElseBlockInsteadOfIfExp, rules::flake8_simplify::rules::CompareWithTuple, diff --git a/crates/ruff/src/rules/flake8_simplify/mod.rs b/crates/ruff/src/rules/flake8_simplify/mod.rs index c66fb03e53..4c915be63c 100644 --- a/crates/ruff/src/rules/flake8_simplify/mod.rs +++ b/crates/ruff/src/rules/flake8_simplify/mod.rs @@ -16,7 +16,7 @@ mod tests { #[test_case(Rule::DuplicateIsinstanceCall, Path::new("SIM101.py"); "SIM101")] #[test_case(Rule::CollapsibleIf, Path::new("SIM102.py"); "SIM102")] #[test_case(Rule::NeedlessBool, Path::new("SIM103.py"); "SIM103")] - #[test_case(Rule::UseContextlibSuppress, Path::new("SIM105.py"); "SIM105")] + #[test_case(Rule::SuppressibleException, Path::new("SIM105.py"); "SIM105")] #[test_case(Rule::ReturnInTryExceptFinally, Path::new("SIM107.py"); "SIM107")] #[test_case(Rule::IfElseBlockInsteadOfIfExp, Path::new("SIM108.py"); "SIM108")] #[test_case(Rule::CompareWithTuple, Path::new("SIM109.py"); "SIM109")] diff --git a/crates/ruff/src/rules/flake8_simplify/rules/mod.rs b/crates/ruff/src/rules/flake8_simplify/rules/mod.rs index 2f6cff9b9a..750297f544 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/mod.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/mod.rs @@ -24,7 +24,7 @@ pub use open_file_with_context_handler::{ }; pub use reimplemented_builtin::{convert_for_loop_to_any_all, ReimplementedBuiltin}; pub use return_in_try_except_finally::{return_in_try_except_finally, ReturnInTryExceptFinally}; -pub use use_contextlib_suppress::{use_contextlib_suppress, UseContextlibSuppress}; +pub use suppressible_exception::{suppressible_exception, SuppressibleException}; pub use yoda_conditions::{yoda_conditions, YodaConditions}; mod ast_bool_op; @@ -39,5 +39,5 @@ mod key_in_dict; mod open_file_with_context_handler; mod reimplemented_builtin; mod return_in_try_except_finally; -mod use_contextlib_suppress; +mod suppressible_exception; mod yoda_conditions; diff --git a/crates/ruff/src/rules/flake8_simplify/rules/use_contextlib_suppress.rs b/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs similarity index 89% rename from crates/ruff/src/rules/flake8_simplify/rules/use_contextlib_suppress.rs rename to crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs index 866c3aaa3a..c48e38d708 100644 --- a/crates/ruff/src/rules/flake8_simplify/rules/use_contextlib_suppress.rs +++ b/crates/ruff/src/rules/flake8_simplify/rules/suppressible_exception.rs @@ -9,19 +9,19 @@ use ruff_python_ast::types::Range; use crate::checkers::ast::Checker; #[violation] -pub struct UseContextlibSuppress { +pub struct SuppressibleException { pub exception: String, } -impl Violation for UseContextlibSuppress { +impl Violation for SuppressibleException { #[derive_message_formats] fn message(&self) -> String { - let UseContextlibSuppress { exception } = self; + let SuppressibleException { exception } = self; format!("Use `contextlib.suppress({exception})` instead of try-except-pass") } } /// SIM105 -pub fn use_contextlib_suppress( +pub fn suppressible_exception( checker: &mut Checker, stmt: &Stmt, body: &[Stmt], @@ -63,7 +63,7 @@ pub fn use_contextlib_suppress( handler_names.join(", ") }; checker.diagnostics.push(Diagnostic::new( - UseContextlibSuppress { exception }, + SuppressibleException { exception }, Range::from(stmt), )); } diff --git a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM105_SIM105.py.snap b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM105_SIM105.py.snap index 36448613b2..9d4cbdaea3 100644 --- a/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM105_SIM105.py.snap +++ b/crates/ruff/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM105_SIM105.py.snap @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/flake8_simplify/mod.rs expression: diagnostics --- - kind: - name: UseContextlibSuppress + name: SuppressibleException body: "Use `contextlib.suppress(ValueError)` instead of try-except-pass" suggestion: ~ fixable: false @@ -16,7 +16,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UseContextlibSuppress + name: SuppressibleException body: "Use `contextlib.suppress(ValueError, OSError)` instead of try-except-pass" suggestion: ~ fixable: false @@ -29,7 +29,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UseContextlibSuppress + name: SuppressibleException body: "Use `contextlib.suppress(Exception)` instead of try-except-pass" suggestion: ~ fixable: false @@ -42,7 +42,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UseContextlibSuppress + name: SuppressibleException body: "Use `contextlib.suppress(a.Error, b.Error)` instead of try-except-pass" suggestion: ~ fixable: false diff --git a/crates/ruff/src/rules/pylint/mod.rs b/crates/ruff/src/rules/pylint/mod.rs index ddc9bbb496..556a80d61e 100644 --- a/crates/ruff/src/rules/pylint/mod.rs +++ b/crates/ruff/src/rules/pylint/mod.rs @@ -60,7 +60,7 @@ mod tests { #[test_case(Rule::TooManyReturnStatements, Path::new("too_many_return_statements.py"); "PLR0911")] #[test_case(Rule::TooManyStatements, Path::new("too_many_statements.py"); "PLR0915")] #[test_case(Rule::UnnecessaryDirectLambdaCall, Path::new("unnecessary_direct_lambda_call.py"); "PLC3002")] - #[test_case(Rule::UsePriorToGlobalDeclaration, Path::new("use_prior_to_global_declaration.py"); "PLE0118")] + #[test_case(Rule::LoadBeforeGlobalDeclaration, Path::new("load_before_global_declaration.py"); "PLE0118")] #[test_case(Rule::UselessElseOnLoop, Path::new("useless_else_on_loop.py"); "PLW0120")] #[test_case(Rule::UselessImportAlias, Path::new("import_aliasing.py"); "PLC0414")] #[test_case(Rule::UselessReturn, Path::new("useless_return.py"); "PLR1711")] diff --git a/crates/ruff/src/rules/pylint/rules/use_prior_to_global_declaration.rs b/crates/ruff/src/rules/pylint/rules/load_before_global_declaration.rs similarity index 77% rename from crates/ruff/src/rules/pylint/rules/use_prior_to_global_declaration.rs rename to crates/ruff/src/rules/pylint/rules/load_before_global_declaration.rs index b31e1e637d..100df18cfa 100644 --- a/crates/ruff/src/rules/pylint/rules/use_prior_to_global_declaration.rs +++ b/crates/ruff/src/rules/pylint/rules/load_before_global_declaration.rs @@ -8,20 +8,20 @@ use ruff_python_ast::types::Range; use crate::checkers::ast::Checker; #[violation] -pub struct UsePriorToGlobalDeclaration { +pub struct LoadBeforeGlobalDeclaration { pub name: String, pub line: usize, } -impl Violation for UsePriorToGlobalDeclaration { +impl Violation for LoadBeforeGlobalDeclaration { #[derive_message_formats] fn message(&self) -> String { - let UsePriorToGlobalDeclaration { name, line } = self; + let LoadBeforeGlobalDeclaration { name, line } = self; format!("Name `{name}` is used prior to global declaration on line {line}") } } /// PLE0118 -pub fn use_prior_to_global_declaration(checker: &mut Checker, name: &str, expr: &Expr) { +pub fn load_before_global_declaration(checker: &mut Checker, name: &str, expr: &Expr) { let globals = match &checker.ctx.scope().kind { ScopeKind::Class(class_def) => &class_def.globals, ScopeKind::Function(function_def) => &function_def.globals, @@ -30,7 +30,7 @@ pub fn use_prior_to_global_declaration(checker: &mut Checker, name: &str, expr: if let Some(stmt) = globals.get(name) { if expr.location < stmt.location { checker.diagnostics.push(Diagnostic::new( - UsePriorToGlobalDeclaration { + LoadBeforeGlobalDeclaration { name: name.to_string(), line: stmt.location.row(), }, diff --git a/crates/ruff/src/rules/pylint/rules/mod.rs b/crates/ruff/src/rules/pylint/rules/mod.rs index 06c8e6a2d2..b897495b73 100644 --- a/crates/ruff/src/rules/pylint/rules/mod.rs +++ b/crates/ruff/src/rules/pylint/rules/mod.rs @@ -18,6 +18,9 @@ pub use invalid_string_characters::{ invalid_string_characters, InvalidCharacterBackspace, InvalidCharacterEsc, InvalidCharacterNul, InvalidCharacterSub, InvalidCharacterZeroWidthSpace, }; +pub use load_before_global_declaration::{ + load_before_global_declaration, LoadBeforeGlobalDeclaration, +}; pub use logging::{logging_call, LoggingTooFewArgs, LoggingTooManyArgs}; pub use magic_value_comparison::{magic_value_comparison, MagicValueComparison}; pub use manual_import_from::{manual_from_import, ManualFromImport}; @@ -34,9 +37,6 @@ pub use too_many_statements::{too_many_statements, TooManyStatements}; pub use unnecessary_direct_lambda_call::{ unnecessary_direct_lambda_call, UnnecessaryDirectLambdaCall, }; -pub use use_prior_to_global_declaration::{ - use_prior_to_global_declaration, UsePriorToGlobalDeclaration, -}; pub use useless_else_on_loop::{useless_else_on_loop, UselessElseOnLoop}; pub use useless_import_alias::{useless_import_alias, UselessImportAlias}; pub use useless_return::{useless_return, UselessReturn}; @@ -59,6 +59,7 @@ mod invalid_all_object; mod invalid_envvar_default; mod invalid_envvar_value; mod invalid_string_characters; +mod load_before_global_declaration; mod logging; mod magic_value_comparison; mod manual_import_from; @@ -73,7 +74,6 @@ mod too_many_branches; mod too_many_return_statements; mod too_many_statements; mod unnecessary_direct_lambda_call; -mod use_prior_to_global_declaration; mod useless_else_on_loop; mod useless_import_alias; mod useless_return; diff --git a/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_use_prior_to_global_declaration.py.snap b/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_load_before_global_declaration.py.snap similarity index 85% rename from crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_use_prior_to_global_declaration.py.snap rename to crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_load_before_global_declaration.py.snap index 870e447382..068b1e22a8 100644 --- a/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_use_prior_to_global_declaration.py.snap +++ b/crates/ruff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0118_load_before_global_declaration.py.snap @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pylint/mod.rs expression: diagnostics --- - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 7" suggestion: ~ fixable: false @@ -16,7 +16,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 17" suggestion: ~ fixable: false @@ -29,7 +29,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 25" suggestion: ~ fixable: false @@ -42,7 +42,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 35" suggestion: ~ fixable: false @@ -55,7 +55,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 43" suggestion: ~ fixable: false @@ -68,7 +68,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 53" suggestion: ~ fixable: false @@ -81,7 +81,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 61" suggestion: ~ fixable: false @@ -94,7 +94,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 71" suggestion: ~ fixable: false @@ -107,7 +107,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 79" suggestion: ~ fixable: false @@ -120,7 +120,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 89" suggestion: ~ fixable: false @@ -133,7 +133,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 97" suggestion: ~ fixable: false @@ -146,7 +146,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 107" suggestion: ~ fixable: false @@ -159,7 +159,7 @@ expression: diagnostics fix: ~ parent: ~ - kind: - name: UsePriorToGlobalDeclaration + name: LoadBeforeGlobalDeclaration body: "Name `x` is used prior to global declaration on line 114" suggestion: ~ fixable: false