How to change code to stop "warning: unreachable pattern"

I don't want to suppress the warning unless I must, so I'm looking for a way to change the code so the warning will go away.

Any suggestions?

warning: unreachable pattern

Here's the code:

    fn on_input(&mut self, inp: &Input) {
        match inp {
            Input::Button(but) => match but.state {
                ButtonState::Press => match but.button {
                    Button::Keyboard(Key::Up) => self.up_d = true,
                    Button::Keyboard(Key::Down) => self.down_d = true,
                    Button::Keyboard(Key::Left) => self.left_d = true,
                    Button::Keyboard(Key::Right) => self.right_d = true,
                    _ => (),
                },
                ButtonState::Release => match but.button {
                    Button::Keyboard(Key::Up) => self.up_d = false,
                    Button::Keyboard(Key::Down) => self.down_d = false,
                    Button::Keyboard(Key::Left) => self.left_d = false,
                    Button::Keyboard(Key::Right) => self.right_d = false,
                    _ => (),
                },
                _ => {},  // WARNING HAPPENS ON THIS LINE
            },
            _ => {}
        }
    }

1 Like

Well think about it, you can only have 2 states, press and release, right?

So if you have cases for both of those, why do you need a third pattern? It's impossible for the third pattern to be triggered, because one of the first two will always match!

So, don't suppress the warning... remove that line, because it's not doing anything

@LoganDark Oh! Ok, ....I was under the impression a final option was mandatory in a match; good to know it's not.

1 Like

It's only necessary to be exhaustive; you don't need a _ case if your other cases already cover all possible outcomes

1 Like

@LoganDark Your insight is truly appreciated!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.