Skip to content

Commit cb5938c

Browse files
committed
Mutable implementation if rewrite_ast_clause()
1 parent 5c24487 commit cb5938c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

query-grammar/src/query_grammar.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,23 @@ pub fn parse_to_ast<'a>() -> impl Parser<&'a str, Output = UserInputAst> {
380380
/// Removes unnecessary children clauses in AST
381381
///
382382
/// Motivated by [issue #1433](https://github.com/quickwit-oss/tantivy/issues/1433)
383-
fn rewrite_ast(input: UserInputAst) -> UserInputAst {
384-
if let UserInputAst::Clause(terms) = input {
385-
UserInputAst::Clause(terms.into_iter().map(rewrite_ast_clause).collect())
386-
} else {
387-
input
383+
fn rewrite_ast(mut input: UserInputAst) -> UserInputAst {
384+
if let UserInputAst::Clause(terms) = &mut input {
385+
for term in terms {
386+
rewrite_ast_clause(term);
387+
}
388388
}
389+
input
389390
}
390391

391-
fn rewrite_ast_clause(input: (Option<Occur>, UserInputAst)) -> (Option<Occur>, UserInputAst) {
392+
fn rewrite_ast_clause(input: &mut (Option<Occur>, UserInputAst)) {
392393
match input {
393-
(None, UserInputAst::Clause(mut clauses)) if clauses.len() == 1 => clauses.remove(0),
394-
other => other,
394+
(None, UserInputAst::Clause(ref mut clauses)) if clauses.len() == 1 => {
395+
let new_term = clauses.pop().unwrap(); // safe because clasues.len() == 1
396+
input.0 = new_term.0;
397+
input.1 = new_term.1;
398+
}
399+
_ => {}
395400
}
396401
}
397402

0 commit comments

Comments
 (0)