This commit is contained in:
hotate29 2021-12-14 21:27:55 +09:00
parent 13ad14b22d
commit b3b65a1bf6
No known key found for this signature in database
GPG Key ID: DB720E72A6380B5A
4 changed files with 148 additions and 13 deletions

View File

@ -1040,4 +1040,37 @@ fn binop_maybe_par() {
let sugg = Sugg::BinOp(AssocOp::Add, "(1 + 1) + (1 + 1)".into());
assert_eq!("((1 + 1) + (1 + 1))", sugg.maybe_par().to_string());
}
#[test]
fn not_op() {
use AssocOp::{Add, Equal, Greater, GreaterEqual, LAnd, LOr, Less, LessEqual, NotEqual};
// Invert the comparison operator.
let sugg = Sugg::BinOp(Equal, "1 == 1".into());
assert_eq!("1 != 1", (!sugg).to_string());
let sugg = Sugg::BinOp(NotEqual, "1 != 1".into());
assert_eq!("1 == 1", (!sugg).to_string());
let sugg = Sugg::BinOp(Less, "1 < 1".into());
assert_eq!("1 >= 1", (!sugg).to_string());
let sugg = Sugg::BinOp(LessEqual, "1 <= 1".into());
assert_eq!("1 > 1", (!sugg).to_string());
let sugg = Sugg::BinOp(Greater, "1 > 1".into());
assert_eq!("1 <= 1", (!sugg).to_string());
let sugg = Sugg::BinOp(GreaterEqual, "1 >= 1".into());
assert_eq!("1 < 1", (!sugg).to_string());
// Other operators are inverted like !(..).
let sugg = Sugg::BinOp(Add, "1 + 1".into());
assert_eq!("!(1 + 1)", (!sugg).to_string());
let sugg = Sugg::BinOp(LAnd, "1 && 1".into());
assert_eq!("!(1 && 1)", (!sugg).to_string());
let sugg = Sugg::BinOp(LOr, "1 || 1".into());
assert_eq!("!(1 || 1)", (!sugg).to_string());
}
}

View File

@ -41,6 +41,15 @@ fn main() {
x;
!x;
!(x && y);
let a = 0;
let b = 1;
a != b;
a == b;
a >= b;
a > b;
a <= b;
a < b;
if x {
x
} else {

View File

@ -53,6 +53,39 @@ fn main() {
} else {
true
};
let a = 0;
let b = 1;
if a == b {
false
} else {
true
};
if a != b {
false
} else {
true
};
if a < b {
false
} else {
true
};
if a <= b {
false
} else {
true
};
if a > b {
false
} else {
true
};
if a >= b {
false
} else {
true
};
if x {
x
} else {

View File

@ -31,7 +31,67 @@ LL | | };
| |_____^ help: you can reduce it to: `!(x && y)`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:72:5
--> $DIR/fixable.rs:59:5
|
LL | / if a == b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a != b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:64:5
|
LL | / if a != b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a == b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:69:5
|
LL | / if a < b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a >= b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:74:5
|
LL | / if a <= b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a > b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:79:5
|
LL | / if a > b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a <= b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:84:5
|
LL | / if a >= b {
LL | | false
LL | | } else {
LL | | true
LL | | };
| |_____^ help: you can reduce it to: `a < b`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:105:5
|
LL | / if x {
LL | | return true;
@ -41,7 +101,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return x`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:80:5
--> $DIR/fixable.rs:113:5
|
LL | / if x {
LL | | return false;
@ -51,7 +111,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return !x`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:88:5
--> $DIR/fixable.rs:121:5
|
LL | / if x && y {
LL | | return true;
@ -61,7 +121,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return x && y`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:96:5
--> $DIR/fixable.rs:129:5
|
LL | / if x && y {
LL | | return false;
@ -71,7 +131,7 @@ LL | | };
| |_____^ help: you can reduce it to: `return !(x && y)`
error: equality checks against true are unnecessary
--> $DIR/fixable.rs:104:8
--> $DIR/fixable.rs:137:8
|
LL | if x == true {};
| ^^^^^^^^^ help: try simplifying it as shown: `x`
@ -79,25 +139,25 @@ LL | if x == true {};
= note: `-D clippy::bool-comparison` implied by `-D warnings`
error: equality checks against false can be replaced by a negation
--> $DIR/fixable.rs:108:8
--> $DIR/fixable.rs:141:8
|
LL | if x == false {};
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
error: equality checks against true are unnecessary
--> $DIR/fixable.rs:118:8
--> $DIR/fixable.rs:151:8
|
LL | if x == true {};
| ^^^^^^^^^ help: try simplifying it as shown: `x`
error: equality checks against false can be replaced by a negation
--> $DIR/fixable.rs:119:8
--> $DIR/fixable.rs:152:8
|
LL | if x == false {};
| ^^^^^^^^^^ help: try simplifying it as shown: `!x`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:128:12
--> $DIR/fixable.rs:161:12
|
LL | } else if returns_bool() {
| ____________^
@ -108,7 +168,7 @@ LL | | };
| |_____^ help: you can reduce it to: `{ !returns_bool() }`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:141:5
--> $DIR/fixable.rs:174:5
|
LL | / if unsafe { no(4) } & 1 != 0 {
LL | | true
@ -118,16 +178,16 @@ LL | | };
| |_____^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:146:30
--> $DIR/fixable.rs:179:30
|
LL | let _brackets_unneeded = if unsafe { no(4) } & 1 != 0 { true } else { false };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `unsafe { no(4) } & 1 != 0`
error: this if-then-else expression returns a bool literal
--> $DIR/fixable.rs:149:9
--> $DIR/fixable.rs:182:9
|
LL | if unsafe { no(4) } & 1 != 0 { true } else { false }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can reduce it to: `(unsafe { no(4) } & 1 != 0)`
error: aborting due to 15 previous errors
error: aborting due to 21 previous errors