mirror of https://github.com/astral-sh/ruff
Avoid some allocations (#179)
This commit is contained in:
parent
2ba767957d
commit
53a7758248
|
|
@ -156,9 +156,7 @@ pub fn check_useless_object_inheritance(
|
||||||
CheckKind::UselessObjectInheritance(name.to_string()),
|
CheckKind::UselessObjectInheritance(name.to_string()),
|
||||||
expr.location,
|
expr.location,
|
||||||
);
|
);
|
||||||
if matches!(autofix, fixer::Mode::Generate)
|
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
||||||
|| matches!(autofix, fixer::Mode::Apply)
|
|
||||||
{
|
|
||||||
if let Some(fix) = fixes::remove_class_def_base(
|
if let Some(fix) = fixes::remove_class_def_base(
|
||||||
locator,
|
locator,
|
||||||
&stmt.location,
|
&stmt.location,
|
||||||
|
|
@ -254,9 +252,7 @@ pub fn check_assert_equals(expr: &Expr, autofix: &fixer::Mode) -> Option<Check>
|
||||||
if let ExprKind::Name { id, .. } = &value.node {
|
if let ExprKind::Name { id, .. } = &value.node {
|
||||||
if id == "self" {
|
if id == "self" {
|
||||||
let mut check = Check::new(CheckKind::NoAssertEquals, expr.location);
|
let mut check = Check::new(CheckKind::NoAssertEquals, expr.location);
|
||||||
if matches!(autofix, fixer::Mode::Generate)
|
if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
||||||
|| matches!(autofix, fixer::Mode::Apply)
|
|
||||||
{
|
|
||||||
check.amend(Fix {
|
check.amend(Fix {
|
||||||
content: "assertEqual".to_string(),
|
content: "assertEqual".to_string(),
|
||||||
start: Location::new(expr.location.row(), expr.location.column() + 1),
|
start: Location::new(expr.location.row(), expr.location.column() + 1),
|
||||||
|
|
@ -315,7 +311,7 @@ pub fn check_repeated_keys(
|
||||||
(Some(DictionaryKey::Variable(v1)), Some(DictionaryKey::Variable(v2))) => {
|
(Some(DictionaryKey::Variable(v1)), Some(DictionaryKey::Variable(v2))) => {
|
||||||
if check_repeated_variables && v1 == v2 {
|
if check_repeated_variables && v1 == v2 {
|
||||||
checks.push(Check::new(
|
checks.push(Check::new(
|
||||||
CheckKind::MultiValueRepeatedKeyVariable(v2.to_string()),
|
CheckKind::MultiValueRepeatedKeyVariable((*v2).to_string()),
|
||||||
k2.location,
|
k2.location,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec<String> {
|
||||||
if let StmtKind::AugAssign { .. } = &stmt.node {
|
if let StmtKind::AugAssign { .. } = &stmt.node {
|
||||||
if let Some(binding) = scope.values.get("__all__") {
|
if let Some(binding) = scope.values.get("__all__") {
|
||||||
if let BindingKind::Export(existing) = &binding.kind {
|
if let BindingKind::Export(existing) = &binding.kind {
|
||||||
names.extend(existing.clone());
|
names.extend_from_slice(existing);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,9 +70,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec<String> {
|
||||||
pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
|
pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
|
||||||
for index in parent_stack.iter().rev() {
|
for index in parent_stack.iter().rev() {
|
||||||
let parent = parents[*index];
|
let parent = parents[*index];
|
||||||
if matches!(parent.node, StmtKind::If { .. })
|
if matches!(parent.node, StmtKind::If { .. } | StmtKind::While { .. }) {
|
||||||
|| matches!(parent.node, StmtKind::While { .. })
|
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if let StmtKind::Expr { value } = &parent.node {
|
if let StmtKind::Expr { value } = &parent.node {
|
||||||
|
|
@ -89,10 +87,10 @@ pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool
|
||||||
pub fn in_nested_block(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
|
pub fn in_nested_block(parent_stack: &[usize], parents: &[&Stmt]) -> bool {
|
||||||
for index in parent_stack.iter().rev() {
|
for index in parent_stack.iter().rev() {
|
||||||
let parent = parents[*index];
|
let parent = parents[*index];
|
||||||
if matches!(parent.node, StmtKind::Try { .. })
|
if matches!(
|
||||||
|| matches!(parent.node, StmtKind::If { .. })
|
parent.node,
|
||||||
|| matches!(parent.node, StmtKind::With { .. })
|
StmtKind::Try { .. } | StmtKind::If { .. } | StmtKind::With { .. }
|
||||||
{
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -472,7 +472,7 @@ where
|
||||||
let binding = Binding {
|
let binding = Binding {
|
||||||
kind: BindingKind::Importation(match module {
|
kind: BindingKind::Importation(match module {
|
||||||
None => name.clone(),
|
None => name.clone(),
|
||||||
Some(parent) => format!("{}.{}", parent, name.clone()),
|
Some(parent) => format!("{}.{}", parent, name),
|
||||||
}),
|
}),
|
||||||
used: None,
|
used: None,
|
||||||
location: stmt.location,
|
location: stmt.location,
|
||||||
|
|
@ -645,8 +645,7 @@ where
|
||||||
.settings
|
.settings
|
||||||
.select
|
.select
|
||||||
.contains(CheckKind::YieldOutsideFunction.code())
|
.contains(CheckKind::YieldOutsideFunction.code())
|
||||||
&& matches!(scope.kind, ScopeKind::Class)
|
&& matches!(scope.kind, ScopeKind::Class | ScopeKind::Module)
|
||||||
|| matches!(scope.kind, ScopeKind::Module)
|
|
||||||
{
|
{
|
||||||
self.checks
|
self.checks
|
||||||
.push(Check::new(CheckKind::YieldOutsideFunction, expr.location));
|
.push(Check::new(CheckKind::YieldOutsideFunction, expr.location));
|
||||||
|
|
@ -998,7 +997,7 @@ impl<'a> Checker<'a> {
|
||||||
|
|
||||||
for builtin in BUILTINS {
|
for builtin in BUILTINS {
|
||||||
scope.values.insert(
|
scope.values.insert(
|
||||||
builtin.to_string(),
|
(*builtin).to_string(),
|
||||||
Binding {
|
Binding {
|
||||||
kind: BindingKind::Builtin,
|
kind: BindingKind::Builtin,
|
||||||
location: Default::default(),
|
location: Default::default(),
|
||||||
|
|
@ -1008,7 +1007,7 @@ impl<'a> Checker<'a> {
|
||||||
}
|
}
|
||||||
for builtin in MAGIC_GLOBALS {
|
for builtin in MAGIC_GLOBALS {
|
||||||
scope.values.insert(
|
scope.values.insert(
|
||||||
builtin.to_string(),
|
(*builtin).to_string(),
|
||||||
Binding {
|
Binding {
|
||||||
kind: BindingKind::Builtin,
|
kind: BindingKind::Builtin,
|
||||||
location: Default::default(),
|
location: Default::default(),
|
||||||
|
|
@ -1077,9 +1076,7 @@ impl<'a> Checker<'a> {
|
||||||
&& !current.values.contains_key(id)
|
&& !current.values.contains_key(id)
|
||||||
{
|
{
|
||||||
for scope in self.scopes.iter().rev().skip(1) {
|
for scope in self.scopes.iter().rev().skip(1) {
|
||||||
if matches!(scope.kind, ScopeKind::Function)
|
if matches!(scope.kind, ScopeKind::Function | ScopeKind::Module) {
|
||||||
|| matches!(scope.kind, ScopeKind::Module)
|
|
||||||
{
|
|
||||||
let used = scope
|
let used = scope
|
||||||
.values
|
.values
|
||||||
.get(id)
|
.get(id)
|
||||||
|
|
@ -1110,9 +1107,10 @@ impl<'a> Checker<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(charlie): Include comprehensions here.
|
// TODO(charlie): Include comprehensions here.
|
||||||
if matches!(parent.node, StmtKind::For { .. })
|
if matches!(
|
||||||
|| matches!(parent.node, StmtKind::AsyncFor { .. })
|
parent.node,
|
||||||
|| operations::is_unpacking_assignment(parent)
|
StmtKind::For { .. } | StmtKind::AsyncFor { .. }
|
||||||
|
) || operations::is_unpacking_assignment(parent)
|
||||||
{
|
{
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
id.to_string(),
|
id.to_string(),
|
||||||
|
|
@ -1127,9 +1125,12 @@ impl<'a> Checker<'a> {
|
||||||
|
|
||||||
if id == "__all__"
|
if id == "__all__"
|
||||||
&& matches!(current.kind, ScopeKind::Module)
|
&& matches!(current.kind, ScopeKind::Module)
|
||||||
&& (matches!(parent.node, StmtKind::Assign { .. })
|
&& matches!(
|
||||||
|| matches!(parent.node, StmtKind::AugAssign { .. })
|
parent.node,
|
||||||
|| matches!(parent.node, StmtKind::AnnAssign { .. }))
|
StmtKind::Assign { .. }
|
||||||
|
| StmtKind::AugAssign { .. }
|
||||||
|
| StmtKind::AnnAssign { .. }
|
||||||
|
)
|
||||||
{
|
{
|
||||||
self.add_binding(
|
self.add_binding(
|
||||||
id.to_string(),
|
id.to_string(),
|
||||||
|
|
@ -1250,7 +1251,7 @@ impl<'a> Checker<'a> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for index in self.dead_scopes.clone() {
|
for index in self.dead_scopes.iter().copied() {
|
||||||
let scope = &self.scopes[index];
|
let scope = &self.scopes[index];
|
||||||
|
|
||||||
let all_binding = scope.values.get("__all__");
|
let all_binding = scope.values.get("__all__");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue