summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/range_contains.rs
blob: c3188ec6d92bed011d8cc01db331aab9062e3d94 (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
#![warn(clippy::manual_range_contains)]
#![allow(unused)]
#![allow(clippy::no_effect)]
#![allow(clippy::short_circuit_statement)]
#![allow(clippy::unnecessary_operation)]
#![allow(clippy::impossible_comparisons)]
#![allow(clippy::redundant_comparisons)]

fn main() {
    let x = 9_i32;

    // order shouldn't matter
    x >= 8 && x < 12;
    x < 42 && x >= 21;
    100 > x && 1 <= x;

    // also with inclusive ranges
    x >= 9 && x <= 99;
    x <= 33 && x >= 1;
    999 >= x && 1 <= x;

    // and the outside
    x < 8 || x >= 12;
    x >= 42 || x < 21;
    100 <= x || 1 > x;

    // also with the outside of inclusive ranges
    x < 9 || x > 99;
    x > 33 || x < 1;
    999 < x || 1 > x;

    // not a range.contains
    x > 8 && x < 12; // lower bound not inclusive
    x < 8 && x <= 12; // same direction
    x >= 12 && 12 >= x; // same bounds
    x < 8 && x > 12; // wrong direction

    x <= 8 || x >= 12;
    x >= 8 || x >= 12;
    x < 12 || 12 < x;
    x >= 8 || x <= 12;

    // Fix #6315
    let y = 3.;
    y >= 0. && y < 1.;
    y < 0. || y > 1.;

    // handle negatives #8721
    x >= -10 && x <= 10;
    x >= 10 && x <= -10;
    y >= -3. && y <= 3.;
    y >= 3. && y <= -3.;

    // Fix #8745
    let z = 42;
    (x >= 0) && (x <= 10) && (z >= 0) && (z <= 10);
    (x < 0) || (x >= 10) || (z < 0) || (z >= 10);
    // Make sure operators in parens don't give a breaking suggestion
    ((x % 2 == 0) || (x < 0)) || (x >= 10);
}

// Fix #6373
pub const fn in_range(a: i32) -> bool {
    3 <= a && a <= 20
}

#[clippy::msrv = "1.34"]
fn msrv_1_34() {
    let x = 5;
    x >= 8 && x < 34;
}

#[clippy::msrv = "1.35"]
fn msrv_1_35() {
    let x = 5;
    x >= 8 && x < 35;
}