Skip to content

Commit 7e57947

Browse files
committed
add new lint: stacked_if_match
1 parent 2536745 commit 7e57947

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5931,6 +5931,7 @@ Released 2018-09-13
59315931
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
59325932
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
59335933
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
5934+
[`stacked_if_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#stacked_if_match
59345935
[`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc
59355936
[`std_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core
59365937
[`str_split_at_newline`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_split_at_newline

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
662662
crate::size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT_INFO,
663663
crate::size_of_ref::SIZE_OF_REF_INFO,
664664
crate::slow_vector_initialization::SLOW_VECTOR_INITIALIZATION_INFO,
665+
crate::stacked_if_match::STACKED_IF_MATCH_INFO,
665666
crate::std_instead_of_core::ALLOC_INSTEAD_OF_CORE_INFO,
666667
crate::std_instead_of_core::STD_INSTEAD_OF_ALLOC_INFO,
667668
crate::std_instead_of_core::STD_INSTEAD_OF_CORE_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ mod single_range_in_vec_init;
335335
mod size_of_in_element_count;
336336
mod size_of_ref;
337337
mod slow_vector_initialization;
338+
mod stacked_if_match;
338339
mod std_instead_of_core;
339340
mod string_patterns;
340341
mod strings;
@@ -942,5 +943,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
942943
store.register_late_pass(move |_| Box::new(manual_div_ceil::ManualDivCeil::new(conf)));
943944
store.register_late_pass(|_| Box::new(manual_is_power_of_two::ManualIsPowerOfTwo));
944945
store.register_late_pass(|_| Box::new(non_zero_suggestions::NonZeroSuggestions));
946+
store.register_late_pass(|_| Box::new(stacked_if_match::StackedIfMatch));
945947
// add lints here, do not remove this comment, it's used in `new_lint`
946948
}

clippy_lints/src/stacked_if_match.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use rustc_hir::*;
2+
use rustc_lint::{LateContext, LateLintPass};
3+
use rustc_session::declare_lint_pass;
4+
5+
declare_clippy_lint! {
6+
/// ### What it does
7+
/// Checks for stacked `if` and `match`, e.g., `if if`.
8+
///
9+
/// ### Why is this bad?
10+
/// Stacked `if`'s and `match`'s are hard to read.
11+
///
12+
/// ### Example
13+
/// ```no_run
14+
/// if if a == b {
15+
/// c == d
16+
/// } else {
17+
/// e == f
18+
/// } {
19+
/// println!("true");
20+
/// } else {
21+
/// println!("false");
22+
/// }
23+
/// ```
24+
///
25+
/// Use instead:
26+
/// ```no_run
27+
/// let cond = if a == b {
28+
/// c == d
29+
/// } else {
30+
/// e == f
31+
/// };
32+
///
33+
/// if cond {
34+
/// println!("true");
35+
/// } else {
36+
/// println!("false");
37+
/// }
38+
/// ```
39+
#[clippy::version = "1.82.0"]
40+
pub STACKED_IF_MATCH,
41+
style,
42+
"`if if` and `match match` that can be eliminated"
43+
}
44+
45+
declare_lint_pass!(StackedIfMatch => [STACKED_IF_MATCH]);
46+
47+
impl<'tcx> LateLintPass<'tcx> for StackedIfMatch {
48+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
49+
50+
}
51+
}

tests/ui/stacked_if_match.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![warn(clippy::stacked_if_match)]
2+
3+
fn main() {
4+
// test code goes here
5+
}

0 commit comments

Comments
 (0)