Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit a39a96a

Browse files
charliermarshzanieb
authored andcommitted
Optimize validate_arguments (#10)
1 parent 30ec63d commit a39a96a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

parser/src/function.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ pub(crate) struct ArgumentList {
1616

1717
// Perform validation of function/lambda arguments in a function definition.
1818
pub(crate) fn validate_arguments(arguments: &ast::Arguments) -> Result<(), LexicalError> {
19-
let mut all_arg_names = FxHashSet::with_hasher(Default::default());
19+
let mut all_arg_names = FxHashSet::with_capacity_and_hasher(
20+
arguments.posonlyargs.len()
21+
+ arguments.args.len()
22+
+ arguments.vararg.is_some() as usize
23+
+ arguments.kwonlyargs.len()
24+
+ arguments.kwarg.is_some() as usize,
25+
Default::default(),
26+
);
2027

2128
let posonlyargs = arguments.posonlyargs.iter();
2229
let args = arguments.args.iter();
@@ -83,16 +90,14 @@ pub(crate) fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentLis
8390
Some((start, end, name)) => {
8491
// Check for duplicate keyword arguments in the call.
8592
if let Some(keyword_name) = &name {
86-
if keyword_names.contains(keyword_name) {
93+
if !keyword_names.insert(keyword_name.clone()) {
8794
return Err(LexicalError {
8895
error: LexicalErrorType::DuplicateKeywordArgumentError(
8996
keyword_name.to_string(),
9097
),
9198
location: start,
9299
});
93100
}
94-
95-
keyword_names.insert(keyword_name.clone());
96101
} else {
97102
double_starred = true;
98103
}

parser/src/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ def args_to_tuple(*args: *Ts) -> Tuple[*Ts]: ...
11041104
#[test]
11051105
#[cfg(feature = "all-nodes-with-ranges")]
11061106
fn decorator_ranges() {
1107-
let parse_ast = parse_program(
1107+
let parse_ast = ast::Suite::parse(
11081108
r#"
11091109
@my_decorator
11101110
def test():

0 commit comments

Comments
 (0)