-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements [`global-at-module-level`/`W0604`](https://pylint.pycqa.org/en/latest/user_guide/messages/warning/global-at-module-level.html) See #970 ## Test Plan `cargo test` and manually
- Loading branch information
1 parent
a85ed30
commit 693f957
Showing
8 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff_linter/resources/test/fixtures/pylint/global_at_module_level.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,10 @@ | ||
global price # W0604 | ||
|
||
price = 25 | ||
|
||
if True: | ||
global X # W0604 | ||
|
||
def no_error(): | ||
global price | ||
price = 30 |
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
34 changes: 34 additions & 0 deletions
34
crates/ruff_linter/src/rules/pylint/rules/global_at_module_level.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,34 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Stmt; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of the `global` keyword at the module level. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `global` keyword is used within functions to indicate that a name | ||
/// refers to a global variable, rather than a local variable. | ||
/// | ||
/// At the module level, all names are global by default, so the `global` | ||
/// keyword is redundant. | ||
#[violation] | ||
pub struct GlobalAtModuleLevel; | ||
|
||
impl Violation for GlobalAtModuleLevel { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`global` at module level is redundant") | ||
} | ||
} | ||
|
||
/// PLW0604 | ||
pub(crate) fn global_at_module_level(checker: &mut Checker, stmt: &Stmt) { | ||
if checker.semantic().current_scope().kind.is_module() { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(GlobalAtModuleLevel, stmt.range())); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...ylint/snapshots/ruff_linter__rules__pylint__tests__PLW0604_global_at_module_level.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,21 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
global_at_module_level.py:1:1: PLW0604 `global` at module level is redundant | ||
| | ||
1 | global price # W0604 | ||
| ^^^^^^^^^^^^ PLW0604 | ||
2 | | ||
3 | price = 25 | ||
| | ||
|
||
global_at_module_level.py:6:5: PLW0604 `global` at module level is redundant | ||
| | ||
5 | if True: | ||
6 | global X # W0604 | ||
| ^^^^^^^^ PLW0604 | ||
7 | | ||
8 | def no_error(): | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.