Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve always-throwing-if-to-when rule #257

Merged
merged 1 commit into from
Aug 31, 2024
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
30 changes: 30 additions & 0 deletions default-recommendations/conditional-shortcuts-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ test: "if expressions with an always-throwing first branch can be refactored to
------------------------------


test: "if expressions with an always-throwing second branch can be refactored to unless"
------------------------------
(define (f c)
(if c
(displayln "foo")
(error 'oops)))
------------------------------
------------------------------
(define (f c)
(unless c
(error 'oops))
(displayln "foo"))
------------------------------


test: "negated if expressions with an always-throwing first branch can be refactored to unless"
------------------------------
(define (f c)
Expand All @@ -167,6 +182,21 @@ test: "negated if expressions with an always-throwing first branch can be refact
------------------------------


test: "negated if expressions with an always-throwing second branch can be refactored to when"
------------------------------
(define (f c)
(if (not c)
(displayln "foo")
(error 'oops)))
------------------------------
------------------------------
(define (f c)
(when c
(error 'oops))
(displayln "foo"))
------------------------------


test: "cond expressions with an always-throwing first branch can be refactored to when"
------------------------------
(define (f c)
Expand Down
23 changes: 18 additions & 5 deletions default-recommendations/conditional-shortcuts.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,31 @@
conditional.condition conditional.body ...)])


(define-syntax-class always-throwing-if-expression
#:attributes (equivalent-guard-expression success-expression)
#:literals (if)
(pattern (if condition:condition-expression
fail:always-throwing-expression
success-expression)
#:with equivalent-guard-expression
#'((~if condition.negated? unless when) condition.base-condition fail))
(pattern (if condition:condition-expression
success-expression
fail:always-throwing-expression)
#:with equivalent-guard-expression
#'((~if condition.negated? when unless) condition.base-condition fail)))


(define-refactoring-rule always-throwing-if-to-when
#:description
"Using `when` and `unless` is simpler than a conditional with an always-throwing branch."
#:literals (if)
[(header:header-form-allowing-internal-definitions
(if condition:condition-expression
fail:always-throwing-expression
else-expression))
throwing-if:always-throwing-if-expression)
(header.formatted
...
((~if condition.negated? unless when) condition.base-condition fail)
else-expression)])
throwing-if.equivalent-guard-expression
throwing-if.success-expression)])


(define-refactoring-rule always-throwing-cond-to-when
Expand Down
Loading