-
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 [`PLR0914` - `too-many-locals`](https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/too-many-locals.html) See #970 ## Test Plan `cargo test`
- Loading branch information
1 parent
be8f8e6
commit 7c89492
Showing
10 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
crates/ruff_linter/resources/test/fixtures/pylint/too_many_locals.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,36 @@ | ||
def func() -> None: # OK | ||
# 15 is max default | ||
first = 1 | ||
second = 2 | ||
third = 3 | ||
fourth = 4 | ||
fifth = 5 | ||
sixth = 6 | ||
seventh = 7 | ||
eighth = 8 | ||
ninth = 9 | ||
tenth = 10 | ||
eleventh = 11 | ||
twelveth = 12 | ||
thirteenth = 13 | ||
fourteenth = 14 | ||
fifteenth = 15 | ||
|
||
|
||
def func() -> None: # PLR0914 | ||
first = 1 | ||
second = 2 | ||
third = 3 | ||
fourth = 4 | ||
fifth = 5 | ||
sixth = 6 | ||
seventh = 7 | ||
eighth = 8 | ||
ninth = 9 | ||
tenth = 10 | ||
eleventh = 11 | ||
twelfth = 12 | ||
thirteenth = 13 | ||
fourteenth = 14 | ||
fifteenth = 15 | ||
sixteenth = 16 |
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
59 changes: 59 additions & 0 deletions
59
crates/ruff_linter/src/rules/pylint/rules/too_many_locals.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,59 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::identifier::Identifier; | ||
use ruff_python_semantic::{Scope, ScopeKind}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for functions that include too many local variables. | ||
/// | ||
/// By default, this rule allows up to fifteen locals, as configured by the | ||
/// [`pylint.max-locals`] option. | ||
/// | ||
/// ## Why is this bad? | ||
/// Functions with many local variables are harder to understand and maintain. | ||
/// | ||
/// Consider refactoring functions with many local variables into smaller | ||
/// functions with fewer assignments. | ||
/// | ||
/// ## Options | ||
/// - `pylint.max-locals` | ||
#[violation] | ||
pub struct TooManyLocals { | ||
current_amount: usize, | ||
max_amount: usize, | ||
} | ||
|
||
impl Violation for TooManyLocals { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooManyLocals { | ||
current_amount, | ||
max_amount, | ||
} = self; | ||
format!("Too many local variables: ({current_amount}/{max_amount})") | ||
} | ||
} | ||
|
||
/// PLR0914 | ||
pub(crate) fn too_many_locals(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) { | ||
let num_locals = scope | ||
.binding_ids() | ||
.filter(|id| { | ||
let binding = checker.semantic().binding(*id); | ||
binding.kind.is_assignment() | ||
}) | ||
.count(); | ||
if num_locals > checker.settings.pylint.max_locals { | ||
if let ScopeKind::Function(func) = scope.kind { | ||
diagnostics.push(Diagnostic::new( | ||
TooManyLocals { | ||
current_amount: num_locals, | ||
max_amount: checker.settings.pylint.max_locals, | ||
}, | ||
func.identifier(), | ||
)); | ||
}; | ||
} | ||
} |
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
...linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__too_many_locals.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 | ||
--- | ||
too_many_locals.py:20:5: PLR0914 Too many local variables: (16/15) | ||
| | ||
20 | def func() -> None: # PLR0914 | ||
| ^^^^ PLR0914 | ||
21 | first = 1 | ||
22 | second = 2 | ||
| | ||
|
||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.