Skip to content

Commit

Permalink
[pylint] Show verbatim constant in magic-value-comparison (`PLR20…
Browse files Browse the repository at this point in the history
…04`) (#9694)

## Summary

Tweaks PLR2004 to show the literal source text, rather than the constant
value.

I noticed this when I had a hexadecimal constant, and the linter turned
it into base-10.

Now, if you have `0x300`, it will show `0x300` instead of `768`.

Also, added backticks around the constant in the output message.

## Test Plan

`cargo test`
  • Loading branch information
diceroll123 authored Jan 30, 2024
1 parent 95e1444 commit dacda0f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
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
|


0 comments on commit dacda0f

Please sign in to comment.