Skip to content
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 @@ -29,7 +29,7 @@

f"{1:z}" # [bad-format-character]

## False negatives
## Supporting concatenated strings

print(("%" "z") % 1)

Expand Down
13 changes: 6 additions & 7 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
Rule::PercentFormatPositionalCountMismatch,
Rule::PercentFormatStarRequiresSequence,
Rule::PercentFormatUnsupportedFormatCharacter,
Rule::BadStringFormatCharacter,
]) {
let location = expr.range();
match pyflakes::cformat::CFormatSummary::try_from(value.to_str()) {
Expand All @@ -1483,6 +1484,11 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
},
location,
);
// PLE1300
checker.report_diagnostic_if_enabled(
pylint::rules::BadStringFormatCharacter { format_char: c },
location,
);
}
Err(e) => {
// F501
Expand Down Expand Up @@ -1535,13 +1541,6 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
if checker.is_rule_enabled(Rule::PrintfStringFormatting) {
pyupgrade::rules::printf_string_formatting(checker, bin_op, format_string);
}
if checker.is_rule_enabled(Rule::BadStringFormatCharacter) {
pylint::rules::bad_string_format_character::percent(
checker,
expr,
format_string,
);
}
if checker.is_rule_enabled(Rule::BadStringFormatType) {
pylint::rules::bad_string_format_type(checker, bin_op, format_string);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use std::str::FromStr;

use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
use ruff_python_literal::{
cformat::{CFormatErrorType, CFormatString},
format::FormatPart,
format::FromTemplate,
format::{FormatSpec, FormatSpecError, FormatString},
};
use ruff_text_size::{Ranged, TextRange};
use ruff_text_size::TextRange;

use crate::Violation;
use crate::checkers::ast::Checker;
Expand All @@ -29,7 +25,7 @@ use crate::checkers::ast::Checker;
#[derive(ViolationMetadata)]
#[violation_metadata(stable_since = "v0.0.283")]
pub(crate) struct BadStringFormatCharacter {
format_char: char,
pub(crate) format_char: char,
}

impl Violation for BadStringFormatCharacter {
Expand Down Expand Up @@ -72,26 +68,3 @@ pub(crate) fn call(checker: &Checker, string: &str, range: TextRange) {
}
}
}

/// PLE1300
/// Ex) `"%z" % "1"`
pub(crate) fn percent(checker: &Checker, expr: &Expr, format_string: &ExprStringLiteral) {
for StringLiteral {
value: _,
node_index: _,
range,
flags,
} in &format_string.value
{
let string = checker.locator().slice(range);
let string = &string
[usize::from(flags.opener_len())..(string.len() - usize::from(flags.closer_len()))];

// Parse the format string (e.g. `"%s"`) into a list of `PercentFormat`.
if let Err(format_error) = CFormatString::from_str(string) {
if let CFormatErrorType::UnsupportedFormatChar(format_char) = format_error.typ {
checker.report_diagnostic(BadStringFormatCharacter { format_char }, expr.range());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ PLE1300 Unsupported format character 'y'
21 | "{0:.{foo}{bar}{foobar}y}".format(...) # OK (cannot validate after nested placeholders)
|

PLE1300 Unsupported format character 'z'
--> bad_string_format_character.py:34:7
|
32 | ## Supporting concatenated strings
33 |
34 | print(("%" "z") % 1)
| ^^^^^^^^^^^^^
35 |
36 | ## `%b` is only valid for bytes formatting.
|

PLE1300 Unsupported format character 'b'
--> bad_string_format_character.py:37:1
|
Expand Down
Loading