From 53a7758248ecd671b9de434a188be580ebdbd53b Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Tue, 13 Sep 2022 16:07:22 +0200 Subject: [PATCH] Avoid some allocations (#179) --- src/ast/checks.rs | 10 +++------- src/ast/operations.rs | 14 ++++++-------- src/check_ast.rs | 31 ++++++++++++++++--------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/ast/checks.rs b/src/ast/checks.rs index e393abf932..6fe2beea9a 100644 --- a/src/ast/checks.rs +++ b/src/ast/checks.rs @@ -156,9 +156,7 @@ pub fn check_useless_object_inheritance( CheckKind::UselessObjectInheritance(name.to_string()), expr.location, ); - if matches!(autofix, fixer::Mode::Generate) - || matches!(autofix, fixer::Mode::Apply) - { + if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) { if let Some(fix) = fixes::remove_class_def_base( locator, &stmt.location, @@ -254,9 +252,7 @@ pub fn check_assert_equals(expr: &Expr, autofix: &fixer::Mode) -> Option if let ExprKind::Name { id, .. } = &value.node { if id == "self" { let mut check = Check::new(CheckKind::NoAssertEquals, expr.location); - if matches!(autofix, fixer::Mode::Generate) - || matches!(autofix, fixer::Mode::Apply) - { + if matches!(autofix, fixer::Mode::Generate | fixer::Mode::Apply) { check.amend(Fix { content: "assertEqual".to_string(), 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))) => { if check_repeated_variables && v1 == v2 { checks.push(Check::new( - CheckKind::MultiValueRepeatedKeyVariable(v2.to_string()), + CheckKind::MultiValueRepeatedKeyVariable((*v2).to_string()), k2.location, )) } diff --git a/src/ast/operations.rs b/src/ast/operations.rs index 1801c899f6..69d532faf2 100644 --- a/src/ast/operations.rs +++ b/src/ast/operations.rs @@ -22,7 +22,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec { if let StmtKind::AugAssign { .. } = &stmt.node { if let Some(binding) = scope.values.get("__all__") { 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 { pub fn on_conditional_branch(parent_stack: &[usize], parents: &[&Stmt]) -> bool { for index in parent_stack.iter().rev() { let parent = parents[*index]; - if matches!(parent.node, StmtKind::If { .. }) - || matches!(parent.node, StmtKind::While { .. }) - { + if matches!(parent.node, StmtKind::If { .. } | StmtKind::While { .. }) { return true; } 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 { for index in parent_stack.iter().rev() { let parent = parents[*index]; - if matches!(parent.node, StmtKind::Try { .. }) - || matches!(parent.node, StmtKind::If { .. }) - || matches!(parent.node, StmtKind::With { .. }) - { + if matches!( + parent.node, + StmtKind::Try { .. } | StmtKind::If { .. } | StmtKind::With { .. } + ) { return true; } } diff --git a/src/check_ast.rs b/src/check_ast.rs index fc0d59c10d..d905e2cc18 100644 --- a/src/check_ast.rs +++ b/src/check_ast.rs @@ -472,7 +472,7 @@ where let binding = Binding { kind: BindingKind::Importation(match module { None => name.clone(), - Some(parent) => format!("{}.{}", parent, name.clone()), + Some(parent) => format!("{}.{}", parent, name), }), used: None, location: stmt.location, @@ -645,8 +645,7 @@ where .settings .select .contains(CheckKind::YieldOutsideFunction.code()) - && matches!(scope.kind, ScopeKind::Class) - || matches!(scope.kind, ScopeKind::Module) + && matches!(scope.kind, ScopeKind::Class | ScopeKind::Module) { self.checks .push(Check::new(CheckKind::YieldOutsideFunction, expr.location)); @@ -998,7 +997,7 @@ impl<'a> Checker<'a> { for builtin in BUILTINS { scope.values.insert( - builtin.to_string(), + (*builtin).to_string(), Binding { kind: BindingKind::Builtin, location: Default::default(), @@ -1008,7 +1007,7 @@ impl<'a> Checker<'a> { } for builtin in MAGIC_GLOBALS { scope.values.insert( - builtin.to_string(), + (*builtin).to_string(), Binding { kind: BindingKind::Builtin, location: Default::default(), @@ -1077,9 +1076,7 @@ impl<'a> Checker<'a> { && !current.values.contains_key(id) { for scope in self.scopes.iter().rev().skip(1) { - if matches!(scope.kind, ScopeKind::Function) - || matches!(scope.kind, ScopeKind::Module) - { + if matches!(scope.kind, ScopeKind::Function | ScopeKind::Module) { let used = scope .values .get(id) @@ -1110,9 +1107,10 @@ impl<'a> Checker<'a> { } // TODO(charlie): Include comprehensions here. - if matches!(parent.node, StmtKind::For { .. }) - || matches!(parent.node, StmtKind::AsyncFor { .. }) - || operations::is_unpacking_assignment(parent) + if matches!( + parent.node, + StmtKind::For { .. } | StmtKind::AsyncFor { .. } + ) || operations::is_unpacking_assignment(parent) { self.add_binding( id.to_string(), @@ -1127,9 +1125,12 @@ impl<'a> Checker<'a> { if id == "__all__" && matches!(current.kind, ScopeKind::Module) - && (matches!(parent.node, StmtKind::Assign { .. }) - || matches!(parent.node, StmtKind::AugAssign { .. }) - || matches!(parent.node, StmtKind::AnnAssign { .. })) + && matches!( + parent.node, + StmtKind::Assign { .. } + | StmtKind::AugAssign { .. } + | StmtKind::AnnAssign { .. } + ) { self.add_binding( id.to_string(), @@ -1250,7 +1251,7 @@ impl<'a> Checker<'a> { return; } - for index in self.dead_scopes.clone() { + for index in self.dead_scopes.iter().copied() { let scope = &self.scopes[index]; let all_binding = scope.values.get("__all__");