summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/needless_if.rs
blob: e2ad17e69a875fb4bf571d0b8a0298af34e2154e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//@aux-build:proc_macros.rs
#![feature(let_chains)]
#![allow(
    clippy::blocks_in_if_conditions,
    clippy::if_same_then_else,
    clippy::ifs_same_cond,
    clippy::let_unit_value,
    clippy::needless_else,
    clippy::no_effect,
    clippy::nonminimal_bool,
    clippy::short_circuit_statement,
    clippy::unnecessary_operation,
    unused
)]
#![warn(clippy::needless_if)]

extern crate proc_macros;
use proc_macros::{external, with_span};

fn maybe_side_effect() -> bool {
    true
}

fn main() {
    // Lint
    if (true) {}
    // Do not remove the condition
    if maybe_side_effect() {}
    // Do not lint
    if (true) {
    } else {
    }
    if {
        return;
    } {}
    // Do not lint if `else if` is present
    if (true) {
    } else if (true) {
    }
    // Do not lint `if let` or let chains
    if let true = true {}
    if let true = true
        && true
    {}
    if true
        && let true = true
    {}
    // Can lint nested `if let`s
    if {
        if let true = true
            && true
        {
            true
        } else {
            false
        }
    } && true
    {}
    external! { if (true) {} }
    with_span! {
        span
        if (true) {}
    }

    if true {
        // comment
    }

    if true {
        #[cfg(any())]
        foo;
    }

    macro_rules! empty_expansion {
        () => {};
    }

    if true {
        empty_expansion!();
    }

    macro_rules! empty_repetition {
        ($($t:tt)*) => {
            if true {
                $($t)*
            }
        }
    }

    empty_repetition!();

    // Must be placed into an expression context to not be interpreted as a block
    if { maybe_side_effect() } {}
    // Would be a block followed by `&&true` - a double reference to `true`
    if { maybe_side_effect() } && true {}

    // Don't leave trailing attributes
    #[allow(unused)]
    if true {}

    let () = if maybe_side_effect() {};
}