Skip to content

Commit c0d1b27

Browse files
authored
Make if-else-false-to-and avoid redundant and (#468)
1 parent 919057b commit c0d1b27

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

default-recommendations/conditional-shortcuts-test.rkt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ test: "multi-line if else false can be refactored to a multi-line and expression
6262
------------------------------
6363

6464

65+
test: "if and else false can be refactored to a single and expression"
66+
- (if (and 'a 'b) 'c #f)
67+
- (and 'a 'b 'c)
68+
69+
6570
test: "if x else x can be refactored to an and expression"
6671
------------------------------
6772
(define x 'a)
@@ -87,6 +92,17 @@ test: "multi-line if x else x can be refactored to a multi-line and expression"
8792
------------------------------
8893

8994

95+
test: "if x then and expression else x can be refactored to a single and expression"
96+
------------------------------
97+
(define x 'a)
98+
(if x (and 'b 'c 'd) x)
99+
------------------------------
100+
------------------------------
101+
(define x 'a)
102+
(and x 'b 'c 'd)
103+
------------------------------
104+
105+
90106
test: "if expressions can be refactored to when expressions when equivalent"
91107
------------------------------
92108
(if #true

default-recommendations/conditional-shortcuts.rkt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@
4949

5050
(define-refactoring-rule if-else-false-to-and
5151
#:description equivalent-conditional-description
52-
#:literals (if)
53-
(if condition then-branch #false)
54-
(and condition then-branch))
52+
#:literals (if and)
53+
(if (~or (and condition-part:expr ...) condition:expr) then-branch #false)
54+
(and (~? (~@ condition-part ...) condition) then-branch))
5555

5656

5757
(define-refactoring-rule if-x-else-x-to-and
5858
#:description equivalent-conditional-description
59-
#:literals (if)
60-
(if x:id then-branch:expr y:id)
61-
#:when (free-identifier=? #'x #'y)
62-
(and x then-branch))
59+
#:literals (if and)
60+
(if x:id (~or (and then-part ...) then-branch:expr) y:id)
61+
#:when (free-identifier=? (attribute x) (attribute y))
62+
(and x (~? (~@ then-part ...) then-branch)))
6363

6464

6565
(define-syntax-class block-expression

0 commit comments

Comments
 (0)