[pylint] Show verbatim constant in magic-value-comparison (PLR2004) (#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`
This commit is contained in:
Steve C 2024-01-30 00:22:09 -05:00 committed by GitHub
parent 95e1444c90
commit dacda0f202
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 14 deletions

View file

@ -68,6 +68,9 @@ if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
if pi_estimation == PI: # correct if pi_estimation == PI: # correct
pass pass
if pi_estimation == 0x3: # [magic-value-comparison]
pass
HELLO_WORLD = b"Hello, World!" HELLO_WORLD = b"Hello, World!"
user_input = b"Hello, There!" user_input = b"Hello, There!"

View file

@ -52,7 +52,7 @@ impl Violation for MagicValueComparison {
fn message(&self) -> String { fn message(&self) -> String {
let MagicValueComparison { value } = self; let MagicValueComparison { value } = self;
format!( 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"
) )
} }
} }
@ -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) { if is_magic_value(value, &checker.settings.pylint.allow_magic_value_types) {
checker.diagnostics.push(Diagnostic::new( checker.diagnostics.push(Diagnostic::new(
MagicValueComparison { MagicValueComparison {
value: checker.generator().expr(comparison_expr), value: checker.locator().slice(comparison_expr).to_string(),
}, },
comparison_expr.range(), comparison_expr.range(),
)); ));

View file

@ -1,7 +1,7 @@
--- ---
source: crates/ruff_linter/src/rules/pylint/mod.rs 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 3 | user_input = 10
4 | 4 |
@ -10,7 +10,7 @@ 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:38:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
| |
36 | pass 36 | pass
37 | 37 |
@ -19,7 +19,7 @@ magic_value_comparison.py:38:12: PLR2004 Magic value used in comparison, conside
39 | pass 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 39 | pass
40 | 40 |
@ -28,7 +28,7 @@ magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, conside
42 | pass 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 42 | pass
43 | 43 |
@ -37,7 +37,7 @@ magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, conside
45 | pass 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 63 | pi_estimation = 3.14
64 | 64 |
@ -46,4 +46,13 @@ magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, conside
66 | pass 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
|

View file

@ -1,7 +1,7 @@
--- ---
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:59:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable
| |
57 | pass 57 | pass
58 | 58 |
@ -10,7 +10,7 @@ magic_value_comparison.py:59:22: PLR2004 Magic value used in comparison, conside
60 | pass 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 63 | pi_estimation = 3.14
64 | 64 |
@ -19,13 +19,13 @@ magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, conside
66 | pass 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!" 75 | user_input = b"Hello, There!"
73 | 76 |
74 | if user_input == b"something": # correct 77 | if user_input == b"something": # correct
| ^^^^^^^^^^^^ PLR2004 | ^^^^^^^^^^^^ PLR2004
75 | pass 78 | pass
| |