Repository: rust-lang/rustlings
File: src/list.rs (Rust)

Code before:

mod scroll_state;
mod state;

fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
    let mut list_state = ListState::new(app_state, stdout)?;
    let mut is_searching = false;

    loop {
        match event::read().context("Failed to read terminal event")? {
            Event::Key(key) => {
                match key.kind {
                    KeyEventKind::Release => continue,
                    KeyEventKind::Press | KeyEventKind::Repeat => (),
                }

                list_state.message.clear();

                let curr_key = key.code;

                if is_searching {
                    match curr_key {
                        KeyCode::Esc | KeyCode::Enter => {
                            is_searching = false;
                            list_state.search_query.clear();
                            continue;
                        }
                        KeyCode::Char(k) => {
                            list_state.search_query.push(k);
                            list_state.message.push_str("search:");
                            list_state.message.push_str(&list_state.search_query);
                            list_state.message.push('|');

                            list_state.select_if_matches_search_query();

                            list_state.draw(stdout)?;
                            continue;
                        }
                        KeyCode::Backspace => {
                            list_state.search_query.pop();
                            list_state.message.push_str("search:");
                            list_state.message.push_str(&list_state.search_query);
                            list_state.message.push('|');

                            list_state.select_if_matches_search_query();

                            list_state.draw(stdout)?;
                            continue;
                        }
                        _ => {
                            continue;

Review comment (suggestion):
We continue in each match arm. It would be better to use `continue` at the end of the `if` body instead.

Code after:

mod scroll_state;
mod state;

fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
    let mut list_state = ListState::new(app_state, stdout)?;
    let mut is_searching = false;

    loop {
        match event::read().context("Failed to read terminal event")? {
            Event::Key(key) => {
                match key.kind {
                    KeyEventKind::Release => continue,
                    KeyEventKind::Press | KeyEventKind::Repeat => (),
                }

                list_state.message.clear();

                let curr_key = key.code;

                if is_searching {
                    match curr_key {
                        KeyCode::Esc | KeyCode::Enter => {
                            is_searching = false;
                            list_state.search_query.clear();
                        }
                        KeyCode::Char(k) => {
                            list_state.search_query.push(k);
                            list_state.apply_search_query();
                            list_state.draw(stdout)?;
                        }
                        KeyCode::Backspace => {
                            list_state.search_query.pop();
                            list_state.apply_search_query();
                            list_state.draw(stdout)?;
                        }
                        _ => {}
                    }
                    continue;
                }

                match key.code {
                    KeyCode::Char('q') => return Ok(()),
                    KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
                    KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
                    KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
                    KeyCode::End | KeyCode::Char('G') => list_state.select_last(),
                    KeyCode::Char('d') => {
                        if list_state.filter() == Filter::Done {
                            list_state.set_filter(Filter::None);
                            list_state.message.push_str("Disabled filter DONE");