Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Dec 17, 2023
1 parent 2364cfb commit 19b31fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
def func():
# Ok
def func() -> None: # Ok
# 15 is max default
first = 1
second = 2
Expand All @@ -17,8 +16,7 @@ def func():
fourteenth = 14
fifteenth = 15

def func():
# PLR0914
def func() -> None: # PLR0914
first = 1
second = 2
third = 3
Expand Down
42 changes: 18 additions & 24 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::ops::Add;

use ruff_diagnostics::Diagnostic;
use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{Binding, BindingKind, ScopeKind};
use ruff_text_size::{Ranged, TextRange, TextSize};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::codes::Rule;
Expand Down Expand Up @@ -342,29 +341,24 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {

if checker.enabled(Rule::TooManyLocals) {
// PLR0914
let mut num_locals = 0;
let mut last_seen_range = TextRange::default();
for (_name, binding_id) in scope.bindings() {
let binding = checker.semantic.binding(binding_id);
if matches!(binding.kind, BindingKind::Assignment) {
num_locals += 1;

// use the beginning of the last seen assignment as the range
last_seen_range = TextRange::new(
binding.range().start(),
binding.range().start().add(TextSize::from(1)),
);
}
}
let num_locals = scope
.binding_ids()
.filter(|&id| {
let binding = checker.semantic.binding(id);
matches!(binding.kind, BindingKind::Assignment)
})
.count();

if num_locals > checker.settings.pylint.max_locals {
diagnostics.push(Diagnostic::new(
pylint::rules::TooManyLocals {
current_amount: num_locals,
max_amount: checker.settings.pylint.max_locals,
},
last_seen_range,
));
if let ScopeKind::Function(func) = scope.kind {
diagnostics.push(Diagnostic::new(
pylint::rules::TooManyLocals {
current_amount: num_locals,
max_amount: checker.settings.pylint.max_locals,
},
func.identifier(),
));
};
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
too_many_locals.py:36:5: PLR0914 Too many local variables: (16/15)
too_many_locals.py:19:5: PLR0914 Too many local variables: (16/15)
|
34 | thirteenth = 13
35 | fourteenth = 14
36 | fifteenth = 15
| ^ PLR0914
37 | sixteenth = 16
17 | fifteenth = 15
18 |
19 | def func() -> None: # PLR0914
| ^^^^ PLR0914
20 | first = 1
21 | second = 2
|


0 comments on commit 19b31fc

Please sign in to comment.