Skip to content

Commit b3b65a1

Browse files
committed
Add test
1 parent 13ad14b commit b3b65a1

File tree

4 files changed

+148
-13
lines changed

4 files changed

+148
-13
lines changed

clippy_utils/src/sugg.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,4 +1040,37 @@ mod test {
10401040
let sugg = Sugg::BinOp(AssocOp::Add, "(1 + 1) + (1 + 1)".into());
10411041
assert_eq!("((1 + 1) + (1 + 1))", sugg.maybe_par().to_string());
10421042
}
1043+
#[test]
1044+
fn not_op() {
1045+
use AssocOp::{Add, Equal, Greater, GreaterEqual, LAnd, LOr, Less, LessEqual, NotEqual};
1046+
1047+
// Invert the comparison operator.
1048+
let sugg = Sugg::BinOp(Equal, "1 == 1".into());
1049+
assert_eq!("1 != 1", (!sugg).to_string());
1050+
1051+
let sugg = Sugg::BinOp(NotEqual, "1 != 1".into());
1052+
assert_eq!("1 == 1", (!sugg).to_string());
1053+
1054+
let sugg = Sugg::BinOp(Less, "1 < 1".into());
1055+
assert_eq!("1 >= 1", (!sugg).to_string());
1056+
1057+
let sugg = Sugg::BinOp(LessEqual, "1 <= 1".into());
1058+
assert_eq!("1 > 1", (!sugg).to_string());
1059+
1060+
let sugg = Sugg::BinOp(Greater, "1 > 1".into());
1061+
assert_eq!("1 <= 1", (!sugg).to_string());
1062+
1063+
let sugg = Sugg::BinOp(GreaterEqual, "1 >= 1".into());
1064+
assert_eq!("1 < 1", (!sugg).to_string());
1065+
1066+
// Other operators are inverted like !(..).
1067+
let sugg = Sugg::BinOp(Add, "1 + 1".into());
1068+
assert_eq!("!(1 + 1)", (!sugg).to_string());
1069+
1070+
let sugg = Sugg::BinOp(LAnd, "1 && 1".into());
1071+
assert_eq!("!(1 && 1)", (!sugg).to_string());
1072+
1073+
let sugg = Sugg::BinOp(LOr, "1 || 1".into());
1074+
assert_eq!("!(1 || 1)", (!sugg).to_string());
1075+
}
10431076
}

