rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Add `let_else`, `if_let` and `if_expression` restriction lints that suggest `match`

Open a7ec5 opened this issue 1 month ago • 2 comments

What it does

Three lints, each reporting one branching construct and suggesting the equivalent match:

  • let_else — let PATTERN = EXPR else { ... }
  • if_let — if let, with or without else
  • if_expression — any if expression

Advantage

  • Every branch is in the text. In if let, if and let ... else a branch can be absent, and "absent" reads the same as "handled by doing nothing". A match has to spell out the wildcard arm, the empty arm or the diverging arm, so discarding data becomes a decision someone made.
  • Exits are enumerable: a return, break or continue hidden in an else or a nested branch becomes an arm, and all arms sit in one column.
  • Each arm states its own preconditions; the reader does not reconstruct what is true at this line and which side effects already ran.
  • Exhaustiveness is checked against the shape of whatever is matched. The build fails instead of the new case being silently unhandled.
  • Some people simply prefer the style.

Drawbacks

These lints are deliberately against the idiomatic style: the three constructs exist to shorten branches, and the suggestion is the longer form. That is why they belong to restriction and are off by default.

Example

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
        let Some(state) = &mut self.state else {
            return;
        };
        if state.window.id() != window_id {
            return;
        }

        if let WindowEvent::CloseRequested = event {
            event_loop.exit();
        } else if let WindowEvent::Resized(size) = event {
            state.gpu.resize(size);
            state.window.request_redraw();
        } else if let WindowEvent::RedrawRequested = event {
            state.gpu.render();
        }

Could be written as:

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
        match self.state.as_mut() {
            None => {}
            Some(state) if state.window.id() != window_id => {}
            Some(state) => match event {
                WindowEvent::CloseRequested => event_loop.exit(),
                WindowEvent::Resized(size) => {
                    state.gpu.resize(size);
                    state.window.request_redraw();
                }
                WindowEvent::RedrawRequested => state.gpu.render(),
                _ => {}
            },
        }

Comparison with existing lints

manual_let_else, single_match / single_match_else and match_bool recommend the opposite direction. else_if_without_else and if_then_some_else_none are existing restriction lints that already object to certain if shapes.

Additional Context

May be a complete example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=da5a1babedc260ff014534ab77cdd70c

a7ec5 avatar Oct 26 '26 15:10 a7ec5

Sorry to be that guy, but can you please split this into one lint suggestion per issue? 3-in-1 issues rarely work out well in my experinece.

Please also go into more depth how this should interact with the existing issues. For example, the match you described might be an false negative in one of our other lints instead from feels, but I have not looked at this in-depth

CommanderStorm avatar Oct 26 '26 18:10 CommanderStorm

@rustbot claim

Bimal-ray99 avatar Oct 27 '26 16:10 Bimal-ray99