Swiftformat: "self" incorrectly stripped from expression inside autoclosure

Created on 14 Nov 2017  路  4Comments  路  Source: nicklockwood/SwiftFormat

My codebase uses Nimble to expand its test assertion vocabulary. Nimble wraps most expectation expressions inside autoclosures, and inside them self is required to make capture semantics explicit. After formatting our test sources some of our test cases stopped compiling.

I understand this is an obscure edge case and that it's unlikely something can be done about it right now, but I'm opening this issue for documentation.

As an example, the following code snippet stops working after being formatted:

func testNonZeroMinutes() {
    let date = Date(timeIntervalSince1970: 1_491_233_400)
    expect(self.formatter.string(from: date)) == "12:30"
}

I'll be glad to provide more context about the issue if needed.

Most helpful comment

This is fixed in 0.30.2. I've just added a special case for expect(), since the Nimble framework is the only place where this issue has arisen so far.

All 4 comments

Hi @fellipecaetano, yes this is a known issue. Unfortunately there's no context available for SwiftFormat to identify autoclosures since they look the same as ordinary arguments. At the moment the only option is to disable the redundantSelf rule.

This is fixed in 0.30.2. I've just added a special case for expect(), since the Nimble framework is the only place where this issue has arisen so far.

We have a custom built framework that uses "@autoclosure". Is there any way to extend this fix for all other use cases?

@LeonidKokhnovich I'm considering adding a customizable whitelist of function names, but this isn't currently implemented.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DagAgren picture DagAgren  路  4Comments

AniHovhannisyanAni picture AniHovhannisyanAni  路  3Comments

meherkasam picture meherkasam  路  4Comments

MaxDesiatov picture MaxDesiatov  路  3Comments

meherkasam picture meherkasam  路  3Comments