From 72dc5ddd78b3fbabd31b05f09b8b13fc8f4bb201 Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sun, 23 Jul 2023 00:59:53 +0100 Subject: [PATCH 1/4] Use helper function instead --- .../flake8_bandit/rules/logging_config_insecure_listen.rs | 8 ++------ .../rules/flake8_bandit/rules/snmp_insecure_version.rs | 8 ++------ .../rules/flake8_bugbear/rules/assert_raises_exception.rs | 5 ++--- .../rules/flake8_bugbear/rules/no_explicit_stacklevel.rs | 8 ++------ .../flake8_bugbear/rules/zip_without_explicit_strict.rs | 6 ++---- .../ruff/src/rules/flake8_pytest_style/rules/fixture.rs | 8 ++------ 6 files changed, 12 insertions(+), 31 deletions(-) diff --git a/crates/ruff/src/rules/flake8_bandit/rules/logging_config_insecure_listen.rs b/crates/ruff/src/rules/flake8_bandit/rules/logging_config_insecure_listen.rs index 58554c2608c36..b3f25bd2ba3d3 100644 --- a/crates/ruff/src/rules/flake8_bandit/rules/logging_config_insecure_listen.rs +++ b/crates/ruff/src/rules/flake8_bandit/rules/logging_config_insecure_listen.rs @@ -2,6 +2,7 @@ use rustpython_parser::ast::{Expr, Keyword, Ranged}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers::find_keyword; use crate::checkers::ast::Checker; @@ -28,12 +29,7 @@ pub(crate) fn logging_config_insecure_listen( matches!(call_path.as_slice(), ["logging", "config", "listen"]) }) { - if keywords.iter().any(|keyword| { - keyword - .arg - .as_ref() - .map_or(false, |arg| arg.as_str() == "verify") - }) { + if find_keyword(keywords, "verify").is_some() { return; } diff --git a/crates/ruff/src/rules/flake8_bandit/rules/snmp_insecure_version.rs b/crates/ruff/src/rules/flake8_bandit/rules/snmp_insecure_version.rs index f0bf18827b9c8..08a97668b44ad 100644 --- a/crates/ruff/src/rules/flake8_bandit/rules/snmp_insecure_version.rs +++ b/crates/ruff/src/rules/flake8_bandit/rules/snmp_insecure_version.rs @@ -3,6 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr, Keyword, Ranged}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers::find_keyword; use crate::checkers::ast::Checker; @@ -50,12 +51,7 @@ pub(crate) fn snmp_insecure_version(checker: &mut Checker, func: &Expr, keywords matches!(call_path.as_slice(), ["pysnmp", "hlapi", "CommunityData"]) }) { - if let Some(keyword) = keywords.iter().find(|keyword| { - keyword - .arg - .as_ref() - .map_or(false, |arg| arg.as_str() == "mpModel") - }) { + if let Some(keyword) = find_keyword(keywords, "mpModel") { if let Expr::Constant(ast::ExprConstant { value: Constant::Int(value), .. diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/assert_raises_exception.rs b/crates/ruff/src/rules/flake8_bugbear/rules/assert_raises_exception.rs index 00c44bf0eee98..d459718b379c0 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/assert_raises_exception.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/assert_raises_exception.rs @@ -4,6 +4,7 @@ use rustpython_parser::ast::{self, Expr, Ranged, WithItem}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers::find_keyword; use crate::checkers::ast::Checker; @@ -115,9 +116,7 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem]) .map_or(false, |call_path| { matches!(call_path.as_slice(), ["pytest", "raises"]) }) - && !keywords - .iter() - .any(|keyword| keyword.arg.as_ref().map_or(false, |arg| arg == "match")) + && find_keyword(keywords, "match").is_none() { AssertionKind::PytestRaises } else { diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs b/crates/ruff/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs index e3f2fcd6586cb..af04d6d63281c 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs @@ -2,6 +2,7 @@ use rustpython_parser::ast::{Expr, Keyword, Ranged}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers::find_keyword; use crate::checkers::ast::Checker; @@ -48,12 +49,7 @@ pub(crate) fn no_explicit_stacklevel(checker: &mut Checker, func: &Expr, keyword return; } - if keywords.iter().any(|keyword| { - keyword - .arg - .as_ref() - .map_or(false, |arg| arg.as_str() == "stacklevel") - }) { + if find_keyword(keywords, "stacklevel").is_some() { return; } diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs b/crates/ruff/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs index 4a4efd4e3c1e4..7629b37f3e0ac 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/zip_without_explicit_strict.rs @@ -2,7 +2,7 @@ use rustpython_parser::ast::{self, Expr, Keyword, Ranged}; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::helpers::is_const_none; +use ruff_python_ast::helpers::{find_keyword, is_const_none}; use ruff_python_semantic::SemanticModel; use crate::checkers::ast::Checker; @@ -51,9 +51,7 @@ pub(crate) fn zip_without_explicit_strict( if let Expr::Name(ast::ExprName { id, .. }) = func { if id == "zip" && checker.semantic().is_builtin("zip") - && !kwargs - .iter() - .any(|keyword| keyword.arg.as_ref().map_or(false, |name| name == "strict")) + && find_keyword(kwargs, "strict").is_none() && !args .iter() .any(|arg| is_infinite_iterator(arg, checker.semantic())) diff --git a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs index 27412760c3a13..60ce2d628bcd0 100644 --- a/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs +++ b/crates/ruff/src/rules/flake8_pytest_style/rules/fixture.rs @@ -8,7 +8,7 @@ use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::call_path::collect_call_path; -use ruff_python_ast::helpers::includes_arg_name; +use ruff_python_ast::helpers::{find_keyword, includes_arg_name}; use ruff_python_ast::identifier::Identifier; use ruff_python_ast::visitor; use ruff_python_ast::visitor::Visitor; @@ -306,11 +306,7 @@ fn check_fixture_decorator(checker: &mut Checker, func_name: &str, decorator: &D } if checker.enabled(Rule::PytestExtraneousScopeFunction) { - let scope_keyword = keywords - .iter() - .find(|kw| kw.arg.as_ref().map_or(false, |arg| arg == "scope")); - - if let Some(scope_keyword) = scope_keyword { + if let Some(scope_keyword) = find_keyword(keywords, "scope") { if keyword_is_literal(scope_keyword, "function") { let mut diagnostic = Diagnostic::new(PytestExtraneousScopeFunction, scope_keyword.range()); From cdddf88d8d348f77fb6b7b92bce265e396218679 Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sun, 23 Jul 2023 02:01:05 +0100 Subject: [PATCH 2/4] Check for unary prefix decrement op --- .../test/fixtures/flake8_bugbear/B002.py | 12 ++- crates/ruff/src/checkers/ast/mod.rs | 6 +- crates/ruff/src/codes.rs | 2 +- crates/ruff/src/rules/flake8_bugbear/mod.rs | 2 +- .../src/rules/flake8_bugbear/rules/mod.rs | 4 +- .../rules/unary_prefix_increment.rs | 57 ------------- .../rules/unary_prefix_increment_decrement.rs | 85 +++++++++++++++++++ ...__flake8_bugbear__tests__B002_B002.py.snap | 31 +++++-- 8 files changed, 125 insertions(+), 74 deletions(-) delete mode 100644 crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment.rs create mode 100644 crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py index 41b53c41aac4d..7f49f2598a8c8 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py @@ -1,6 +1,6 @@ """ Should emit: -B002 - on lines 15 and 20 +B002 - on lines 18, 19, 24, and 28 """ @@ -8,13 +8,17 @@ def this_is_all_fine(n): x = n + 1 y = 1 + n z = +x + y - return +z + a = n - 1 + b = 1 - n + c = -a - b + return +z, -c def this_is_buggy(n): x = ++n - return x + y = --n + return x, y def this_is_buggy_too(n): - return ++n + return ++n, --n diff --git a/crates/ruff/src/checkers/ast/mod.rs b/crates/ruff/src/checkers/ast/mod.rs index 9dd96ce866426..c9e7bf08ef497 100644 --- a/crates/ruff/src/checkers/ast/mod.rs +++ b/crates/ruff/src/checkers/ast/mod.rs @@ -3302,8 +3302,10 @@ where check_not_is, ); } - if self.enabled(Rule::UnaryPrefixIncrement) { - flake8_bugbear::rules::unary_prefix_increment(self, expr, *op, operand); + if self.enabled(Rule::UnaryPrefixIncrementDecrement) { + flake8_bugbear::rules::unary_prefix_increment_decrement( + self, expr, *op, operand, + ); } if self.enabled(Rule::NegateEqualOp) { flake8_simplify::rules::negation_with_equal_op(self, expr, *op, operand); diff --git a/crates/ruff/src/codes.rs b/crates/ruff/src/codes.rs index 745489e464413..7ca269945b568 100644 --- a/crates/ruff/src/codes.rs +++ b/crates/ruff/src/codes.rs @@ -235,7 +235,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Flake8Builtins, "003") => (RuleGroup::Unspecified, rules::flake8_builtins::rules::BuiltinAttributeShadowing), // flake8-bugbear - (Flake8Bugbear, "002") => (RuleGroup::Unspecified, rules::flake8_bugbear::rules::UnaryPrefixIncrement), + (Flake8Bugbear, "002") => (RuleGroup::Unspecified, rules::flake8_bugbear::rules::UnaryPrefixIncrementDecrement), (Flake8Bugbear, "003") => (RuleGroup::Unspecified, rules::flake8_bugbear::rules::AssignmentToOsEnviron), (Flake8Bugbear, "004") => (RuleGroup::Unspecified, rules::flake8_bugbear::rules::UnreliableCallableCheck), (Flake8Bugbear, "005") => (RuleGroup::Unspecified, rules::flake8_bugbear::rules::StripWithMultiCharacters), diff --git a/crates/ruff/src/rules/flake8_bugbear/mod.rs b/crates/ruff/src/rules/flake8_bugbear/mod.rs index df1c4632503c4..005fe03002145 100644 --- a/crates/ruff/src/rules/flake8_bugbear/mod.rs +++ b/crates/ruff/src/rules/flake8_bugbear/mod.rs @@ -42,7 +42,7 @@ mod tests { #[test_case(Rule::SetAttrWithConstant, Path::new("B009_B010.py"))] #[test_case(Rule::StarArgUnpackingAfterKeywordArg, Path::new("B026.py"))] #[test_case(Rule::StripWithMultiCharacters, Path::new("B005.py"))] - #[test_case(Rule::UnaryPrefixIncrement, Path::new("B002.py"))] + #[test_case(Rule::UnaryPrefixIncrementDecrement, Path::new("B002.py"))] #[test_case(Rule::UnintentionalTypeAnnotation, Path::new("B032.py"))] #[test_case(Rule::UnreliableCallableCheck, Path::new("B004.py"))] #[test_case(Rule::UnusedLoopControlVariable, Path::new("B007.py"))] diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs b/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs index 3a1cfb474f28c..3f8a1fdaf646f 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/mod.rs @@ -23,7 +23,7 @@ pub(crate) use reuse_of_groupby_generator::*; pub(crate) use setattr_with_constant::*; pub(crate) use star_arg_unpacking_after_keyword_arg::*; pub(crate) use strip_with_multi_characters::*; -pub(crate) use unary_prefix_increment::*; +pub(crate) use unary_prefix_increment_decrement::*; pub(crate) use unintentional_type_annotation::*; pub(crate) use unreliable_callable_check::*; pub(crate) use unused_loop_control_variable::*; @@ -57,7 +57,7 @@ mod reuse_of_groupby_generator; mod setattr_with_constant; mod star_arg_unpacking_after_keyword_arg; mod strip_with_multi_characters; -mod unary_prefix_increment; +mod unary_prefix_increment_decrement; mod unintentional_type_annotation; mod unreliable_callable_check; mod unused_loop_control_variable; diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment.rs b/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment.rs deleted file mode 100644 index e9a096aaccb71..0000000000000 --- a/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment.rs +++ /dev/null @@ -1,57 +0,0 @@ -use rustpython_parser::ast::{self, Expr, Ranged, UnaryOp}; - -use ruff_diagnostics::{Diagnostic, Violation}; -use ruff_macros::{derive_message_formats, violation}; - -use crate::checkers::ast::Checker; - -/// ## What it does -/// Checks for uses of the unary prefix increment operator (e.g., `++n`). -/// -/// ## Why is this bad? -/// Python does not support the unary prefix increment operator. Writing `++n` -/// is equivalent to `+(+(n))`, which is equivalent to `n`. -/// -/// ## Example -/// ```python -/// ++n -/// ``` -/// -/// Use instead: -/// ```python -/// n += 1 -/// ``` -/// -/// ## References -/// - [Python documentation: Unary arithmetic and bitwise operations](https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations) -/// - [Python documentation: Augmented assignment statements](https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements) -#[violation] -pub struct UnaryPrefixIncrement; - -impl Violation for UnaryPrefixIncrement { - #[derive_message_formats] - fn message(&self) -> String { - format!("Python does not support the unary prefix increment") - } -} - -/// B002 -pub(crate) fn unary_prefix_increment( - checker: &mut Checker, - expr: &Expr, - op: UnaryOp, - operand: &Expr, -) { - if !matches!(op, UnaryOp::UAdd) { - return; - } - let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = operand else { - return; - }; - if !matches!(op, UnaryOp::UAdd) { - return; - } - checker - .diagnostics - .push(Diagnostic::new(UnaryPrefixIncrement, expr.range())); -} diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs b/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs new file mode 100644 index 0000000000000..afb4ca95db218 --- /dev/null +++ b/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs @@ -0,0 +1,85 @@ +use rustpython_parser::ast::{self, Expr, Ranged, UnaryOp}; + +use ruff_diagnostics::{Diagnostic, Violation}; +use ruff_macros::{derive_message_formats, violation}; + +use crate::checkers::ast::Checker; + +/// ## What it does +/// Checks for the attempted use of the unary prefix increment (`++`) or +/// decrement operator (`--`). +/// +/// ## Why is this bad? +/// Python does not support the unary prefix increment or decrement operator. +/// Writing `++n` is equivalent to `+(+(n))` and writing `--n` is equivalent to +/// `-(-(n))`. In both cases, it is equivalent to `n`. +/// +/// ## Example +/// ```python +/// ++x +/// --y +/// ``` +/// +/// Use instead: +/// ```python +/// x += 1 +/// y -= 1 +/// ``` +/// +/// ## References +/// - [Python documentation: Unary arithmetic and bitwise operations](https://docs.python.org/3/reference/expressions.html#unary-arithmetic-and-bitwise-operations) +/// - [Python documentation: Augmented assignment statements](https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements) +#[violation] +pub struct UnaryPrefixIncrementDecrement { + operator: UnaryPrefixOperatorType, +} + +impl Violation for UnaryPrefixIncrementDecrement { + #[derive_message_formats] + fn message(&self) -> String { + let UnaryPrefixIncrementDecrement { operator } = self; + match operator { + UnaryPrefixOperatorType::Increment => { + format!("Python does not support the unary prefix increment operator `++`") + } + UnaryPrefixOperatorType::Decrement => { + format!("Python does not support the unary prefix decrement operator `--`") + } + } + } +} + +/// B002 +pub(crate) fn unary_prefix_increment_decrement( + checker: &mut Checker, + expr: &Expr, + op: UnaryOp, + operand: &Expr, +) { + if !matches!(op, UnaryOp::UAdd | UnaryOp::USub) { + return; + } + if let Expr::UnaryOp(ast::ExprUnaryOp { op: inner_op, .. }) = operand { + if matches!(op, UnaryOp::UAdd) && matches!(inner_op, UnaryOp::UAdd) { + checker.diagnostics.push(Diagnostic::new( + UnaryPrefixIncrementDecrement { + operator: UnaryPrefixOperatorType::Increment, + }, + expr.range(), + )); + } else if matches!(op, UnaryOp::USub) && matches!(inner_op, UnaryOp::USub) { + checker.diagnostics.push(Diagnostic::new( + UnaryPrefixIncrementDecrement { + operator: UnaryPrefixOperatorType::Decrement, + }, + expr.range(), + )); + } + } +} + +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +enum UnaryPrefixOperatorType { + Increment, + Decrement, +} diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap index 5afe7c81896c2..6b620d92d0675 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap @@ -1,19 +1,36 @@ --- source: crates/ruff/src/rules/flake8_bugbear/mod.rs --- -B002.py:15:9: B002 Python does not support the unary prefix increment +B002.py:18:9: B002 Python does not support the unary prefix increment operator `++` | -14 | def this_is_buggy(n): -15 | x = ++n +17 | def this_is_buggy(n): +18 | x = ++n | ^^^ B002 -16 | return x +19 | y = --n +20 | return x, y | -B002.py:20:12: B002 Python does not support the unary prefix increment +B002.py:19:9: B002 Python does not support the unary prefix decrement operator `--` | -19 | def this_is_buggy_too(n): -20 | return ++n +17 | def this_is_buggy(n): +18 | x = ++n +19 | y = --n + | ^^^ B002 +20 | return x, y + | + +B002.py:24:12: B002 Python does not support the unary prefix increment operator `++` + | +23 | def this_is_buggy_too(n): +24 | return ++n, --n | ^^^ B002 | +B002.py:24:17: B002 Python does not support the unary prefix decrement operator `--` + | +23 | def this_is_buggy_too(n): +24 | return ++n, --n + | ^^^ B002 + | + From 2d88a6b1c51e08449e65798d547699696a3ac501 Mon Sep 17 00:00:00 2001 From: Tom Kuson Date: Sun, 23 Jul 2023 02:06:42 +0100 Subject: [PATCH 3/4] Fix comment --- crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py index 7f49f2598a8c8..ac57eeef35dc6 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B002.py @@ -1,6 +1,6 @@ """ Should emit: -B002 - on lines 18, 19, 24, and 28 +B002 - on lines 18, 19, and 24 """ From b9e6aed82a79d8503288c1f36f39a9c7e344f3c8 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 22 Jul 2023 21:33:35 -0400 Subject: [PATCH 4/4] Tweaks --- .../rules/unary_prefix_increment_decrement.rs | 16 +++++++++------- ...les__flake8_bugbear__tests__B002_B002.py.snap | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs b/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs index afb4ca95db218..b562491334596 100644 --- a/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs +++ b/crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs @@ -40,10 +40,10 @@ impl Violation for UnaryPrefixIncrementDecrement { let UnaryPrefixIncrementDecrement { operator } = self; match operator { UnaryPrefixOperatorType::Increment => { - format!("Python does not support the unary prefix increment operator `++`") + format!("Python does not support the unary prefix increment operator (`++`)") } UnaryPrefixOperatorType::Decrement => { - format!("Python does not support the unary prefix decrement operator `--`") + format!("Python does not support the unary prefix decrement operator (`--`)") } } } @@ -56,18 +56,19 @@ pub(crate) fn unary_prefix_increment_decrement( op: UnaryOp, operand: &Expr, ) { - if !matches!(op, UnaryOp::UAdd | UnaryOp::USub) { + let Expr::UnaryOp(ast::ExprUnaryOp { op: nested_op, .. }) = operand else { return; - } - if let Expr::UnaryOp(ast::ExprUnaryOp { op: inner_op, .. }) = operand { - if matches!(op, UnaryOp::UAdd) && matches!(inner_op, UnaryOp::UAdd) { + }; + match (op, nested_op) { + (UnaryOp::UAdd, UnaryOp::UAdd) => { checker.diagnostics.push(Diagnostic::new( UnaryPrefixIncrementDecrement { operator: UnaryPrefixOperatorType::Increment, }, expr.range(), )); - } else if matches!(op, UnaryOp::USub) && matches!(inner_op, UnaryOp::USub) { + } + (UnaryOp::USub, UnaryOp::USub) => { checker.diagnostics.push(Diagnostic::new( UnaryPrefixIncrementDecrement { operator: UnaryPrefixOperatorType::Decrement, @@ -75,6 +76,7 @@ pub(crate) fn unary_prefix_increment_decrement( expr.range(), )); } + _ => {} } } diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap index 6b620d92d0675..d20338980ce82 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff/src/rules/flake8_bugbear/mod.rs --- -B002.py:18:9: B002 Python does not support the unary prefix increment operator `++` +B002.py:18:9: B002 Python does not support the unary prefix increment operator (`++`) | 17 | def this_is_buggy(n): 18 | x = ++n @@ -10,7 +10,7 @@ B002.py:18:9: B002 Python does not support the unary prefix increment operator ` 20 | return x, y | -B002.py:19:9: B002 Python does not support the unary prefix decrement operator `--` +B002.py:19:9: B002 Python does not support the unary prefix decrement operator (`--`) | 17 | def this_is_buggy(n): 18 | x = ++n @@ -19,14 +19,14 @@ B002.py:19:9: B002 Python does not support the unary prefix decrement operator ` 20 | return x, y | -B002.py:24:12: B002 Python does not support the unary prefix increment operator `++` +B002.py:24:12: B002 Python does not support the unary prefix increment operator (`++`) | 23 | def this_is_buggy_too(n): 24 | return ++n, --n | ^^^ B002 | -B002.py:24:17: B002 Python does not support the unary prefix decrement operator `--` +B002.py:24:17: B002 Python does not support the unary prefix decrement operator (`--`) | 23 | def this_is_buggy_too(n): 24 | return ++n, --n