Skip to content
Merged
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
35 changes: 29 additions & 6 deletions default-recommendations/match-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -230,46 +230,69 @@ no-change-test: "root-level and pattern not removed when not matching on a simpl
------------------------------


test: "match patterns with if conditionals can be simplified using #:when clauses"
test: "match patterns with long if conditionals can be simplified using #:when clauses"
------------------------------
(define (f pt)
(match pt
[(list x y)
(if (> x y)
(list y x)
(list y x 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo)
pt)]
[(list) '()]))
==============================
(define (f pt)
(match pt
[(list x y)
#:when (> x y)
(list y x)]
(list y x 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo)]
[(list x y) pt]
[(list) '()]))
------------------------------


test: "match patterns with cond conditionals can be simplified using #:when clauses"
no-change-test: "match patterns with short if conditionals won't be simplified using #:when clauses"
------------------------------
(define (f pt)
(match pt
[(list x y)
(if (> x y)
(list y x)
pt)]
[(list) '()]))
------------------------------


test: "match patterns with long cond conditionals can be simplified using #:when clauses"
------------------------------
(define (f pt)
(match pt
[(list x y)
(cond
[(> x y) (list y x)]
[(> x y) (list y x 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo)]
[else pt])]
[(list) '()]))
==============================
(define (f pt)
(match pt
[(list x y)
#:when (> x y)
(list y x)]
(list y x 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo 'foo)]
[(list x y) pt]
[(list) '()]))
------------------------------


no-change-test: "match patterns with short cond conditionals won't be simplified using #:when clauses"
------------------------------
(define (f pt)
(match pt
[(list x y)
(cond
[(> x y) (list y x)]
[else pt])]
[(list) '()]))
------------------------------

test: "match patterns with multi-body cond conditionals can be simplified using #:when clauses"
------------------------------
(define (f pt)
Expand Down
7 changes: 7 additions & 0 deletions default-recommendations/match-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@
#:when (oneline-syntax? (attribute pattern))
#:when (<= (syntax-span (attribute pattern)) 60)

; Conversely, a conditional with short body expressions isn't worth duplicating the condition
; expression, so we only attempt to refactor conditionals whose body expressions aren't short.
#:when (or (> (length (attribute conditional.then-expr)) 1)
(> (length (attribute conditional.else-expr)) 1)
(>= (syntax-span (first (attribute conditional.then-expr))) 60)
(>= (syntax-span (first (attribute conditional.else-expr))) 60))

(match subject
clause-before ...
(~@ . (~splicing-replacement ([pattern #:when conditional.condition conditional.then-expr ...]
Expand Down