Skip to content

Convert if_chain! macros to the regular if statement #9354

Open
@nyurik

Description

@nyurik

What it does

The if-let chains are getting very close to stabilization (they were stabilized but later reverted due to a bug). There are nearly 500 if_chain! usages in this repo alone, so converting them by hand might prove too error-prone. Plus, there are other projects using the same if_chain crate (5 million+ downloads).

I do not know if its possible to create a lint that uses an external crate (any suggestions or links?), but if so, I would think this would be useful to other code as well.

Lint Name

uses_if_chain_crate

Category

complexity

Advantage

keep the code simpler by using non-macro code

Drawbacks

cargo fmt still hasn't been decided (see rfc) on how to handle it, and doesn't seem likely to be decided soon. This is not a blocker, as the code can always be reformatted later. Also, the current macro code is not supported by fmt either, so there is no difference in this regard.

Example

if_chain! {
    if let Some(retexpr) = block.expr;
    if let Some(stmt) = block.stmts.iter().last();
    then {
      // ...
    }
}

Could be written as:

if let Some(retexpr) = block.expr 
    && let Some(stmt) = block.stmts.iter().last() {
  // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions