`PLR2004`: Accept 0.0 and 1.0 as common magic values (#9964)

## Summary

Accept 0.0 and 1.0 as common magic values. This is in line with the
pylint behaviour, and I think makes sense conceptually.


## Test Plan

Test cases were added to
`crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py`
This commit is contained in:
Auguste Lalande 2024-02-12 20:21:06 -05:00 committed by GitHub
parent 5bc0d9c324
commit 8fba97f72f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 100 additions and 47 deletions

View File

@ -35,6 +35,15 @@ if argc != 0: # correct
if argc != 1: # correct if argc != 1: # correct
pass pass
if argc != -1.0: # correct
pass
if argc != 0.0: # correct
pass
if argc != 1.0: # correct
pass
if argc != 2: # [magic-value-comparison] if argc != 2: # [magic-value-comparison]
pass pass
@ -44,6 +53,12 @@ if argc != -2: # [magic-value-comparison]
if argc != +2: # [magic-value-comparison] if argc != +2: # [magic-value-comparison]
pass pass
if argc != -2.0: # [magic-value-comparison]
pass
if argc != +2.0: # [magic-value-comparison]
pass
if __name__ == "__main__": # correct if __name__ == "__main__": # correct
pass pass

View File

@ -86,8 +86,10 @@ fn is_magic_value(literal_expr: LiteralExpressionRef, allowed_types: &[ConstantT
!matches!(value.to_str(), "" | "__main__") !matches!(value.to_str(), "" | "__main__")
} }
LiteralExpressionRef::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value { LiteralExpressionRef::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {
#[allow(clippy::float_cmp)]
ast::Number::Float(value) => !(*value == 0.0 || *value == 1.0),
ast::Number::Int(value) => !matches!(*value, Int::ZERO | Int::ONE), ast::Number::Int(value) => !matches!(*value, Int::ZERO | Int::ONE),
_ => true, ast::Number::Complex { .. } => true,
}, },
LiteralExpressionRef::BytesLiteral(_) => true, LiteralExpressionRef::BytesLiteral(_) => true,
} }

View File

@ -10,49 +10,67 @@ magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider
6 | pass 6 | pass
| |
magic_value_comparison.py:38:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable magic_value_comparison.py:47:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
| |
36 | pass
37 |
38 | if argc != 2: # [magic-value-comparison]
| ^ PLR2004
39 | pass
|
magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable
|
39 | pass
40 |
41 | if argc != -2: # [magic-value-comparison]
| ^^ PLR2004
42 | pass
|
magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable
|
42 | pass
43 |
44 | if argc != +2: # [magic-value-comparison]
| ^^ PLR2004
45 | pass 45 | pass
46 |
47 | if argc != 2: # [magic-value-comparison]
| ^ PLR2004
48 | pass
| |
magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable magic_value_comparison.py:50:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable
| |
63 | pi_estimation = 3.14 48 | pass
64 | 49 |
65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] 50 | if argc != -2: # [magic-value-comparison]
| ^^ PLR2004
51 | pass
|
magic_value_comparison.py:53:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable
|
51 | pass
52 |
53 | if argc != +2: # [magic-value-comparison]
| ^^ PLR2004
54 | pass
|
magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable
|
54 | pass
55 |
56 | if argc != -2.0: # [magic-value-comparison]
| ^^^^ PLR2004
57 | pass
|
magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable
|
57 | pass
58 |
59 | if argc != +2.0: # [magic-value-comparison]
| ^^^^ PLR2004
60 | pass
|
magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
78 | pi_estimation = 3.14
79 |
80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
| ^^^^^^^^^^^^^^^^^^^^ PLR2004 | ^^^^^^^^^^^^^^^^^^^^ PLR2004
66 | pass 81 | pass
| |
magic_value_comparison.py:71:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable magic_value_comparison.py:86:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable
| |
69 | pass 84 | pass
70 | 85 |
71 | if pi_estimation == 0x3: # [magic-value-comparison] 86 | if pi_estimation == 0x3: # [magic-value-comparison]
| ^^^ PLR2004 | ^^^ PLR2004
72 | pass 87 | pass
| |

View File

@ -1,31 +1,49 @@
--- ---
source: crates/ruff_linter/src/rules/pylint/mod.rs source: crates/ruff_linter/src/rules/pylint/mod.rs
--- ---
magic_value_comparison.py:59:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable
|
54 | pass
55 |
56 | if argc != -2.0: # [magic-value-comparison]
| ^^^^ PLR2004
57 | pass
|
magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable
| |
57 | pass 57 | pass
58 | 58 |
59 | if input_password == "Hunter2": # correct 59 | if argc != +2.0: # [magic-value-comparison]
| ^^^^^^^^^ PLR2004 | ^^^^ PLR2004
60 | pass 60 | pass
| |
magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable magic_value_comparison.py:74:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable
| |
63 | pi_estimation = 3.14 72 | pass
64 | 73 |
65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] 74 | if input_password == "Hunter2": # correct
| ^^^^^^^^^ PLR2004
75 | pass
|
magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
78 | pi_estimation = 3.14
79 |
80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
| ^^^^^^^^^^^^^^^^^^^^ PLR2004 | ^^^^^^^^^^^^^^^^^^^^ PLR2004
66 | pass 81 | pass
| |
magic_value_comparison.py:77:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable magic_value_comparison.py:92:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable
| |
75 | user_input = b"Hello, There!" 90 | user_input = b"Hello, There!"
76 | 91 |
77 | if user_input == b"something": # correct 92 | if user_input == b"something": # correct
| ^^^^^^^^^^^^ PLR2004 | ^^^^^^^^^^^^ PLR2004
78 | pass 93 | pass
| |