Skip to content

False positive on collapsible_if with if let #1197

@llogiq

Description

@llogiq

This is from linefeed:

fn read_init(&mut self) {
    if let Some(path) = env_init_file() {
        // If `INPUTRC` is present, even if invalid, parse nothing else.
        // Thus, an empty `INPUTRC` will inhibit loading configuration.
        self.read_init_file_if_exists(Some(path));
    } else {
        if !self.read_init_file_if_exists(user_init_file()) {
            self.read_init_file_if_exists(system_init_file());
        }
    }
}

If you try to collapse this, rustc won't have it:

error[E0301]: cannot mutably borrow in a pattern guard
    --> src/reader.rs:2116:20
     |
2116 |         } else if !self.read_init_file_if_exists(user_init_file()) {
     |                    ^^^^ borrowed mutably in pattern guard

error: aborting due to previous error

The difference appears to be that the else if part of an if let desugars to a guard. And guards appear to have different borrowing rules than simple if expressions (I'm not sure why). Perhaps that is a bug in borrowck, but as long as it is there, we should at least note it in the docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions