mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[pylint
]: bidirectional-unicode (#2589)
This commit is contained in:
parent
7686179318
commit
7647cafe12
10 changed files with 195 additions and 0 deletions
24
crates/ruff/resources/test/fixtures/pylint/bidirectional_unicode.py
vendored
Normal file
24
crates/ruff/resources/test/fixtures/pylint/bidirectional_unicode.py
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
# E2502
|
||||
print("\u202B\u202E\u05e9\u05DC\u05D5\u05DD\u202C")
|
||||
|
||||
# E2502
|
||||
print("שלום")
|
||||
|
||||
# E2502
|
||||
example = "x" * 100 # "x" is assigned
|
||||
|
||||
# E2502
|
||||
if access_level != "none": # Check if admin ' and access_level != 'user
|
||||
print("You are an admin.")
|
||||
|
||||
|
||||
# E2502
|
||||
def subtract_funds(account: str, amount: int):
|
||||
"""Subtract funds from bank account then """
|
||||
return
|
||||
bank[account] -= amount
|
||||
return
|
||||
|
||||
|
||||
# OK
|
||||
print("Hello World")
|
|
@ -3107,6 +3107,9 @@ where
|
|||
if self.settings.rules.enabled(&Rule::RewriteUnicodeLiteral) {
|
||||
pyupgrade::rules::rewrite_unicode_literal(self, expr, kind.as_deref());
|
||||
}
|
||||
if self.settings.rules.enabled(&Rule::BidirectionalUnicode) {
|
||||
pylint::rules::bidirectional_unicode(self, expr, value);
|
||||
}
|
||||
}
|
||||
ExprKind::Lambda { args, body, .. } => {
|
||||
if self.settings.rules.enabled(&Rule::PreferListBuiltin) {
|
||||
|
|
|
@ -107,6 +107,7 @@ ruff_macros::define_rule_mapping!(
|
|||
// pylint
|
||||
PLE0604 => rules::pylint::rules::InvalidAllObject,
|
||||
PLE0605 => rules::pylint::rules::InvalidAllFormat,
|
||||
PLE2502 => rules::pylint::rules::BidirectionalUnicode,
|
||||
PLE1310 => rules::pylint::rules::BadStrStripCall,
|
||||
PLC0414 => rules::pylint::rules::UselessImportAlias,
|
||||
PLC3002 => rules::pylint::rules::UnnecessaryDirectLambdaCall,
|
||||
|
|
|
@ -40,6 +40,7 @@ mod tests {
|
|||
#[test_case(Rule::TooManyArguments, Path::new("too_many_arguments.py"); "PLR0913")]
|
||||
#[test_case(Rule::TooManyBranches, Path::new("too_many_branches.py"); "PLR0912")]
|
||||
#[test_case(Rule::TooManyStatements, Path::new("too_many_statements.py"); "PLR0915")]
|
||||
#[test_case(Rule::BidirectionalUnicode, Path::new("bidirectional_unicode.py"); "PLE2502")]
|
||||
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"); "PLE01310")]
|
||||
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy());
|
||||
|
|
49
crates/ruff/src/rules/pylint/rules/bidirectional_unicode.rs
Normal file
49
crates/ruff/src/rules/pylint/rules/bidirectional_unicode.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
use rustpython_ast::Expr;
|
||||
|
||||
use ruff_macros::derive_message_formats;
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::define_violation;
|
||||
use crate::registry::Diagnostic;
|
||||
use crate::violation::Violation;
|
||||
|
||||
const BIDI_UNICODE: [char; 10] = [
|
||||
'\u{202A}', //{LEFT-TO-RIGHT EMBEDDING}
|
||||
'\u{202B}', //{RIGHT-TO-LEFT EMBEDDING}
|
||||
'\u{202C}', //{POP DIRECTIONAL FORMATTING}
|
||||
'\u{202D}', //{LEFT-TO-RIGHT OVERRIDE}
|
||||
'\u{202E}', //{RIGHT-TO-LEFT OVERRIDE}
|
||||
'\u{2066}', //{LEFT-TO-RIGHT ISOLATE}
|
||||
'\u{2067}', //{RIGHT-TO-LEFT ISOLATE}
|
||||
'\u{2068}', //{FIRST STRONG ISOLATE}
|
||||
'\u{2069}', //{POP DIRECTIONAL ISOLATE}
|
||||
// The following was part of PEP 672:
|
||||
// https://www.python.org/dev/peps/pep-0672/
|
||||
// so the list above might not be complete
|
||||
'\u{200F}', //{RIGHT-TO-LEFT MARK}
|
||||
// We don't use
|
||||
// "\u200E" # \n{LEFT-TO-RIGHT MARK}
|
||||
// as this is the default for latin files and can't be used
|
||||
// to hide code
|
||||
];
|
||||
|
||||
define_violation!(
|
||||
pub struct BidirectionalUnicode;
|
||||
);
|
||||
impl Violation for BidirectionalUnicode {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("Avoid using bidirectional unicode")
|
||||
}
|
||||
}
|
||||
|
||||
/// PLE2502
|
||||
pub fn bidirectional_unicode(checker: &mut Checker, expr: &Expr, value: &str) {
|
||||
if value.contains(BIDI_UNICODE) {
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
BidirectionalUnicode,
|
||||
Range::from_located(expr),
|
||||
));
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
pub use await_outside_async::{await_outside_async, AwaitOutsideAsync};
|
||||
pub use bad_str_strip_call::{bad_str_strip_call, BadStrStripCall};
|
||||
pub use bidirectional_unicode::{bidirectional_unicode, BidirectionalUnicode};
|
||||
pub use comparison_of_constant::{comparison_of_constant, ComparisonOfConstant};
|
||||
pub use consider_using_sys_exit::{consider_using_sys_exit, ConsiderUsingSysExit};
|
||||
pub use global_variable_not_assigned::GlobalVariableNotAssigned;
|
||||
|
@ -25,6 +26,7 @@ pub use useless_import_alias::{useless_import_alias, UselessImportAlias};
|
|||
|
||||
mod await_outside_async;
|
||||
mod bad_str_strip_call;
|
||||
mod bidirectional_unicode;
|
||||
mod comparison_of_constant;
|
||||
mod consider_using_sys_exit;
|
||||
mod global_variable_not_assigned;
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
---
|
||||
source: src/rules/pylint/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 2
|
||||
column: 6
|
||||
end_location:
|
||||
row: 2
|
||||
column: 50
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 3
|
||||
column: 6
|
||||
end_location:
|
||||
row: 3
|
||||
column: 13
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 5
|
||||
column: 10
|
||||
end_location:
|
||||
row: 5
|
||||
column: 14
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 7
|
||||
column: 19
|
||||
end_location:
|
||||
row: 7
|
||||
column: 27
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 11
|
||||
column: 4
|
||||
end_location:
|
||||
row: 11
|
||||
column: 49
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
---
|
||||
source: crates/ruff/src/rules/pylint/mod.rs
|
||||
expression: diagnostics
|
||||
---
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 2
|
||||
column: 6
|
||||
end_location:
|
||||
row: 2
|
||||
column: 50
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 5
|
||||
column: 6
|
||||
end_location:
|
||||
row: 5
|
||||
column: 13
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 8
|
||||
column: 10
|
||||
end_location:
|
||||
row: 8
|
||||
column: 14
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 11
|
||||
column: 19
|
||||
end_location:
|
||||
row: 11
|
||||
column: 27
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
BidirectionalUnicode: ~
|
||||
location:
|
||||
row: 17
|
||||
column: 4
|
||||
end_location:
|
||||
row: 17
|
||||
column: 49
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue