Skip to content

Commit 6e4206d

Browse files
Copilotjackfirthgithub-actions[bot]
authored
Add printf-without-specifiers refactoring rule (#709)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]> Co-authored-by: Jacqueline Firth <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 00a81a1 commit 6e4206d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

default-recommendations/console-io-suggestions-test.rkt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ test: "should suggest 'any linemode with read-line when linemode and port not sp
2626
(define (foo)
2727
(read-line (current-input-port) 'any))
2828
----------------------------------------
29+
30+
31+
test: "printf to display"
32+
- (printf "foo")
33+
- (display "foo")
34+
35+
36+
test: "printf to displayln"
37+
- (printf "foo\nbar\n")
38+
- (displayln "foo\nbar")
39+
40+
41+
test: "printf to newline"
42+
- (printf "\n")
43+
- (newline)

default-recommendations/console-io-suggestions.rkt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
(require racket/file
1313
racket/list
14+
racket/string
1415
rebellion/private/static-name
1516
resyntax/base
1617
syntax/parse)
@@ -28,5 +29,29 @@
2829
(read-line (~? port (current-input-port)) 'any))
2930

3031

32+
(define-syntax-class printf-without-specifiers
33+
#:attributes (refactored)
34+
#:literals (printf)
35+
36+
(pattern (printf "\n")
37+
#:with refactored #'(newline))
38+
39+
(pattern (printf s:str)
40+
#:when (string-suffix? (syntax-e #'s) "\n")
41+
#:with stripped (substring (syntax-e #'s) 0 (- (string-length (syntax-e #'s)) 1))
42+
#:with refactored #'(displayln stripped))
43+
44+
(pattern (printf s:str)
45+
#:with refactored #'(display s)))
46+
47+
48+
(define-refactoring-rule printf-to-display
49+
#:description
50+
"This use of `printf` has no arguments other than the template string."
51+
expr:printf-without-specifiers
52+
expr.refactored)
53+
54+
3155
(define-refactoring-suite console-io-suggestions
32-
#:rules (read-line-any))
56+
#:rules (printf-to-display
57+
read-line-any))

0 commit comments

Comments
 (0)