Skip to content

Commit 8b389b8

Browse files
Copilotjackfirth
andauthored
Move if-let-to-cond and and-let-to-cond rules to let-replacement (#659)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]>
1 parent b5ea366 commit 8b389b8

File tree

5 files changed

+315
-300
lines changed

5 files changed

+315
-300
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 0 additions & 254 deletions
Original file line numberDiff line numberDiff line change
@@ -698,260 +698,6 @@ test: "if clause with begin in false branch and commented true branch refactorab
698698
------------------------------
699699

700700

701-
test: "if clause with let in true branch refactorable to cond"
702-
------------------------------
703-
(define (f a b)
704-
(if a
705-
(let ([x 1])
706-
x)
707-
b))
708-
==============================
709-
(define (f a b)
710-
(cond
711-
[a
712-
(define x 1)
713-
x]
714-
[else b]))
715-
------------------------------
716-
717-
718-
test: "if clause with let in false branch refactorable to cond"
719-
------------------------------
720-
(define (f a b)
721-
(if a
722-
b
723-
(let ([x 1])
724-
x)))
725-
==============================
726-
(define (f a b)
727-
(cond
728-
[a b]
729-
[else
730-
(define x 1)
731-
x]))
732-
------------------------------
733-
734-
735-
test: "if clause with let in both branches refactorable to cond"
736-
------------------------------
737-
(define (f a)
738-
(if a
739-
(let ([x 1])
740-
x)
741-
(let ([x 1])
742-
x)))
743-
==============================
744-
(define (f a)
745-
(cond
746-
[a
747-
(define x 1)
748-
x]
749-
[else
750-
(define x 1)
751-
x]))
752-
------------------------------
753-
754-
755-
test: "if clause with multiline condition and let in true branch refactorable to cond"
756-
------------------------------
757-
(define (f a b)
758-
(if (a 10000000000000000000000000000000000000
759-
20000000000000000000000000000000000000
760-
30000000000000000000000000000000000000)
761-
(let ([x 1])
762-
x)
763-
b))
764-
==============================
765-
(define (f a b)
766-
(cond
767-
[(a 10000000000000000000000000000000000000
768-
20000000000000000000000000000000000000
769-
30000000000000000000000000000000000000)
770-
(define x 1)
771-
x]
772-
[else b]))
773-
------------------------------
774-
775-
776-
test: "if clause with multiline condition and let in false branch refactorable to cond"
777-
------------------------------
778-
(define (f a b)
779-
(if (a 10000000000000000000000000000000000000
780-
20000000000000000000000000000000000000
781-
30000000000000000000000000000000000000)
782-
b
783-
(let ([x 1])
784-
x)))
785-
==============================
786-
(define (f a b)
787-
(cond
788-
[(a 10000000000000000000000000000000000000
789-
20000000000000000000000000000000000000
790-
30000000000000000000000000000000000000)
791-
b]
792-
[else
793-
(define x 1)
794-
x]))
795-
------------------------------
796-
797-
798-
test: "if clause with multiline condition and let in both branches refactorable to cond"
799-
------------------------------
800-
(define (f a)
801-
(if (a 10000000000000000000000000000000000000
802-
20000000000000000000000000000000000000
803-
30000000000000000000000000000000000000)
804-
(let ([x 1])
805-
x)
806-
(let ([x 1])
807-
x)))
808-
==============================
809-
(define (f a)
810-
(cond
811-
[(a 10000000000000000000000000000000000000
812-
20000000000000000000000000000000000000
813-
30000000000000000000000000000000000000)
814-
(define x 1)
815-
x]
816-
[else
817-
(define x 1)
818-
x]))
819-
------------------------------
820-
821-
822-
test: "if clause with let in commented true branch refactorable to cond"
823-
------------------------------
824-
(define (f a b)
825-
(if a
826-
;; This is the true case
827-
(let ([x 1])
828-
x)
829-
b))
830-
==============================
831-
(define (f a b)
832-
(cond
833-
[a
834-
;; This is the true case
835-
(define x 1)
836-
x]
837-
[else b]))
838-
------------------------------
839-
840-
841-
test: "if clause with let in commented false branch refactorable to cond"
842-
------------------------------
843-
(define (f a b)
844-
(if a
845-
b
846-
;; This is the false case
847-
(let ([x 1])
848-
x)))
849-
==============================
850-
(define (f a b)
851-
(cond
852-
[a b]
853-
;; This is the false case
854-
[else
855-
(define x 1)
856-
x]))
857-
------------------------------
858-
859-
860-
test: "if clause with let in both commented branches refactorable to cond"
861-
------------------------------
862-
(define (f a)
863-
(if a
864-
;; This is the true case
865-
(let ([x 1])
866-
x)
867-
;; This is the false case
868-
(let ([x 1])
869-
x)))
870-
==============================
871-
(define (f a)
872-
(cond
873-
[a
874-
;; This is the true case
875-
(define x 1)
876-
x]
877-
;; This is the false case
878-
[else
879-
(define x 1)
880-
x]))
881-
------------------------------
882-
883-
884-
885-
test: "if clause with let in true branch and commented false branch refactorable to cond"
886-
------------------------------
887-
(define (f a b)
888-
(if a
889-
(let ([x 1])
890-
x)
891-
;; This is the false case
892-
b))
893-
==============================
894-
(define (f a b)
895-
(cond
896-
[a
897-
(define x 1)
898-
x]
899-
;; This is the false case
900-
[else b]))
901-
------------------------------
902-
903-
904-
test: "if clause with let in false branch and commented true branch refactorable to cond"
905-
------------------------------
906-
(define (f a b)
907-
(if a
908-
;; This is the true case
909-
b
910-
(let ([x 1])
911-
x)))
912-
==============================
913-
(define (f a b)
914-
(cond
915-
;; This is the true case
916-
[a b]
917-
[else
918-
(define x 1)
919-
x]))
920-
------------------------------
921-
922-
923-
test: "and with let can be refactored to cond with define"
924-
------------------------------
925-
(and 'some-condition
926-
(let ([x 42])
927-
(* x 2)))
928-
==============================
929-
(cond
930-
['some-condition
931-
(define x 42)
932-
(* x 2)]
933-
[else #f])
934-
------------------------------
935-
936-
937-
no-change-test: "and without let should not be refactored"
938-
------------------------------
939-
(and 'some-condition (* 42 2))
940-
------------------------------
941-
942-
943-
no-change-test: "and with empty let should not be refactored"
944-
------------------------------
945-
(and 'some-condition (let () (* 42 2)))
946-
------------------------------
947-
948-
949-
no-change-test: "and with more than two arguments should not be refactored"
950-
------------------------------
951-
(and 'some-condition 'another-condition (let ([x 42]) (* x 2)))
952-
------------------------------
953-
954-
955701
test: "immediately-nested when expressions can be merged"
956702
--------------------
957703
(define (f c1 c2)

default-recommendations/conditional-shortcuts.rkt

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
resyntax/base
1515
resyntax/default-recommendations/private/boolean
1616
resyntax/default-recommendations/private/exception
17-
resyntax/default-recommendations/let-replacement/private/let-binding
17+
resyntax/default-recommendations/private/if-arm
1818
resyntax/default-recommendations/private/metafunction
1919
resyntax/default-recommendations/private/syntax-equivalence
2020
syntax/parse)
@@ -185,25 +185,6 @@
185185

186186

187187

188-
(define-syntax-class if-arm
189-
#:attributes (uses-begin? uses-let? [refactored 1])
190-
#:literals (begin)
191-
192-
(pattern (begin body ...)
193-
#:attr uses-begin? #true
194-
#:attr uses-let? #false
195-
#:with (refactored ...) #`(~splicing-replacement (body ...) #:original #,this-syntax))
196-
197-
(pattern :refactorable-let-expression
198-
#:attr uses-begin? #false
199-
#:attr uses-let? #true)
200-
201-
(pattern other
202-
#:with (refactored ...) #'(other)
203-
#:attr uses-begin? #false
204-
#:attr uses-let? #false))
205-
206-
207188
(define-refactoring-rule if-begin-to-cond
208189
#:description "Using `cond` instead of `if` here makes `begin` unnecessary"
209190
#:literals (if void)
@@ -215,28 +196,6 @@
215196
(~replacement [else else-expr.refactored ...] #:original else-expr)))
216197

217198

218-
(define-refactoring-rule if-let-to-cond
219-
#:description
220-
"`cond` with internal definitions is preferred over `if` with `let`, to reduce nesting"
221-
#:literals (if void)
222-
(if condition
223-
(~and then-expr:if-arm (~not (void)))
224-
(~and else-expr:if-arm (~not (void))))
225-
#:when (or (attribute then-expr.uses-let?) (attribute else-expr.uses-let?))
226-
(cond (~replacement [condition then-expr.refactored ...] #:original-splice (condition then-expr))
227-
(~replacement [else else-expr.refactored ...] #:original else-expr)))
228-
229-
230-
(define-refactoring-rule and-let-to-cond
231-
#:description
232-
"Using `cond` allows converting `let` to internal definitions, reducing nesting"
233-
#:literals (and cond)
234-
(and condition let-expr:refactorable-let-expression)
235-
#:when (not (empty? (attribute let-expr.id)))
236-
(cond [condition let-expr.refactored ...]
237-
[else #false]))
238-
239-
240199
(define-refactoring-rule nested-when-to-compound-when
241200
#:description
242201
"Nested `when` expressions can be merged into a single compound `when` expression."
@@ -280,13 +239,11 @@ tail expression outside `cond` lets you replace `cond` with `when`."
280239
(define-refactoring-suite conditional-shortcuts
281240
#:rules (always-throwing-cond-to-when
282241
always-throwing-if-to-when
283-
and-let-to-cond
284242
cond-else-cond-to-cond
285243
cond-void-to-when-or-unless
286244
explicit-cond-else-void
287245
if-begin-to-cond
288246
if-else-false-to-and
289-
if-let-to-cond
290247
if-void-to-when-or-unless
291248
if-x-else-x-to-and
292249
ignored-and-to-when

0 commit comments

Comments
 (0)