Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Implement if-stmt-min-max (PLR1730, PLR1731) #10002

Merged
merged 10 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
tibor-reiss committed Feb 15, 2024
commit 16f897182f2f849fc43639b39fc14a720dd059c4
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# pylint: disable=missing-docstring, invalid-name, too-few-public-methods, redefined-outer-name

value = 10
value2 = 0
value3 = 3

# Positive
if value < 10: # [max-instead-of-if]
value = 10

if value <= 10: # [max-instead-of-if]
value = 10

if value < value2: # [max-instead-of-if]
value = value2


class A:
def __init__(self):
self.value = 13


A1 = A()
if A1.value < 10: # [max-instead-of-if]
A1.value = 10


class AA:
def __init__(self, value):
self.value = value

def __gt__(self, b):
return self.value > b

def __ge__(self, b):
return self.value >= b

def __lt__(self, b):
return self.value < b

def __le__(self, b):
return self.value <= b


A1 = AA(0)
A2 = AA(3)

if A2 < A1: # [max-instead-of-if]
A2 = A1

if A2 <= A1: # [max-instead-of-if]
A2 = A1

# Negative
if value < 10:
value = 2

if value <= 3:
value = 5

if value < 10:
value = 2
value2 = 3

if value < value2:
value = value3

if value < 5:
value = value3

if 2 < value <= 3:
value = 1

if value < 10:
value = 10
else:
value = 3

if value <= 3:
value = 5
elif value == 3:
value = 2
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod tests {
#[test_case(Rule::EqWithoutHash, Path::new("eq_without_hash.py"))]
#[test_case(Rule::EmptyComment, Path::new("empty_comment.py"))]
#[test_case(Rule::ManualFromImport, Path::new("import_aliasing.py"))]
#[test_case(Rule::MaxInsteadOfIf, Path::new("max_instead_of_if.py"))]
#[test_case(Rule::SingleStringSlots, Path::new("single_string_slots.py"))]
#[test_case(Rule::SysExitAlias, Path::new("sys_exit_alias_0.py"))]
#[test_case(Rule::SysExitAlias, Path::new("sys_exit_alias_1.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub(crate) fn max_instead_of_if(checker: &mut Checker, stmt_if: &ast::StmtIf) {
&& !left.is_subscript_expr()
) {return;}

let ([op], [right_statement]) = (ops.as_slice(), comparators.as_slice()) else {
let ([op], [right_statement]) = (&**ops, &**comparators) else {
return;
};

Expand All @@ -112,8 +112,8 @@ pub(crate) fn max_instead_of_if(checker: &mut Checker, stmt_if: &ast::StmtIf) {
let value_node = ast::ExprCall {
func: Box::new(func_node.into()),
arguments: Arguments {
args: vec![body_target.clone(), body_value.deref().clone()],
keywords: vec![],
args: Box::from([body_target.clone(), body_value.deref().clone()]),
keywords: Box::from([]),
range: TextRange::default(),
},
range: TextRange::default(),
Expand Down Expand Up @@ -169,7 +169,7 @@ fn match_right(right_statement: &Expr, body_value: &Box<Expr>) -> bool {
LiteralExpressionRef::BytesLiteral(ast::ExprBytesLiteral{value: value1, ..}),
LiteralExpressionRef::BytesLiteral(ast::ExprBytesLiteral{value: value2, ..})
) => {
return value1.iter().map(|b| b.value.as_slice()).eq(value2.iter().map(|b| b.value.as_slice()))
return value1.iter().map(|b| b.as_slice()).eq(value2.iter().map(|b| b.as_slice()))
},
(
LiteralExpressionRef::StringLiteral(ast::ExprStringLiteral{value: value1, ..}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
max_instead_of_if.py:8:1: PLR1731 Consider using `value = max(value, 10)` instead of unnecessary if block
|
7 | # Positive
8 | / if value < 10: # [max-instead-of-if]
9 | | value = 10
| |______________^ PLR1731
10 |
11 | if value <= 10: # [max-instead-of-if]
|

max_instead_of_if.py:11:1: PLR1731 Consider using `value = max(value, 10)` instead of unnecessary if block
|
9 | value = 10
10 |
11 | / if value <= 10: # [max-instead-of-if]
12 | | value = 10
| |______________^ PLR1731
13 |
14 | if value < value2: # [max-instead-of-if]
|

max_instead_of_if.py:14:1: PLR1731 Consider using `value = max(value, value2)` instead of unnecessary if block
|
12 | value = 10
13 |
14 | / if value < value2: # [max-instead-of-if]
15 | | value = value2
| |__________________^ PLR1731
|

max_instead_of_if.py:24:1: PLR1731 Consider using `A1.value = max(A1.value, 10)` instead of unnecessary if block
|
23 | A1 = A()
24 | / if A1.value < 10: # [max-instead-of-if]
25 | | A1.value = 10
| |_________________^ PLR1731
|

max_instead_of_if.py:48:1: PLR1731 Consider using `A2 = max(A2, A1)` instead of unnecessary if block
|
46 | A2 = AA(3)
47 |
48 | / if A2 < A1: # [max-instead-of-if]
49 | | A2 = A1
| |___________^ PLR1731
50 |
51 | if A2 <= A1: # [max-instead-of-if]
|

max_instead_of_if.py:51:1: PLR1731 Consider using `A2 = max(A2, A1)` instead of unnecessary if block
|
49 | A2 = A1
50 |
51 | / if A2 <= A1: # [max-instead-of-if]
52 | | A2 = A1
| |___________^ PLR1731
53 |
54 | # Negative
|


1 change: 1 addition & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.