Skip to content

Commit 824a5d1

Browse files
Automated Resyntax fixes (#1450)
* Fix 35 occurrences of `single-clause-match-to-match-define` This `match` expression can be simplified using `match-define`. * Fix 1 occurrence of `map-to-for` This `map` operation can be replaced with a `for/list` loop. * Fix 7 occurrences of `let-to-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Fix 2 occurrences of `inline-unnecessary-define` This variable is returned immediately and can be inlined. * Fix 1 occurrence of `if-else-false-to-and` This `if` expression can be refactored to an equivalent expression using `and`. * Fix 1 occurrence of `cond-else-if-to-cond` The `else`-`if` branch of this `cond` expression can be collapsed into the `cond` expression. * Fix 2 occurrences of `define-let-to-double-define` This `let` expression can be pulled up into a `define` expression. * Fix 1 occurrence of `zero-comparison-to-negative?` This expression is equivalent to calling the `negative?` predicate. --------- Co-authored-by: resyntax-ci[bot] <181813515+resyntax-ci[bot]@users.noreply.github.com>
1 parent c14115c commit 824a5d1

File tree

18 files changed

+266
-331
lines changed

18 files changed

+266
-331
lines changed

typed-racket-lib/typed-racket/env/global-env.rkt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
#:when (attribute type)))
7575

7676
(define (maybe-finish-register-type id)
77-
(let ([v (free-id-table-ref the-mapping id)])
78-
(if (box? v)
79-
(register-type id (unbox v))
80-
#f)))
77+
(define v (free-id-table-ref the-mapping id))
78+
(if (box? v)
79+
(register-type id (unbox v))
80+
#f))
8181

8282
(define (unregister-type id)
8383
(free-id-table-remove! the-mapping id))
@@ -91,13 +91,15 @@
9191
the-mapping
9292
(lambda (id e)
9393
(when (box? e)
94-
(let ([bnd (identifier-binding id)])
95-
(tc-error/delayed #:stx id
96-
"Declaration for `~a' provided, but `~a' ~a"
97-
(syntax-e id) (syntax-e id)
98-
(cond [(eq? bnd 'lexical) "is a lexical binding"] ;; should never happen
99-
[(not bnd) "has no definition"]
100-
[else "is defined in another module"])))))))
94+
(define bnd (identifier-binding id))
95+
(tc-error/delayed #:stx id
96+
"Declaration for `~a' provided, but `~a' ~a"
97+
(syntax-e id)
98+
(syntax-e id)
99+
(cond
100+
[(eq? bnd 'lexical) "is a lexical binding"] ;; should never happen
101+
[(not bnd) "has no definition"]
102+
[else "is defined in another module"]))))))
101103

102104
;; map over the-mapping, producing a list
103105
;; (id type -> T) -> listof[T]

typed-racket-lib/typed-racket/env/init-envs.rkt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,10 @@
331331
[(Instance: ty) `(make-Instance ,(type->sexp ty))]
332332
[(Signature: name extends mapping)
333333
(define (serialize-mapping m)
334-
(map (lambda (id/ty)
335-
(define id (car id/ty))
336-
(define ty (force (cdr id/ty)))
337-
`(cons (quote-syntax ,id) ,(type->sexp ty)))
338-
m))
334+
(for/list ([id/ty (in-list m)])
335+
(define id (car id/ty))
336+
(define ty (force (cdr id/ty)))
337+
`(cons (quote-syntax ,id) ,(type->sexp ty))))
339338
(define serialized-extends (and extends `(quote-syntax ,extends)))
340339
`(make-Signature (quote-syntax ,name)
341340
,serialized-extends
@@ -437,11 +436,11 @@
437436
`(make-PrefabPE (quote ,key) ,idx)]))
438437

