Skip to content

Commit

Permalink
Pre-init hash map
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 12, 2023
1 parent 9e5e578 commit 829b687
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
abc(**{"for": 3})
foo(**{},)

# Duplicated key names wont be fixed to avoid syntax error.
# Duplicated key names won't be fixed, to avoid syntax errors.
abc(**{'a': b}, **{'a': c}) # PIE804
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashSet;
use std::hash::BuildHasherDefault;

use itertools::Itertools;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_python_ast::{self as ast, Expr};
use rustc_hash::FxHashSet;

use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_text_size::Ranged;

use ruff_python_ast::{self as ast, Expr};
use ruff_python_stdlib::identifiers::is_identifier;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::fix::edits::{remove_argument, Parentheses};
Expand Down Expand Up @@ -69,16 +69,19 @@ impl AlwaysFixableViolation for UnnecessaryDictKwargs {

/// PIE804
pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCall) {
let mut all_kwargs = HashSet::new();
let mut seen = FxHashSet::with_capacity_and_hasher(
call.arguments.keywords.len(),
BuildHasherDefault::default(),
);

for kw in &call.arguments.keywords {
for keyword in &call.arguments.keywords {
// keyword is a spread operator (indicated by None)
if let Some(name) = &kw.arg {
all_kwargs.insert(name.as_str());
if let Some(name) = &keyword.arg {
seen.insert(name.as_str());
continue;
}

let Expr::Dict(ast::ExprDict { keys, values, .. }) = &kw.value else {
let Expr::Dict(ast::ExprDict { keys, values, .. }) = &keyword.value else {
continue;
};

Expand All @@ -88,7 +91,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal

diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
format!("**{}", checker.locator().slice(values[0].range())),
kw.range(),
keyword.range(),
)));

checker.diagnostics.push(diagnostic);
Expand All @@ -100,7 +103,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
let kwargs = keys
.iter()
.filter_map(|key| key.as_ref().and_then(as_kwarg))
.filter(|name| all_kwargs.insert(name))
.filter(|name| seen.insert(name))
.collect::<Vec<_>>();
if kwargs.len() != keys.len() {
continue;
Expand All @@ -111,7 +114,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
if values.is_empty() {
diagnostic.try_set_fix(|| {
remove_argument(
kw,
keyword,
&call.arguments,
Parentheses::Preserve,
checker.locator().contents(),
Expand All @@ -127,7 +130,7 @@ pub(crate) fn unnecessary_dict_kwargs(checker: &mut Checker, call: &ast::ExprCal
format!("{}={}", kwarg, checker.locator().slice(value.range()))
})
.join(", "),
kw.range(),
keyword.range(),
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ PIE804.py:22:1: PIE804 [*] Unnecessary `dict` kwargs
22 | foo(**{},)
| ^^^^^^^^^^ PIE804
23 |
24 | # Duplicated key names wont be fixed to avoid syntax error.
24 | # Duplicated key names won't be fixed, to avoid syntax errors.
|
= help: Remove unnecessary kwargs

Expand All @@ -139,12 +139,12 @@ PIE804.py:22:1: PIE804 [*] Unnecessary `dict` kwargs
22 |-foo(**{},)
22 |+foo()
23 23 |
24 24 | # Duplicated key names wont be fixed to avoid syntax error.
24 24 | # Duplicated key names won't be fixed, to avoid syntax errors.
25 25 | abc(**{'a': b}, **{'a': c}) # PIE804

PIE804.py:25:1: PIE804 [*] Unnecessary `dict` kwargs
|
24 | # Duplicated key names wont be fixed to avoid syntax error.
24 | # Duplicated key names won't be fixed, to avoid syntax errors.
25 | abc(**{'a': b}, **{'a': c}) # PIE804
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PIE804
|
Expand All @@ -153,7 +153,7 @@ PIE804.py:25:1: PIE804 [*] Unnecessary `dict` kwargs
Safe fix
22 22 | foo(**{},)
23 23 |
24 24 | # Duplicated key names wont be fixed to avoid syntax error.
24 24 | # Duplicated key names won't be fixed, to avoid syntax errors.
25 |-abc(**{'a': b}, **{'a': c}) # PIE804
25 |+abc(a=b, **{'a': c}) # PIE804

Expand Down

0 comments on commit 829b687

Please sign in to comment.