Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions default-recommendations/loops/for-loop-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -341,51 +341,91 @@ test: "nested for/and forms can be flattened to a for*/and form"
------------------------------


test: "(when ...) in a for loop refactored to #:when clause"
test: "(when ...) in a for loop refactored to #:when clause when multiple body forms"
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)))
(displayln x)
(displayln (* x 2))))
============================================================
(for ([x (in-list (list 1 2 'a 3 'b 4))]
#:when (number? x))
(displayln x))
(displayln x)
(displayln (* x 2)))
------------------------------------------------------------


test: "(when ...) in a for* loop refactored to #:when clause"
no-change-test: "(when ...) with single body form in a for loop not refactored"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)))
------------------------------------------------------------


test: "(when ...) in a for* loop refactored to #:when clause when multiple body forms"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)
(displayln (* x 2))))
============================================================
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
#:when (number? x))
(displayln x))
(displayln x)
(displayln (* x 2)))
------------------------------------------------------------


no-change-test: "(when ...) with single body form in a for* loop not refactored"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(when (number? x)
(displayln x)))
------------------------------------------------------------


test: "(unless ...) in a for loop refactored to #:when clause"
test: "(unless ...) in a for loop refactored to #:unless clause when multiple body forms"
------------------------------------------------------------
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)))
(displayln x)
(displayln "non-number")))
============================================================
(for ([x (in-list (list 1 2 'a 3 'b 4))]
#:unless (number? x))
(displayln x))
(displayln x)
(displayln "non-number"))
------------------------------------------------------------


test: "(unless ...) in a for* loop refactored to #:when clause"
no-change-test: "(unless ...) with single body form in a for loop not refactored"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(for ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)))
------------------------------------------------------------


test: "(unless ...) in a for* loop refactored to #:unless clause when multiple body forms"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)
(displayln "non-number")))
============================================================
(for* ([x (in-list (list 1 2 'a 3 'b 4))]
#:unless (number? x))
(displayln x))
(displayln x)
(displayln "non-number"))
------------------------------------------------------------


no-change-test: "(unless ...) with single body form in a for* loop not refactored"
------------------------------------------------------------
(for* ([x (in-list (list 1 2 'a 3 'b 4))])
(unless (number? x)
(displayln x)))
------------------------------------------------------------


Expand Down
2 changes: 2 additions & 0 deletions default-recommendations/loops/for-loop-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,15 @@ return just that result."
#:description "Use the `#:when` keyword instead of `when` to reduce loop body indentation."
#:literals (when for for*)
((~or for-id:for for-id:for*) (clause ...) (when condition body ...))
#:when (>= (length (attribute body)) 2)
(for-id (clause ... #:when condition) body ...))


(define-refactoring-rule unless-expression-in-for-loop-to-unless-keyword
#:description "Use the `#:unless` keyword instead of `unless` to reduce loop body indentation."
#:literals (unless for for*)
((~or for-id:for for-id:for*) (clause ...) (unless condition body ...))
#:when (>= (length (attribute body)) 2)
(for-id (clause ... #:unless condition) body ...))


Expand Down