439438
(define (bound-in-this-module id)
440-
(let ([binding (identifier-binding id)])
441-
(if (and (list? binding) (module-path-index? (car binding)))
442-
(let-values ([(mp base) (module-path-index-split (car binding))])
443-
(not mp))
444-
#f)))
439+
(define binding (identifier-binding id))
440+
(if (and (list? binding) (module-path-index? (car binding)))
441+
(let-values ([(mp base) (module-path-index-split (car binding))])
442+
(not mp))
443+
#f))
445444

446445
(define (make-init-code map f)
447446
(define (bound-f id v)

typed-racket-lib/typed-racket/env/type-alias-env.rkt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@
8080
(match v
8181
[(struct unresolved (stx _ persistent?))
8282
(set-unresolved-in-process! v #t)
83-
(let ([t (parse-type stx)])
84-
(when persistent?
85-
(mapping-put! id (make-resolved t)))
86-
t)]
83+
(define t (parse-type stx))
84+
(when persistent?
85+
(mapping-put! id (make-resolved t)))
86+
t]
8787
[(struct resolved (t))
8888
t]))
8989

typed-racket-lib/typed-racket/private/shallow-rewrite.rkt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@
239239
"#%plain-lambda formals"
240240
#'formals
241241
args)]))
242-
(define check*
243-
(let ([dom+ (for/fold ([acc '()])
244-
([dom (in-list dom*)]
245-
#:when (pair? dom))
246-
(cons (cdr dom) acc))])
247-
(protect-loop rst dom+)))
242+
(define dom+
243+
(for/fold ([acc '()])
244+
([dom (in-list dom*)]
245+
#:when (pair? dom))
246+
(cons (cdr dom) acc)))
247+
(define check* (protect-loop rst dom+))
248248
(define ann-ty
249249
(and (type-annotation fst #:infer #f)
250250
(get-type fst #:infer #t #:default Univ)))

typed-racket-lib/typed-racket/rep/object-rep.rkt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,12 @@
272272
(if (Empty? nm)
273273
(values c ts)
274274
(values c (terms-set ts p (+ coeff (terms-ref ts p)))))]
275-
[(? exact-integer? new-const)
276-
(values (+ new-const c) ts)]
277-
[(LExp: c* ts*)
278-
(values (+ c c*) (add-terms ts ts*))]
275+
[(? exact-integer? new-const) (values (+ new-const c) ts)]
276+
[(LExp: c* ts*) (values (+ c c*) (add-terms ts ts*))]
279277
[(list (? exact-integer? l-coeff) (? LExp? l))
280-
(match (scale-obj l-coeff l)
281-
[(LExp: c* ts*)
282-
(values (+ c c*) (add-terms ts ts*))])]
283-
[(? Object? p)
284-
(values c (terms-set ts p (add1 (terms-ref ts p))))]
278+
(match-define (LExp: c* ts*) (scale-obj l-coeff l))
279+
(values (+ c c*) (add-terms ts ts*))]
280+
[(? Object? p) (values c (terms-set ts p (add1 (terms-ref ts p))))]
285281
[(? name-ref/c var)
286282
(define p (-id-path var))
287283
(values c (terms-set ts p (add1 (terms-ref ts p))))])))

typed-racket-lib/typed-racket/static-contracts/combinators/case-lambda.rkt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@
4545
(arr-seq-sc-map f (combinator-args v))
4646
(void))
4747
(define (sc->contract v f)
48-
(match v
49-
[(arr-combinator (arr-seq args rest range))
50-
(with-syntax ([(arg-stx ...) (map f args)]
51-
[(rest-stx ...) (if rest #`(#:rest #,(f rest)) #'())]
52-
[range-stx (if range #`(values #,@(map f range)) #'any)])
53-
#'(arg-stx ... rest-stx ... . -> . range-stx))]))
48+
(match-define (arr-combinator (arr-seq args rest range)) v)
49+
(with-syntax ([(arg-stx ...) (map f args)]
50+
[(rest-stx ...) (if rest
51+
#`(#:rest #,(f rest))
52+
#'())]
53+
[range-stx (if range
54+
#`(values #,@(map f range))
55+
#'any)])
56+
#'(arg-stx ... rest-stx ... . -> . range-stx)))
5457
(define (sc->constraints v f)
5558
(merge-restricts* 'chaperone (map f (arr-seq->list (combinator-args v)))))])
5659

@@ -66,20 +69,18 @@
6669

6770

6871
(define (arr-seq-sc-map f seq)
69-
(match seq
70-
[(arr-seq args rest range)
71-
(arr-seq
72-
(map (λ (a) (f a 'contravariant)) args)
73-
(and rest (f rest 'contravariant))
74-
(and range (map (λ (a) (f a 'covariant)) range)))]))
72+
(match-define (arr-seq args rest range) seq)
73+
(arr-seq (map (λ (a) (f a 'contravariant)) args)
74+
(and rest (f rest 'contravariant))
75+
(and range (map (λ (a) (f a 'covariant)) range))))
7576

7677
(define (arr-seq->list seq)
77-
(match seq
78-
[(arr-seq args rest range)
79-
(append
80-
args
81-
(if rest (list rest) empty)
82-
(or range empty))]))
78+
(match-define (arr-seq args rest range) seq)
79+
(append args
80+
(if rest
81+
(list rest)
82+
empty)
83+
(or range empty)))
8384

8485

8586
(struct arr-seq (args rest range)

typed-racket-lib/typed-racket/static-contracts/combinators/control.rkt

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424
(pt-seq-map f (combinator-args v))
2525
(void))
2626
(define (sc->contract v f)
27-
(match v
28-
[(prompt-tag-combinator (pt-seq vals call-cc))
29-
(with-syntax ([(vals-stx ...) (map f vals)]
30-
[(call-cc-stx ...)
31-
(if call-cc
32-
#`(#:call/cc (values #,@(map f call-cc)))
33-
empty)])
34-
#'(prompt-tag/c vals-stx ... call-cc-stx ...))]))
27+
(match-define (prompt-tag-combinator (pt-seq vals call-cc)) v)
28+
(with-syntax ([(vals-stx ...) (map f vals)]
29+
[(call-cc-stx ...) (if call-cc
30+
#`(#:call/cc (values #,@(map f call-cc)))
31+
empty)])
32+
#'(prompt-tag/c vals-stx ... call-cc-stx ...)))
3533
(define (sc->constraints v f)
3634
(merge-restricts* 'chaperone (map f (pt-seq->list (combinator-args v)))))])
3735

@@ -52,16 +50,11 @@
5250

5351

5452
(define (pt-seq-map f seq)
55-
(match seq
56-
[(pt-seq vals call-cc)
57-
(define (f* a) (f a 'invariant))
58-
(pt-seq
59-
(map f* vals)
60-
(and call-cc (map f* call-cc)))]))
53+
(match-define (pt-seq vals call-cc) seq)
54+
(define (f* a)
55+
(f a 'invariant))
56+
(pt-seq (map f* vals) (and call-cc (map f* call-cc))))
6157

6258
(define (pt-seq->list seq)
63-
(match seq
64-
[(pt-seq vals call-cc)
65-
(append
66-
vals
67-
(or call-cc empty))]))
59+
(match-define (pt-seq vals call-cc) seq)
60+
(append vals (or call-cc empty)))

typed-racket-lib/typed-racket/static-contracts/combinators/exist.rkt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
(define (sc->contract v f)
3030
(match-define (exist-combinator (list names doms rngs)) v)
3131
(parameterize ([static-contract-may-contain-free-ids? #t])
32-
(define a
33-
(with-syntax ([doms-stx (f doms)]
34-
[rngs-stx (f rngs)]
35-
[n (car names)])
36-
#'(->i ([n doms-stx]) (_ (n) rngs-stx))))
37-
a))
32+
(with-syntax ([doms-stx (f doms)]
33+
[rngs-stx (f rngs)]
34+
[n (car names)])
35+
#'(->i ([n doms-stx]) (_ (n) rngs-stx)))))
3836
(define (sc->constraints v f)
3937
(simple-contract-restrict 'flat))])
4038

typed-racket-lib/typed-racket/static-contracts/combinators/function.rkt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,10 @@
146146
(match-define (function-combinator args indices mand-kws opt-kws typed-side?) v)
147147
(define-values (mand-args opt-args mand-kw-args opt-kw-args rest-arg range-args)
148148
(apply split-function-args args indices))
149-
(if (and (not rest-arg)
150-
(null? (append mand-kw-args mand-args opt-kw-args opt-args))
151-
typed-side?)
152-
;; currently we only handle this trivial case
153-
;; we could probably look at the actual kind of `range-args` as well
154-
(if (not range-args) 'flat #f)
155-
#f))
149+
(and (and (not rest-arg) (null? (append mand-kw-args mand-args opt-kw-args opt-args)) typed-side?)
150+
;; currently we only handle this trivial case
151+
;; we could probably look at the actual kind of `range-args` as well
152+
(if (not range-args) 'flat #f)))
156153

157154

158155
(define (function-sc-constraints v f)

0 commit comments

Comments
 (0)