Skip to content

if let scoping of lifetime creates compiler warning #83419

Open
@xNxExOx

Description

@xNxExOx
    use glium::Display;
    let display = Display::new(builder, context, &event_loop).unwrap();
    if let gl_window = display.gl_window() {
        let window = gl_window.window();
        platform.attach_window(&mut gui_display, window, HiDpiMode::Default);
    }
    return display;

The current output is:

warning: irrefutable `if let` pattern
   --> imgui-example\src\main.rs:153:5
    |
153 | /     if let gl_window = display.gl_window() {
154 | |         let window = gl_window.window();
155 | |         platform.attach_window(&mut gui_display, window, HiDpiMode::Default);
156 | |     }
    | |_____^
    |
    = note: `#[warn(irrefutable_let_patterns)]` on by default
    = note: this pattern will always match, so the `if let` is useless
    = help: consider replacing the `if let` with a `let`

Following the suggestion breaks the code, because lifetime in gl_window could be used in Drop of gl_window while display is still being used.

I am not even sure if this can be reasonably implemented, so it is more of an question/sugestion what you think about scoping with if let over manual call Drop, or wrapping with block like this:

    use glium::Display;
    let display = Display::new(builder, context, &event_loop).unwrap();
    {
        let gl_window = display.gl_window();
        let window = gl_window.window();
        platform.attach_window(&mut gui_display, window, HiDpiMode::Default);
    }
    return display;

n my opinion if let scoping is the shortest, and best explains the scope, without looking like too artificial scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-discussionCategory: Discussion or questions that doesn't represent real issues.L-irrefutable_let_patternsLint: irrefutable_let_patternsT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions