[`pylint`] Add U+061C to `PLE2502` (#20106)

Resolves #20058
This commit is contained in:
Kot 2025-08-28 13:35:48 -07:00 committed by GitHub
parent 637a2b1170
commit b6522cb534
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 106 additions and 15 deletions

View File

@ -4,6 +4,9 @@ print("שלום‬")
# E2502 # E2502
example = "x" * 100 # "x" is assigned example = "x" * 100 # "x" is assigned
# E2502
another = "x؜" * 50 # "؜x" is assigned
# E2502 # E2502
if access_level != "none": # Check if admin ' and access_level != 'user if access_level != "none": # Check if admin ' and access_level != 'user
print("You are an admin.") print("You are an admin.")

View File

@ -255,3 +255,8 @@ pub(crate) const fn is_trailing_comma_type_params_enabled(settings: &LinterSetti
pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool { pub(crate) const fn is_maxsplit_without_separator_fix_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled() settings.preview.is_enabled()
} }
// https://github.com/astral-sh/ruff/pull/20106
pub(crate) const fn is_bidi_forbid_arabic_letter_mark_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}

View File

@ -252,6 +252,30 @@ mod tests {
Ok(()) Ok(())
} }
#[test_case(Rule::BidirectionalUnicode, Path::new("bidirectional_unicode.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
rule_code.noqa_code(),
path.to_string_lossy()
);
let diagnostics = test_path(
Path::new("pylint").join(path).as_path(),
&LinterSettings {
pylint: pylint::settings::Settings {
allow_dunder_method_names: FxHashSet::from_iter([
"__special_custom_magic__".to_string()
]),
..pylint::settings::Settings::default()
},
preview: PreviewMode::Enabled,
..LinterSettings::for_rule(rule_code)
},
)?;
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}
#[test] #[test]
fn continue_in_finally() -> Result<()> { fn continue_in_finally() -> Result<()> {
let diagnostics = test_path( let diagnostics = test_path(

View File

@ -1,7 +1,9 @@
use ruff_macros::{ViolationMetadata, derive_message_formats}; use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_source_file::Line; use ruff_source_file::Line;
use crate::{Violation, checkers::ast::LintContext}; use crate::{
Violation, checkers::ast::LintContext, preview::is_bidi_forbid_arabic_letter_mark_enabled,
};
const BIDI_UNICODE: [char; 10] = [ const BIDI_UNICODE: [char; 10] = [
'\u{202A}', //{LEFT-TO-RIGHT EMBEDDING} '\u{202A}', //{LEFT-TO-RIGHT EMBEDDING}
@ -60,7 +62,12 @@ impl Violation for BidirectionalUnicode {
/// PLE2502 /// PLE2502
pub(crate) fn bidirectional_unicode(line: &Line, context: &LintContext) { pub(crate) fn bidirectional_unicode(line: &Line, context: &LintContext) {
if line.contains(BIDI_UNICODE) { if line.contains(BIDI_UNICODE)
|| (is_bidi_forbid_arabic_letter_mark_enabled(context.settings())
&& line.contains(
'\u{061C}', //{ARABIC LETTER MARK}
))
{
context.report_diagnostic(BidirectionalUnicode, line.full_range()); context.report_diagnostic(BidirectionalUnicode, line.full_range());
} }
} }

View File

@ -22,21 +22,21 @@ PLE2502 Contains control characters that can permit obfuscated code
| |
PLE2502 Contains control characters that can permit obfuscated code PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:8:1 --> bidirectional_unicode.py:11:1
| |
7 | # E2502 10 | # E2502
8 | if access_level != "none": # Check if admin ' and access_level != 'user 11 | if access_level != "none": # Check if admin ' and access_level != 'user
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 | print("You are an admin.") 12 | print("You are an admin.")
| |
PLE2502 Contains control characters that can permit obfuscated code PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:14:1 --> bidirectional_unicode.py:17:1
| |
12 | # E2502 15 | # E2502
13 | def subtract_funds(account: str, amount: int): 16 | def subtract_funds(account: str, amount: int):
14 | """Subtract funds from bank account then """ 17 | """Subtract funds from bank account then """
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15 | return 18 | return
16 | bank[account] -= amount 19 | bank[account] -= amount
| |

View File

@ -0,0 +1,52 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:2:1
|
1 | # E2502
2 | print("שלום")
| ^^^^^^^^^^^^^
3 |
4 | # E2502
|
PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:5:1
|
4 | # E2502
5 | example = "x" * 100 # "x" is assigned
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 |
7 | # E2502
|
PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:8:1
|
7 | # E2502
8 | another = "x؜" * 50 # "؜x" is assigned
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9 |
10 | # E2502
|
PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:11:1
|
10 | # E2502
11 | if access_level != "none": # Check if admin ' and access_level != 'user
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12 | print("You are an admin.")
|
PLE2502 Contains control characters that can permit obfuscated code
--> bidirectional_unicode.py:17:1
|
15 | # E2502
16 | def subtract_funds(account: str, amount: int):
17 | """Subtract funds from bank account then """
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18 | return
19 | bank[account] -= amount
|