tests/ui/needless_bool/fixable.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ fn main() {
4141
x;
4242
!x;
4343
!(x && y);
44+
let a = 0;
45+
let b = 1;
46+
47+
a != b;
48+
a == b;
49+
a >= b;
50+
a > b;
51+
a <= b;
52+
a < b;
4453
if x {
4554
x
4655
} else {

tests/ui/needless_bool/fixable.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,39 @@ fn main() {
5353
} else {
5454
true
5555
};
56+
let a = 0;
57+
let b = 1;
58+
59+
if a == b {
60+
false
61+
} else {
62+
true
63+
};
64+
if a != b {
65+
false
66+
} else {
67+
true
68+
};
69+
if a < b {
70+
false
71+
} else {
72+
true
73+
};
74+
if a <= b {
75+
false
76+
} else {
77+
true
78+
};
79+
if a > b {
80+
false
81+
} else {
82+
true
83+
};
84+
if a >= b {
85+
false
86+
} else {
87+
true
88+
};
5689
if x {
5790
x
5891
} else {

tests/ui/needless_bool/fixable.stderr

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,67 @@ LL | | };
3131
| |_____^ help: you can reduce it to: `!(x && y)`
3232

3333
error: this if-then-else expression returns a bool literal
34-
--> $DIR/fixable.rs:72:5
34+
--> $DIR/fixable.rs:59:5
35+
|
36+
LL | / if a == b {
37+
LL | | false
38+
LL | | } else {
39+
LL | | true
40+
LL | | };
41+
| |_____^ help: you can reduce it to: `a != b`
42+
43+
error: this if-then-else expression returns a bool literal
44+
--> $DIR/fixable.rs:64:5
45+
|
46+
LL | / if a != b {
47+
LL | | false
48+
LL | | } else {
49+
LL | | true
50+
LL | | };
51+
| |_____^ help: you can reduce it to: `a == b`
52+
53+
error: this if-then-else expression returns a bool literal
54+
--> $DIR/fixable.rs:69:5
55+
|
56+
LL | / if a < b {
57+
LL | | false
58+
LL | | } else {
59+
LL | | true
60+
LL | | };
61+
| |_____^ help: you can reduce it to: `a >= b`
62+
63+
error: this if-then-else expression returns a bool literal
64+
--> $DIR/fixable.rs:74:5
65+
|
66+
LL | / if a <= b {
67+
LL | | false
68+
LL | | } else {
69+
LL | | true
70+
LL | | };
71+
| |_____^ help: you can reduce it to: `a > b`
72+
73+
error: this if-then-else expression returns a bool literal
74+
--> $DIR/fixable.rs:79:5
75+
|
76+
LL | / if a > b {
77+
LL | | false
78+
LL | | } else {
79+
LL | | true
80+
LL | | };
81+
| |_____^ help: you can reduce it to: `a <= b`
82+
83+
error: this if-then-else expression returns a bool literal
84+
--> $DIR/fixable.rs:84:5
85+
|
86+
LL | / if a >= b {
87+
LL | | false
88+
LL | | } else {
89+
LL | | true
90+
LL | | };
91+
| |_____^ help: you can reduce it to: `a < b`
92+
93+
error: this if-then-else expression returns a bool literal
94+
--> $DIR/fixable.rs:105:5
3595
|
3696
LL | / if x {
3797
LL | | return true;
@@ -41,7 +101,7 @@ LL | | };
41101
| |_____^ help: you can reduce it to: `return x`
42102

43103
error: this if-then-else expression returns a bool literal
44-
--> $DIR/fixable.rs:80:5
104+
--> $DIR/fixable.rs:113:5
45105
|
46106
LL | / if x {
47107
LL | | return false;
@@ -51,7 +111,7 @@ LL | | };
51111
| |_____^ help: you can reduce it to: `return !x`
52112

53113
error: this if-then-else expression returns a bool literal
54-
--> $DIR/fixable.rs:88:5
114+
--> $DIR/fixable.rs:121:5
55115
|
56116
LL | / if x && y {
57117
LL | | return true;
@@ -61,7 +121,7 @@ LL | | };
61121
| |_____^ help: you can reduce it to: `return x && y`
62122

63123
error: this if-then-else expression returns a bool literal
64-
--> $DIR/fixable.rs:96:5
124+
--> $DIR/fixable.rs:129:5
65125
|
66126
LL | / if x && y {
67127
LL | | return false;
@@ -71,33 +131,33 @@ LL | | };
71131
| |_____^ help: you can reduce it to: `return !(x && y)`
72132

73133
error: equality checks against true are unnecessary
74-
--> $DIR/fixable.rs:104:8
134+
--> $DIR/fixable.rs:137:8
75135
|
76136
LL | if x == true {};
77137
| ^^^^^^^^^ help: try simplifying it as shown: `x`
78138
|
79139
= note: `-D clippy::bool-comparison` implied by `-D warnings`
80140

81141
error: equality checks against false can be replaced by a negation
82-
--> $DIR/fixable.rs:108:8
142+
--> $DIR/fixable.rs:141:8
83143
|
84144
LL | if x == false {};
85145
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
86146

87147
error: equality checks against true are unnecessary
88-
--> $DIR/fixable.rs:118:8
148+
--> $DIR/fixable.rs:151:8
89149
|
90150
LL | if x == true {};
91151
| ^^^^^^^^^ help: try simplifying it as shown: `x`
92152

93153
error: equality checks against false can be replaced by a negation
94-
--> $DIR/fixable.rs:119:8
154+
--> $DIR/fixable.rs:152:8
95155
|
96156
LL | if x == false {};
97157
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
98158

99159
error: this if-then-else expression returns a bool literal
100-
--> $DIR/fixable.rs:128:12
160+
--> $DIR/fixable.rs:161:12
101161
|
102162
LL | } else if returns_bool() {
103163
| ____________^
@@ -108,7 +168,7 @@ LL | | };
108168
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
109169

110170
error: this if-then-else expression returns a bool literal
111-
--> $DIR/fixable.rs:141:5
171+
--> $DIR/fixable.rs:174:5
112172
|
113173
LL | / if unsafe { no(4) } & 1 != 0 {
114174
LL | | true
@@ -118,16 +178,16 @@ LL | | };
118178
| |_____^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`
119179

120180
error: this if-then-else expression returns a bool literal
121-
--> $DIR/fixable.rs:146:30
181+
--> $DIR/fixable.rs:179:30
122182
|
123183
LL | let _brackets_unneeded = if unsafe { no(4) } & 1 != 0 { true } else { false };
124184
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `unsafe { no(4) } & 1 != 0`
125185

126186
error: this if-then-else expression returns a bool literal
127-
--> $DIR/fixable.rs:149:9
187+
--> $DIR/fixable.rs:182:9
128188
|
129189
LL | if unsafe { no(4) } & 1 != 0 { true } else { false }
130190
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`
131191

132-
error: aborting due to 15 previous errors
192+
error: aborting due to 21 previous errors
133193

0 commit comments

Comments
 (0)