diff --git a/default-recommendations/exception-suggestions-test.rkt b/default-recommendations/exception-suggestions-test.rkt index 81eb2d1..ef44b33 100644 --- a/default-recommendations/exception-suggestions-test.rkt +++ b/default-recommendations/exception-suggestions-test.rkt @@ -153,3 +153,18 @@ test: "error with ~a at end of string" (define (end-tilde x) (raise-arguments-error 'end-tilde "value is" "x" x)) -------------------- + + +no-change-test: "error with ~a in quotes should not be refactored" +-------------------- +(define (find-output-radio-box label) + (error 'find-output-radio-box "could not find `~a' radio box" + label)) +-------------------- + + +no-change-test: "error with ~a in parentheses should not be refactored" +-------------------- +(define (f x y) + (error 'f "bad point (~a, ~a)" x y)) +-------------------- diff --git a/default-recommendations/exception-suggestions.rkt b/default-recommendations/exception-suggestions.rkt index 98aa585..c125acd 100644 --- a/default-recommendations/exception-suggestions.rkt +++ b/default-recommendations/exception-suggestions.rkt @@ -43,8 +43,18 @@ conventions." (error sym:expr message:str arg:id ...+) #:do [(define message-str (syntax-e (attribute message))) - (define args-list (attribute arg))] - #:when (= (length (regexp-match* #rx"~a" message-str)) (length args-list)) + (define args-list (attribute arg)) + (define tilde-a-matches (regexp-match-positions* #rx"~a" message-str))] + #:when (= (length tilde-a-matches) (length args-list)) + ;; Check that all ~a occurrences are surrounded by spaces or at string boundaries + #:when (for/and ([match (in-list tilde-a-matches)]) + (define start (car match)) + (define end (cdr match)) + (define before-ok? (or (= start 0) + (char-whitespace? (string-ref message-str (- start 1))))) + (define after-ok? (or (= end (string-length message-str)) + (char-whitespace? (string-ref message-str end)))) + (and before-ok? after-ok?)) #:do [(define cleaned-message (string-replace message-str "~a" "")) ;; Clean up extra spaces and trailing punctuation from placeholder removal (define cleaned-message-normalized