-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extends
B002
to detect unary prefix decrement operators (#5998)
## Summary Extends `B002` to detect unary decrement prefix operators. Closes #5992. ## Test Plan `cargo test`
- Loading branch information
Showing
8 changed files
with
127 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
""" | ||
Should emit: | ||
B002 - on lines 15 and 20 | ||
B002 - on lines 18, 19, and 24 | ||
""" | ||
|
||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment.rs
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
crates/ruff/src/rules/flake8_bugbear/rules/unary_prefix_increment_decrement.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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, | ||
) { | ||
let Expr::UnaryOp(ast::ExprUnaryOp { op: nested_op, .. }) = operand else { | ||
return; | ||
}; | ||
match (op, nested_op) { | ||
(UnaryOp::UAdd, UnaryOp::UAdd) => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnaryPrefixIncrementDecrement { | ||
operator: UnaryPrefixOperatorType::Increment, | ||
}, | ||
expr.range(), | ||
)); | ||
} | ||
(UnaryOp::USub, UnaryOp::USub) => { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnaryPrefixIncrementDecrement { | ||
operator: UnaryPrefixOperatorType::Decrement, | ||
}, | ||
expr.range(), | ||
)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
enum UnaryPrefixOperatorType { | ||
Increment, | ||
Decrement, | ||
} |
31 changes: 24 additions & 7 deletions
31
.../src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B002_B002.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
| | ||
|
||
|