Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

This PR implements a new refactoring rule that transforms error function calls with format strings into raise-arguments-error calls, helping developers follow Racket's error message conventions.

Problem

The error function is easy to use in ways that violate Racket's error messages conventions. When developers use error with format strings like ~a, they miss the opportunity to provide structured error information that raise-arguments-error offers. For example:

(error 'foo "low should be less than high, ~a ~a" low high)

produces an error message that's harder to parse and less structured than:

(raise-arguments-error 'foo "low should be less than high" "low" low "high" high)

Solution

The new error-to-raise-arguments-error rule automatically detects and refactors these cases. The rule:

  1. Identifies error calls that use ~a format placeholders with simple identifier arguments
  2. Validates that the number of placeholders matches the number of arguments
  3. Removes the format placeholders and cleans up the message string
  4. Extracts identifier names to create field name/value pairs
  5. Generates a properly structured raise-arguments-error call

Example Transformations

Basic case:

(error 'bar "x must be non-negative: ~a" x)
→
(raise-arguments-error 'bar "x must be non-negative" "x" x)

Multiple arguments:

(error 'test-error "values are wrong: ~a ~a ~a" x y z)
→
(raise-arguments-error 'test-error "values are wrong" "x" x "y" y "z" z)

Safety

The rule is conservative and only applies when:

  • All arguments are simple identifiers (not complex expressions)
  • The number of ~a placeholders exactly matches the number of arguments
  • The format string contains only ~a placeholders (not other format directives)

This ensures the transformation is safe and doesn't change program behavior.

Testing

Added comprehensive tests covering:

  • Single and multiple argument transformations
  • Edge cases like ~a at the end of strings
  • No-change scenarios (missing placeholders, mismatched counts, complex expressions)

All 670 tests pass (667 existing + 3 new).

Fixes #[issue number]

Original prompt

This section details on the original issue you should resolve

<issue_title>New rule: error-to-raise-arguments-error</issue_title>
<issue_description>### Rule summary

The error function is easy to use in ways that violate Racket's error messages conventions. Most uses of it would be better off using raise-arguments-error. Resyntax should detect uses of error that are easy to migrate to raise-arguments-error.

Test case

#lang resyntax/test

test: "original code should be refactorable to new code"
--------------------
#lang racket
(define (foo low high)
  (unless (<= low high)
    (error 'foo "low should be less than high, ~a ~a" low high))
  (void))
====================
#lang racket
(define (foo low high)
  (unless (<= low high)
    (raise-arguments-error 'foo "low should be less than high" "low" low "high" high))
  (void))
--------------------

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #604


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add new rule for error to raise-arguments-error migration Add error-to-raise-arguments-error refactoring rule Oct 24, 2025
Copilot AI requested a review from jackfirth October 24, 2025 06:31
Copilot finished work on behalf of jackfirth October 24, 2025 06:31
@jackfirth jackfirth marked this pull request as ready for review October 25, 2025 08:37
@jackfirth jackfirth merged commit 5234155 into master Oct 25, 2025
2 checks passed
@jackfirth jackfirth deleted the copilot/add-error-to-raise-arguments-error-rule branch October 25, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule: error-to-raise-arguments-error

2 participants