rust-lang/rust-clippy

Ban Stacked `if`'s

Open

#12,483 创建于 2024年3月14日

在 GitHub 查看
 (10 评论) (32 反应) (1 负责人)Rust (1,391 fork)batch import
A-lintgood first issue

仓库指标

Star
 (10,406 star)
PR 合并指标
 (平均合并 19天 22小时) (30 天内合并 113 个 PR)

描述

What it does

This lint should prohibit placing bool returning if statements as the expression inside another if statement. Currently, there appears to be no limit to the depth of stacked if's permitted by both rust fmt and clippy

Advantage

  • Stacked if's are hard to read

Drawbacks

  • Ternary-style expressions couldn't be used inside an if statement, but I personally think that's poor form

Example

if if if a == b {
    b == c
} else {
    a == c
} {
    a == d
} else {
    c == d
} {
    println!("True!");
} else {
    println!("False!");
}

Could be written as:

let expression_1 = if a == b {
    b == c
} else {
    a == c
};

let expression_2 = if expression_1 {
    a == d
} else {
    c == d
};

if expression_2  {
    println!("True!");
} else {
    println!("False!");
}

贡献者指南