From 37c856cbc13ab2a1a1279b5bef7e9ae17177a6b0 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 25 Jun 2024 10:09:56 +0530 Subject: [PATCH] Redirect `F509` to `PLE1300` --- .../show_settings__display_default_settings.snap | 2 -- .../src/checkers/ast/analyze/expression.rs | 11 ++--------- crates/ruff_linter/src/codes.rs | 2 +- crates/ruff_linter/src/rule_redirects.rs | 1 + crates/ruff_linter/src/rules/pyflakes/mod.rs | 1 - .../src/rules/pyflakes/rules/strings.rs | 7 +++++++ ...ter__rules__pyflakes__tests__F509_F50x.py.snap | 14 -------------- .../pylint/rules/bad_string_format_character.rs | 15 +++++++++++++-- ruff.schema.json | 1 - 9 files changed, 24 insertions(+), 30 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F509_F50x.py.snap diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index fd4370ae1d..6b5a67c62d 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -102,7 +102,6 @@ linter.rules.enabled = [ percent-format-mixed-positional-and-named (F506), percent-format-positional-count-mismatch (F507), percent-format-star-requires-sequence (F508), - percent-format-unsupported-format-character (F509), string-dot-format-invalid-format (F521), string-dot-format-extra-named-arguments (F522), string-dot-format-extra-positional-arguments (F523), @@ -164,7 +163,6 @@ linter.rules.should_fix = [ percent-format-mixed-positional-and-named (F506), percent-format-positional-count-mismatch (F507), percent-format-star-requires-sequence (F508), - percent-format-unsupported-format-character (F509), string-dot-format-invalid-format (F521), string-dot-format-extra-named-arguments (F522), string-dot-format-extra-positional-arguments (F523), diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 9c12ac0333..b54937273d 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -1098,17 +1098,10 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { let location = expr.range(); match pyflakes::cformat::CFormatSummary::try_from(value.to_str()) { Err(CFormatError { - typ: CFormatErrorType::UnsupportedFormatChar(c), + typ: CFormatErrorType::UnsupportedFormatChar(_), .. }) => { - if checker.enabled(Rule::PercentFormatUnsupportedFormatCharacter) { - checker.diagnostics.push(Diagnostic::new( - pyflakes::rules::PercentFormatUnsupportedFormatCharacter { - char: c, - }, - location, - )); - } + // Unsupported format character violation is raised by `PLE1300` } Err(e) => { if checker.enabled(Rule::PercentFormatInvalidFormat) { diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index 5cfea34a37..c035701144 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -152,7 +152,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Pyflakes, "506") => (RuleGroup::Stable, rules::pyflakes::rules::PercentFormatMixedPositionalAndNamed), (Pyflakes, "507") => (RuleGroup::Stable, rules::pyflakes::rules::PercentFormatPositionalCountMismatch), (Pyflakes, "508") => (RuleGroup::Stable, rules::pyflakes::rules::PercentFormatStarRequiresSequence), - (Pyflakes, "509") => (RuleGroup::Stable, rules::pyflakes::rules::PercentFormatUnsupportedFormatCharacter), + (Pyflakes, "509") => (RuleGroup::Removed, rules::pyflakes::rules::PercentFormatUnsupportedFormatCharacter), (Pyflakes, "521") => (RuleGroup::Stable, rules::pyflakes::rules::StringDotFormatInvalidFormat), (Pyflakes, "522") => (RuleGroup::Stable, rules::pyflakes::rules::StringDotFormatExtraNamedArguments), (Pyflakes, "523") => (RuleGroup::Stable, rules::pyflakes::rules::StringDotFormatExtraPositionalArguments), diff --git a/crates/ruff_linter/src/rule_redirects.rs b/crates/ruff_linter/src/rule_redirects.rs index 85c120e2d3..f8e6c247f2 100644 --- a/crates/ruff_linter/src/rule_redirects.rs +++ b/crates/ruff_linter/src/rule_redirects.rs @@ -103,6 +103,7 @@ static REDIRECTS: Lazy> = Lazy::new(|| { ("TRY200", "B904"), ("PGH001", "S307"), ("PGH002", "G010"), + ("F509", "PLE1300"), // Test redirect by exact code #[cfg(any(feature = "test-rules", test))] ("RUF940", "RUF950"), diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index f2b1e6d114..b35fe4e2a5 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -75,7 +75,6 @@ mod tests { #[test_case(Rule::PercentFormatMixedPositionalAndNamed, Path::new("F50x.py"))] #[test_case(Rule::PercentFormatPositionalCountMismatch, Path::new("F50x.py"))] #[test_case(Rule::PercentFormatStarRequiresSequence, Path::new("F50x.py"))] - #[test_case(Rule::PercentFormatUnsupportedFormatCharacter, Path::new("F50x.py"))] #[test_case(Rule::StringDotFormatInvalidFormat, Path::new("F521.py"))] #[test_case(Rule::StringDotFormatExtraNamedArguments, Path::new("F522.py"))] #[test_case(Rule::StringDotFormatExtraPositionalArguments, Path::new("F523.py"))] diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs index d61c501bde..4839e31e2c 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/strings.rs @@ -297,6 +297,10 @@ impl Violation for PercentFormatStarRequiresSequence { } } +/// ## Removed +/// +/// This rule is a subset of [PLE1300] which should be used instead. +/// /// ## What it does /// Checks for `printf`-style format strings with invalid format characters. /// @@ -317,11 +321,14 @@ impl Violation for PercentFormatStarRequiresSequence { /// /// ## References /// - [Python documentation: `printf`-style String Formatting](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) +/// +/// [PLE1300]: https://docs.astral.sh/ruff/rules/bad-string-format-character/ #[violation] pub struct PercentFormatUnsupportedFormatCharacter { pub(crate) char: char, } +/// F509 impl Violation for PercentFormatUnsupportedFormatCharacter { #[derive_message_formats] fn message(&self) -> String { diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F509_F50x.py.snap b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F509_F50x.py.snap deleted file mode 100644 index e1eeb3f4c2..0000000000 --- a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__F509_F50x.py.snap +++ /dev/null @@ -1,14 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/pyflakes/mod.rs ---- -F50x.py:4:1: F509 `%`-format string has unsupported format character `j` - | -2 | '%s %(foo)s' % {'foo': 'bar'} # F506 -3 | '%(foo)s %s' % {'foo': 'bar'} # F506 -4 | '%j' % (1,) # F509 - | ^^^^^^^^^^^ F509 -5 | '%s %s' % (1,) # F507 -6 | '%s %s' % (1, 2, 3) # F507 - | - - diff --git a/crates/ruff_linter/src/rules/pylint/rules/bad_string_format_character.rs b/crates/ruff_linter/src/rules/pylint/rules/bad_string_format_character.rs index 334f8d2330..b7511fc393 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/bad_string_format_character.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/bad_string_format_character.rs @@ -22,10 +22,21 @@ use crate::checkers::ast::Checker; /// ## Example /// ```python /// # `z` is not a valid format type. -/// print("%z" % "1") +/// print("%z" % 1) /// -/// print("{:z}".format("1")) +/// print("{:z}".format(1)) /// ``` +/// +/// Use instead: +/// ```python +/// print("%d" % 1) +/// +/// print("{:d}".format(1)) +/// ``` +/// +/// ## References +/// - [Python documentation: `printf`-style String Formatting](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) +/// - [Python documentation: `str.format`](https://docs.python.org/3/library/stdtypes.html#str.format) #[violation] pub struct BadStringFormatCharacter { format_char: char, diff --git a/ruff.schema.json b/ruff.schema.json index 5d390e2408..85dd546e15 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -2988,7 +2988,6 @@ "F506", "F507", "F508", - "F509", "F52", "F521", "F522",