This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,32 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
85
85
) {
86
86
NonminimalBoolVisitor { cx } . visit_body ( body) ;
87
87
}
88
+
89
+ fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
90
+ if let ExprKind :: Unary ( UnOp :: Not , sub) = expr. kind
91
+ && !expr. span . from_expansion ( )
92
+ && let ExprKind :: Binary ( op, left, right) = sub. kind
93
+ {
94
+ let new_op = match op. node {
95
+ BinOpKind :: Eq => "!=" ,
96
+ BinOpKind :: Ne => "==" ,
97
+ _ => return ,
98
+ } ;
99
+ let Some ( left) = snippet_opt ( cx, left. span ) else { return } ;
100
+ let Some ( right) = snippet_opt ( cx, right. span ) else {
101
+ return ;
102
+ } ;
103
+ span_lint_and_sugg (
104
+ cx,
105
+ NONMINIMAL_BOOL ,
106
+ expr. span ,
107
+ "this boolean expression can be simplified" ,
108
+ "try" ,
109
+ format ! ( "{left} {new_op} {right}" ) ,
110
+ Applicability :: MachineApplicable ,
111
+ ) ;
112
+ }
113
+ }
88
114
}
89
115
struct NonminimalBoolVisitor < ' a , ' tcx > {
90
116
cx : & ' a LateContext < ' tcx > ,
Original file line number Diff line number Diff line change @@ -156,3 +156,11 @@ fn issue11932() {
156
156
x % 3 == 0
157
157
} ;
158
158
}
159
+
160
+ fn issue_5794 ( ) {
161
+ let a = 0 ;
162
+ if !( 12 == a) { } //~ ERROR: this boolean expression can be simplified
163
+ if !( a == 12 ) { } //~ ERROR: this boolean expression can be simplified
164
+ if !( 12 != a) { } //~ ERROR: this boolean expression can be simplified
165
+ if !( a != 12 ) { } //~ ERROR: this boolean expression can be simplified
166
+ }
Original file line number Diff line number Diff line change @@ -114,5 +114,29 @@ error: this boolean expression can be simplified
114
114
LL | if matches!(true, true) && true {
115
115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
116
116
117
- error: aborting due to 13 previous errors
117
+ error: this boolean expression can be simplified
118
+ --> $DIR/nonminimal_bool.rs:162:8
119
+ |
120
+ LL | if !(12 == a) {}
121
+ | ^^^^^^^^^^ help: try: `12 != a`
122
+
123
+ error: this boolean expression can be simplified
124
+ --> $DIR/nonminimal_bool.rs:163:8
125
+ |
126
+ LL | if !(a == 12) {}
127
+ | ^^^^^^^^^^ help: try: `a != 12`
128
+
129
+ error: this boolean expression can be simplified
130
+ --> $DIR/nonminimal_bool.rs:164:8
131
+ |
132
+ LL | if !(12 != a) {}
133
+ | ^^^^^^^^^^ help: try: `12 == a`
134
+
135
+ error: this boolean expression can be simplified
136
+ --> $DIR/nonminimal_bool.rs:165:8
137
+ |
138
+ LL | if !(a != 12) {}
139
+ | ^^^^^^^^^^ help: try: `a == 12`
140
+
141
+ error: aborting due to 17 previous errors
118
142
You can’t perform that action at this time.
0 commit comments