Avoid some allocations (#179)

This commit is contained in:
Dmitry Dygalo 2022-09-13 16:07:22 +02:00 committed by GitHub
parent 2ba767957d
commit 53a7758248
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 30 deletions

View file

@ -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<Check>
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,
))
}

View file

@ -22,7 +22,7 @@ pub fn extract_all_names(stmt: &Stmt, scope: &Scope) -> Vec<String> {
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<String> {
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;
}
}

View file

@ -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__");