Skip to content

Add Convert Bool to enum code action #2518

Description

@Tabonx

Description

Replace a boolean variable/parameter with a two-case enum for better readability.

Before:

func configure(animated: Bool) {
    if animated {
        fadeIn()
    } else {
        show()
    }
}

configure(animated: true)

After:

enum AnimationStyle {
    case animated
    case immediate
}

func configure(animation: AnimationStyle) {
    if animation == .animated {
        fadeIn()
    } else {
        show()
    }
}

configure(animation: .animated)

See also: rust-analyzer convert_bool_to_enum

Activity

  1. added theissue type on Feb 28, 2026
  2. ahoppen commented on Mar 2, 2026

    @ahoppen
    Member

    We’ll need to design the user interaction to name the enum and the cases as I don’t think there’s any LSP way of doing this right now. Ideally, we’d be able to use LSP snippets here that bind multiple locations to the same placeholder but I think code actions don’t support snippets…

    We should be able to find all locations where the function is called based on the semantic index but finding the parameter index would likely require swiftlang/swift-syntax#2047.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    code actionCode action provided by LSP

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions