-
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.
[pylint] - implement
global-at-module-level
(W0604
)
- Loading branch information
1 parent
a85ed30
commit 9d4ade9
Showing
8 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
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,7 @@ | ||
global price # W0604 | ||
|
||
price = 25 | ||
|
||
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 ast::Stmt; | ||
use ruff_python_ast::{self as ast}; | ||
|
||
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 `global` at the top level of a module. | ||
/// | ||
/// ## Why is this bad? | ||
/// `global` at the top level of a module 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().at_top_level() { | ||
return; | ||
} | ||
|
||
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
12 changes: 12 additions & 0 deletions
12
...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,12 @@ | ||
--- | ||
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 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.