-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ruff] implement useless if-else (RUF034) (#13218)
- Loading branch information
1 parent
862bd0c
commit e37bde4
Showing
8 changed files
with
101 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Valid | ||
x = 1 if True else 2 | ||
|
||
# Invalid | ||
x = 1 if True else 1 | ||
|
||
# Invalid | ||
x = "a" if True else "a" | ||
|
||
# Invalid | ||
x = 0.1 if False else 0.1 |
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
55 changes: 55 additions & 0 deletions
55
crates/ruff_linter/src/rules/ruff/rules/useless_if_else.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,55 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_python_ast::comparable::ComparableExpr; | ||
|
||
/// ## What it does | ||
/// Checks for useless if-else conditions with identical arms. | ||
/// | ||
/// ## Why is this bad? | ||
/// Useless if-else conditions add unnecessary complexity to the code without | ||
/// providing any logical benefit. | ||
/// | ||
/// Assigning the value directly is clearer and more explicit, and | ||
/// should be preferred. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// # Bad | ||
/// foo = x if y else x | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// # Good | ||
/// foo = x | ||
/// ``` | ||
#[violation] | ||
pub struct UselessIfElse; | ||
|
||
impl Violation for UselessIfElse { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Useless if-else condition") | ||
} | ||
} | ||
|
||
/// RUF031 | ||
pub(crate) fn useless_if_else(checker: &mut Checker, if_expr: &ast::ExprIf) { | ||
let ast::ExprIf { | ||
body, | ||
orelse, | ||
range, | ||
.. | ||
} = if_expr; | ||
|
||
// Skip if the body and orelse are not the same | ||
if ComparableExpr::from(body) != ComparableExpr::from(orelse) { | ||
return; | ||
} | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(UselessIfElse, *range)); | ||
} |
27 changes: 27 additions & 0 deletions
27
...ff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF034_RUF034.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
--- | ||
RUF034.py:5:5: RUF034 Useless if-else condition | ||
| | ||
4 | # Invalid | ||
5 | x = 1 if True else 1 | ||
| ^^^^^^^^^^^^^^^^ RUF034 | ||
6 | | ||
7 | # Invalid | ||
| | ||
|
||
RUF034.py:8:5: RUF034 Useless if-else condition | ||
| | ||
7 | # Invalid | ||
8 | x = "a" if True else "a" | ||
| ^^^^^^^^^^^^^^^^^^^^ RUF034 | ||
9 | | ||
10 | # Invalid | ||
| | ||
|
||
RUF034.py:11:5: RUF034 Useless if-else condition | ||
| | ||
10 | # Invalid | ||
11 | x = 0.1 if False else 0.1 | ||
| ^^^^^^^^^^^^^^^^^^^^^ RUF034 | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.