Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert if_chain! macros to the regular if statement #9354

Open
nyurik opened this issue Aug 20, 2022 · 2 comments
Open

Convert if_chain! macros to the regular if statement #9354

nyurik opened this issue Aug 20, 2022 · 2 comments
Labels
A-lint Area: New lints

Comments

@nyurik
Copy link
Contributor

nyurik commented Aug 20, 2022

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() {
  // ...
}
@nyurik nyurik added the A-lint Area: New lints label Aug 20, 2022
@est31
Copy link
Member

est31 commented Aug 22, 2022

Note that it's not a 1:1 transition if you want to avoid warnings from the rust compiler. If there are suggestions, they should suggest moving any irrefutable patterns that the chain starts with outside of the chain, and any irrefutable pattern that the chain ends with into the body. Because otherwise there would be a lint as per rust-lang/rust#94951 .

@est31
Copy link
Member

est31 commented Nov 10, 2024

There is a tool for this now: https://github.com/Alexendoo/if-to-let-chain, which was used for #11750.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants