[`ruff`] Do not flag `%r` + `repr()` combinations (`RUF065`) (#20600)

## Summary

Fixes the first part of #20583
This commit is contained in:
Dan Parizher 2025-09-30 15:49:50 -04:00 committed by GitHub
parent 2b1d3c60fa
commit 7c87b31533
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 44 deletions

View File

@ -37,3 +37,9 @@ log(logging.INFO, "Hello %r", repr("World!"))
def str(s): return f"str = {s}" def str(s): return f"str = {s}"
# Don't flag this # Don't flag this
logging.info("Hello %s", str("World!")) logging.info("Hello %s", str("World!"))
logging.info("Debug info: %r", repr("test\nstring"))
logging.warning("Value: %r", repr(42))
logging.error("Error: %r", repr([1, 2, 3]))
logging.info("Debug info: %s", repr("test\nstring"))
logging.warning("Value: %s", repr(42))

View File

@ -117,11 +117,9 @@ pub(crate) fn logging_eager_conversion(checker: &Checker, call: &ast::ExprCall)
continue; continue;
}; };
// Check for use of %s with str() or %r with repr() // Check for use of %s with str()
if checker.semantic().match_builtin_expr(func.as_ref(), "str") if checker.semantic().match_builtin_expr(func.as_ref(), "str")
&& matches!(format_conversion, FormatConversion::Str) && matches!(format_conversion, FormatConversion::Str)
|| checker.semantic().match_builtin_expr(func.as_ref(), "repr")
&& matches!(format_conversion, FormatConversion::Repr)
{ {
checker checker
.report_diagnostic(LoggingEagerConversion { format_conversion }, arg.range()); .report_diagnostic(LoggingEagerConversion { format_conversion }, arg.range());

View File

@ -1,6 +1,5 @@
--- ---
source: crates/ruff_linter/src/rules/ruff/mod.rs source: crates/ruff_linter/src/rules/ruff/mod.rs
assertion_line: 124
--- ---
RUF065 Unnecessary `str()` conversion when formatting with `%s` RUF065 Unnecessary `str()` conversion when formatting with `%s`
--> RUF065.py:4:26 --> RUF065.py:4:26
@ -22,26 +21,6 @@ RUF065 Unnecessary `str()` conversion when formatting with `%s`
7 | # %s + repr() 7 | # %s + repr()
| |
RUF065 Unnecessary `repr()` conversion when formatting with `%r`
--> RUF065.py:16:26
|
15 | # %r + repr()
16 | logging.info("Hello %r", repr("World!"))
| ^^^^^^^^^^^^^^
17 | logging.log(logging.INFO, "Hello %r", repr("World!"))
|
RUF065 Unnecessary `repr()` conversion when formatting with `%r`
--> RUF065.py:17:39
|
15 | # %r + repr()
16 | logging.info("Hello %r", repr("World!"))
17 | logging.log(logging.INFO, "Hello %r", repr("World!"))
| ^^^^^^^^^^^^^^
18 |
19 | from logging import info, log
|
RUF065 Unnecessary `str()` conversion when formatting with `%s` RUF065 Unnecessary `str()` conversion when formatting with `%s`
--> RUF065.py:22:18 --> RUF065.py:22:18
| |
@ -61,23 +40,3 @@ RUF065 Unnecessary `str()` conversion when formatting with `%s`
24 | 24 |
25 | # %s + repr() 25 | # %s + repr()
| |
RUF065 Unnecessary `repr()` conversion when formatting with `%r`
--> RUF065.py:34:18
|
33 | # %r + repr()
34 | info("Hello %r", repr("World!"))
| ^^^^^^^^^^^^^^
35 | log(logging.INFO, "Hello %r", repr("World!"))
|
RUF065 Unnecessary `repr()` conversion when formatting with `%r`
--> RUF065.py:35:31
|
33 | # %r + repr()
34 | info("Hello %r", repr("World!"))
35 | log(logging.INFO, "Hello %r", repr("World!"))
| ^^^^^^^^^^^^^^
36 |
37 | def str(s): return f"str = {s}"
|