-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dda4ced
commit 88561c4
Showing
8 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/pylint/non_ascii_name.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ápple_count: int = 1 # C2401 | ||
ápple_count += 2 # C2401 | ||
ápple_count = 3 # C2401 | ||
|
||
# this rule only works on assignment! | ||
ápple_count == 3 # Ok | ||
|
||
# normal ascii | ||
apple_count = 4 # Ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
crates/ruff_linter/src/rules/pylint/rules/non_ascii_name.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use ruff_python_ast::{self as ast, Expr}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for the use of non-ASCII characters in symbol names. | ||
/// | ||
/// ## Why is this bad? | ||
/// Pylint discourages the use of non-ASCII characters in symbol names as | ||
/// they can cause confusion and compatibility issues. | ||
/// | ||
/// ## References | ||
/// - [PEP 672](https://peps.python.org/pep-0672/) | ||
#[violation] | ||
pub struct NonAsciiName; | ||
|
||
impl Violation for NonAsciiName { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Symbol name contains a non-ASCII character, consider renaming it.") | ||
} | ||
} | ||
|
||
/// PLC2401 | ||
pub(crate) fn non_ascii_name(checker: &mut Checker, target: &Expr) { | ||
let Expr::Name(ast::ExprName { id, .. }) = target else { | ||
return; | ||
}; | ||
|
||
if id.is_ascii() { | ||
return; | ||
} | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(NonAsciiName, target.range())); | ||
} |
30 changes: 30 additions & 0 deletions
30
.../rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2401_non_ascii_name.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
non_ascii_name.py:1:1: PLC2401 Symbol name contains a non-ASCII character, consider renaming it. | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
2 | ápple_count += 2 # C2401 | ||
3 | ápple_count = 3 # C2401 | ||
| | ||
|
||
non_ascii_name.py:2:1: PLC2401 Symbol name contains a non-ASCII character, consider renaming it. | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
2 | ápple_count += 2 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
3 | ápple_count = 3 # C2401 | ||
| | ||
|
||
non_ascii_name.py:3:1: PLC2401 Symbol name contains a non-ASCII character, consider renaming it. | ||
| | ||
1 | ápple_count: int = 1 # C2401 | ||
2 | ápple_count += 2 # C2401 | ||
3 | ápple_count = 3 # C2401 | ||
| ^^^^^^^^^^^ PLC2401 | ||
4 | | ||
5 | # this rule only works on assignment! | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.