Skip to content

Commit 07ca7ac

Browse files
authored
Add tail-sharing-cond-to-when rule (#618)
1 parent 67cea13 commit 07ca7ac

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,3 +1088,32 @@ test: "cond in ignored context with single clause refactorable"
10881088
[else (void)])
10891089
42)
10901090
--------------------
1091+
1092+
1093+
test: "cond with shared tail expression refactorable to when"
1094+
--------------------
1095+
(define (f c1)
1096+
(cond
1097+
[c1
1098+
(displayln "condition 1")
1099+
(displayln "shared tail")]
1100+
[else
1101+
(displayln "shared tail")]))
1102+
====================
1103+
(define (f c1)
1104+
(when c1
1105+
(displayln "condition 1"))
1106+
(displayln "shared tail"))
1107+
--------------------
1108+
1109+
1110+
no-change-test: "cond with unshared tail expression not refactorable to when"
1111+
--------------------
1112+
(define (f c1)
1113+
(cond
1114+
[c1
1115+
(displayln "condition 1")
1116+
(displayln "true tail")]
1117+
[else
1118+
(displayln "false tail")]))
1119+
--------------------

default-recommendations/conditional-shortcuts.rkt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
resyntax/default-recommendations/private/exception
1717
resyntax/default-recommendations/private/let-binding
1818
resyntax/default-recommendations/private/metafunction
19+
resyntax/default-recommendations/private/syntax-equivalence
1920
syntax/parse)
2021

2122

@@ -276,6 +277,21 @@
276277
(cond-id clause ... [else (void)]))
277278

278279

280+
(define-definition-context-refactoring-rule tail-sharing-cond-to-when
281+
#:description "Both branches of this `cond` expression share the same tail expression. Moving that \
282+
tail expression outside `cond` lets you replace `cond` with `when`."
283+
#:literals (cond else)
284+
(~seq body-before ...
285+
(cond [condition:expr true-body ... true-tail]
286+
[else false-tail])
287+
body-after ...)
288+
#:when (syntax-free-identifier=? (attribute true-tail) (attribute false-tail))
289+
(body-before ...
290+
(when condition true-body ...)
291+
true-tail
292+
body-after ...))
293+
294+
279295
(define-refactoring-suite conditional-shortcuts
280296
#:rules (always-throwing-cond-to-when
281297
always-throwing-if-to-when
@@ -292,4 +308,5 @@
292308
ignored-and-to-when
293309
nested-if-to-cond
294310
nested-when-to-compound-when
311+
tail-sharing-cond-to-when
295312
throw-unless-truthy-to-or))

default-recommendations/private/syntax-equivalence.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
(define other-datum (syntax-e other-stx))
2828
(match datum
2929
[(? symbol?) (and (symbol? other-datum) (free-identifier=? stx other-stx))]
30-
[(? number? boolean? string?) (equal? datum other-datum)]
30+
[(or (? number?) (? boolean?) (? string?)) (equal? datum other-datum)]
3131
[(? list?) (and (list? other-datum) (syntax-pair-free-identifier=? datum other-datum))]
3232
[(? box?) (and (box? other-datum) (syntax-free-identifier=? (unbox datum) (unbox other-datum)))]
3333
[(? vector?)

0 commit comments

Comments
 (0)