Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Rule Request: Avoid optional collections #1885
New Issue Checklist
Collections types, such as dictionaries, arrays, etc. should default to empty collections rather than optional collections. Using optional collections often means a double check of ensuring the collection is not nil, and checking the collection has members; while optional chaining can make this somewhat simple, it can still complicate the code. There's little point to a function returning an empty collection, rather than a nil collection, or defining a property as an optional collection rather than just providing it with a default empty collection.
Additionally, Apple strongly discourages the use of optional collections. This is a quote from Apple during an API review:
Would not trigger:
It's not likely there are any valid configurable options
I'd suggest the rule be enabled by default. To reference above, it's already rare to use optional collections in Swift, and there's usually no meaningful distinction between an empty collection and nil.