Skip to content

Conversation

Copy link

Copilot AI commented Feb 3, 2026

Replaces verbose manual checks for removing trailing empty arguments with a single-line extension method.

Implementation

Added removeLastTwoIfEmpty() to extensions.swift:

extension Array where Element == String {
    mutating func removeLastTwoIfEmpty() {
        let count = self.count
        guard count >= 2,
              self[count - 1].isEmpty,
              self[count - 2].isEmpty else {
            return
        }
        removeLast(2)
    }
}

Usage

Before:

var arguments = ["--verbose", "--archive", "", ""]
let count = arguments.count
if count >= 2 {
    if arguments[count - 1].isEmpty, arguments[count - 2].isEmpty {
        arguments.removeLast()
        arguments.removeLast()
    }
}

After:

var arguments = ["--verbose", "--archive", "", ""]
arguments.removeLastTwoIfEmpty()

Bounds-checked, type-safe, and available project-wide.

Original prompt

how can I easily remove the two last argumenst from an array: if arguments[count - 1].isEmpty, arguments[count - 2].isEmpty {

            }

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@rsyncOSX rsyncOSX closed this Feb 3, 2026
@rsyncOSX rsyncOSX deleted the copilot/remove-last-two-empty-arguments branch February 3, 2026 09:58
Copilot AI restored the copilot/remove-last-two-empty-arguments branch February 3, 2026 10:01
Copilot AI changed the title [WIP] Remove last two arguments if both are empty Add Array.removeLastTwoIfEmpty() extension for String arrays Feb 3, 2026
Copilot AI requested a review from rsyncOSX February 3, 2026 10:06
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.

2 participants