Skip to content

Commit d34ea09

Browse files
Copilotjackfirth
andcommitted
Add guards to prevent rule conflicts instead of relying on ordering
Co-authored-by: jackfirth <[email protected]>
1 parent f700806 commit d34ea09

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

default-recommendations/conditional-shortcuts.rkt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,18 @@
9191
#:attributes (negated? condition [body 1])
9292
#:literals (cond void not begin let)
9393

94-
(pattern (cond [(not condition) (void)] [else :block-expression]) #:with negated? #false)
95-
(pattern (cond [(not condition) :block-expression] [else (void)]) #:with negated? #true)
96-
(pattern (cond [condition (void)] [else :block-expression]) #:with negated? #true)
97-
(pattern (cond [condition :block-expression] [else (void)]) #:with negated? #false))
94+
(pattern (cond [(not condition) (void)] [else block-expr:block-expression])
95+
#:with negated? #false
96+
#:with (body ...) #'(block-expr.body ...))
97+
(pattern (cond [(not condition) block-expr:block-expression] [else (void)])
98+
#:with negated? #true
99+
#:with (body ...) #'(block-expr.body ...))
100+
(pattern (cond [condition (void)] [else block-expr:block-expression])
101+
#:with negated? #true
102+
#:with (body ...) #'(block-expr.body ...))
103+
(pattern (cond [condition block-expr:block-expression] [else (void)])
104+
#:with negated? #false
105+
#:with (body ...) #'(block-expr.body ...)))
98106

99107

100108
(define-refactoring-rule cond-void-to-when-or-unless
@@ -257,18 +265,32 @@ tail expression outside `cond` lets you replace `cond` with `when`."
257265

258266
(define-refactoring-rule cond-begin-to-cond
259267
#:description "The bodies of `cond` clauses are already implicitly wrapped in `begin`."
260-
#:literals (cond begin)
268+
#:literals (cond begin else void)
261269
(cond clause-before ... [condition (begin body ...)] clause-after ...)
270+
;; Don't match if this is the else clause of a cond that could be converted to when/unless
271+
#:when (not (and (empty? (attribute clause-after))
272+
(= (length (attribute clause-before)) 1)
273+
(syntax-parse (first (attribute clause-before))
274+
#:literals (void not)
275+
[(~or [_ (void)]
276+
[(not _) (void)]) #true]
277+
[_ #false])))
278+
;; Also don't match if the clause after is [else (void)]
279+
#:when (or (empty? (attribute clause-after))
280+
(syntax-parse #'(clause-after ...)
281+
#:literals (else void)
282+
[(~not ([else (void)])) #true]
283+
[_ #false]))
262284
(cond clause-before ... [condition body ...] clause-after ...))
263285

264286

265287
(define-refactoring-suite conditional-shortcuts
266288
#:rules (always-throwing-cond-to-when
267289
always-throwing-if-to-when
290+
cond-begin-to-cond
268291
cond-else-cond-to-cond
269292
cond-else-if-to-cond
270293
cond-void-to-when-or-unless
271-
cond-begin-to-cond
272294
explicit-cond-else-void
273295
if-begin-to-cond
274296
if-else-cond-to-cond

0 commit comments

Comments
 (0)