Skip to content

Add Remove unused parameter code action#2548

Closed
william-laverty wants to merge 1 commit intoswiftlang:mainfrom
william-laverty:remove-unused-parameter-code-action
Closed

Add Remove unused parameter code action#2548
william-laverty wants to merge 1 commit intoswiftlang:mainfrom
william-laverty:remove-unused-parameter-code-action

Conversation

@william-laverty
Copy link
Copy Markdown

Description

Add a syntactic code action that removes a function parameter not referenced in the function body and updates call sites within the same file.

Changes

  • New RemoveUnusedParameter code action in Sources/SwiftLanguageService/CodeActions/
  • Registered in allSyntaxCodeActions
  • Tests for removal with call site updates and non-offering for used parameters

Behavior

The action is offered when the cursor is on a parameter in a function declaration where:

  • The parameter's local name is not _
  • The function has a body
  • The parameter name is not referenced anywhere in the function body

Call site updates:

  • Finds function calls by matching the function name and argument label at the expected position
  • Handles removal of first, middle, and last arguments correctly (including comma cleanup)
  • Only updates call sites in the same file (syntactic limitation)

Before:

func greet(name: String, title: String) {
    print("Hello, \\(name)")
}

greet(name: "Alice", title: "Ms.")

After:

func greet(name: String) {
    print("Hello, \\(name)")
}

greet(name: "Alice")

Resolves #2513

Add a syntactic code action that removes a function parameter that is
not referenced in the function body and updates call sites within the
same file.

The action is offered when the cursor is on a parameter in a function
declaration and that parameter's local name is not referenced anywhere
in the function body.

Call site detection matches function calls by name and argument label
at the expected position, removing the corresponding argument.

Resolves #2513
@ahoppen
Copy link
Copy Markdown
Member

ahoppen commented Mar 8, 2026

Same here, there’s already a PR that addresses this: #2530

@ahoppen ahoppen closed this Mar 8, 2026
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.

Add Remove unused parameter code action

2 participants