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 {
8585 ) {
8686 NonminimalBoolVisitor { cx } . visit_body ( body) ;
8787 }
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+ }
88114}
89115struct NonminimalBoolVisitor < ' a , ' tcx > {
90116 cx : & ' a LateContext < ' tcx > ,
Original file line number Diff line number Diff line change @@ -156,3 +156,11 @@ fn issue11932() {
156156 x % 3 == 0
157157 } ;
158158}
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
114114LL | if matches!(true, true) && true {
115115 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `matches!(true, true)`
116116
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
118142
You can’t perform that action at this time.
0 commit comments