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] Show verbatim constant in magic-value-comparison (PLR2004) #9694

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
if pi_estimation == PI: # correct
pass

if pi_estimation == 0x3: # [magic-value-comparison]
pass

HELLO_WORLD = b"Hello, World!"
user_input = b"Hello, There!"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Violation for MagicValueComparison {
fn message(&self) -> String {
let MagicValueComparison { value } = self;
format!(
"Magic value used in comparison, consider replacing {value} with a constant variable"
"Magic value used in comparison, consider replacing `{value}` with a constant variable"
)
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ pub(crate) fn magic_value_comparison(checker: &mut Checker, left: &Expr, compara
if is_magic_value(value, &checker.settings.pylint.allow_magic_value_types) {
checker.diagnostics.push(Diagnostic::new(
MagicValueComparison {
value: checker.generator().expr(comparison_expr),
value: checker.locator().slice(comparison_expr).to_string(),
},
comparison_expr.range(),
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider replacing 10 with a constant variable
magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider replacing `10` with a constant variable
|
3 | user_input = 10
4 |
Expand All @@ -10,7 +10,7 @@ magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider
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:38:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
|
36 | pass
37 |
Expand All @@ -19,7 +19,7 @@ magic_value_comparison.py:38:12: PLR2004 Magic value used in comparison, conside
39 | pass
|

magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, consider replacing -2 with a constant variable
magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable
|
39 | pass
40 |
Expand All @@ -28,7 +28,7 @@ magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, conside
42 | pass
|

magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, consider replacing +2 with a constant variable
magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable
|
42 | pass
43 |
Expand All @@ -37,7 +37,7 @@ magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, conside
45 | pass
|

magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing 3.141592653589793 with a constant variable
magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
63 | pi_estimation = 3.14
64 |
Expand All @@ -46,4 +46,13 @@ magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, conside
66 | pass
|

magic_value_comparison.py:71:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable
|
69 | pass
70 |
71 | if pi_estimation == 0x3: # [magic-value-comparison]
| ^^^ PLR2004
72 | pass
|


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
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:59:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable
|
57 | pass
58 |
Expand All @@ -10,7 +10,7 @@ magic_value_comparison.py:59:22: PLR2004 Magic value used in comparison, conside
60 | pass
|

magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing 3.141592653589793 with a constant variable
magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
63 | pi_estimation = 3.14
64 |
Expand All @@ -19,13 +19,13 @@ magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, conside
66 | pass
|

magic_value_comparison.py:74:18: PLR2004 Magic value used in comparison, consider replacing b"something" with a constant variable
magic_value_comparison.py:77:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable
|
72 | user_input = b"Hello, There!"
73 |
74 | if user_input == b"something": # correct
75 | user_input = b"Hello, There!"
76 |
77 | if user_input == b"something": # correct
| ^^^^^^^^^^^^ PLR2004
75 | pass
78 | pass
|


Loading