Skip to content

Commit e0e361e

Browse files
authored
Add length-comparison-to-empty-check rule (#472)
Closes #426.
1 parent 8f3e3b4 commit e0e361e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

default-recommendations/list-shortcuts-test.rkt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,46 @@ test: "consing onto static improper list expression can be simplified"
269269
- (list* 1 2 (list* 3 4 (list* 5)))
270270
- (list* (list* 1 2 3 4 5))
271271
- (list* 1 2 3 4 5)
272+
273+
274+
test: "comparing length to zero refactorable to empty check"
275+
------------------------------
276+
(require racket/list)
277+
(equal? (length (list 1 2 3)) 0)
278+
------------------------------
279+
------------------------------
280+
(require racket/list)
281+
(eqv? (length (list 1 2 3)) 0)
282+
------------------------------
283+
------------------------------
284+
(require racket/list)
285+
(eq? (length (list 1 2 3)) 0)
286+
------------------------------
287+
------------------------------
288+
(require racket/list)
289+
(= (length (list 1 2 3)) 0)
290+
------------------------------
291+
------------------------------
292+
(require racket/list)
293+
(equal? 0 (length (list 1 2 3)))
294+
------------------------------
295+
------------------------------
296+
(require racket/list)
297+
(eqv? 0 (length (list 1 2 3)))
298+
------------------------------
299+
------------------------------
300+
(require racket/list)
301+
(eq? 0 (length (list 1 2 3)))
302+
------------------------------
303+
------------------------------
304+
(require racket/list)
305+
(= 0 (length (list 1 2 3)))
306+
------------------------------
307+
------------------------------
308+
(require racket/list)
309+
(zero? (length (list 1 2 3)))
310+
------------------------------
311+
------------------------------
312+
(require racket/list)
313+
(empty? (list 1 2 3))
314+
------------------------------

default-recommendations/list-shortcuts.rkt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@
246246
(expr.maker expr.element ... (~? expr.improper-tail)))
247247

248248

249+
(define-refactoring-rule length-comparison-to-empty-check
250+
#:description
251+
"Checking if a list's length is zero is less efficient than using the `empty?` predicate"
252+
#:literals (length equal? eqv? eq? = zero?)
253+
(~or ((~or equal? eqv? eq? =) (length list-expr:expr) 0)
254+
((~or equal? eqv? eq? =) 0 (length list-expr:expr))
255+
(zero? (length list-expr:expr)))
256+
(empty? list-expr))
257+
258+
249259
(define-refactoring-suite list-shortcuts
250260
#:rules (append-single-list-to-single-list
251261
append*-and-map-to-append-map
@@ -257,6 +267,7 @@
257267
filter-to-remv*
258268
first-reverse-to-last
259269
ignored-map-to-for-each
270+
length-comparison-to-empty-check
260271
list-selectors-to-take-and-drop
261272
quasiquote-to-append
262273
quasiquote-to-list

0 commit comments

Comments
 (0)