Performs the following validations on the arguments: 1. There aren't any duplicate keyword argument 2. Generator expressions are parenthesized when required by the argument context.
(
&mut self,
arguments: &ast::Arguments,
has_trailing_comma: bool,
context: ArgumentsContext,
)
| 3089 | /// 1. There aren't any duplicate keyword argument |
| 3090 | /// 2. Generator expressions are parenthesized when required by the argument context. |
| 3091 | fn validate_arguments( |
| 3092 | &mut self, |
| 3093 | arguments: &ast::Arguments, |
| 3094 | has_trailing_comma: bool, |
| 3095 | context: ArgumentsContext, |
| 3096 | ) { |
| 3097 | let mut all_arg_names = |
| 3098 | FxHashSet::with_capacity_and_hasher(arguments.keywords.len(), FxBuildHasher); |
| 3099 | |
| 3100 | for (name, range) in arguments |
| 3101 | .keywords |
| 3102 | .iter() |
| 3103 | .filter_map(|argument| argument.arg.as_ref().map(|arg| (arg, argument.range))) |
| 3104 | { |
| 3105 | let arg_name = name.as_str(); |
| 3106 | if !all_arg_names.insert(arg_name) { |
| 3107 | self.add_error( |
| 3108 | ParseErrorType::DuplicateKeywordArgumentError(arg_name.to_string()), |
| 3109 | range, |
| 3110 | ); |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | let generator_must_be_parenthesized = match context { |
| 3115 | ArgumentsContext::Call => has_trailing_comma || arguments.len() > 1, |
| 3116 | // CPython rejects an unparenthesized generator expression as a class base even though |
| 3117 | // this restriction isn't specified in the class definition grammar. |
| 3118 | ArgumentsContext::ClassDefinition => true, |
| 3119 | }; |
| 3120 | |
| 3121 | if generator_must_be_parenthesized { |
| 3122 | for arg in &*arguments.args { |
| 3123 | if let Some(ast::ExprGenerator { |
| 3124 | range, |
| 3125 | parenthesized: false, |
| 3126 | .. |
| 3127 | }) = arg.as_generator_expr() |
| 3128 | { |
| 3129 | // test_ok args_unparenthesized_generator |
| 3130 | // zip((x for x in range(10)), (y for y in range(10))) |
| 3131 | // sum(x for x in range(10)) |
| 3132 | // sum((x for x in range(10)),) |
| 3133 | |
| 3134 | // test_err args_unparenthesized_generator |
| 3135 | // sum(x for x in range(10), 5) |
| 3136 | // total(1, 2, x for x in range(5), 6) |
| 3137 | // sum(x for x in range(10),) |
| 3138 | self.add_error(ParseErrorType::UnparenthesizedGeneratorExpression, range); |
| 3139 | } |
| 3140 | } |
| 3141 | } |
| 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | /// Identifies the syntactic context for an argument list. |
no test coverage detected