Find functions that have optional return parameters but always return a non optional.
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- [ x ] Updated SwiftLint to the latest version
- [ x ] I searched for existing GitHub issues
New rule request
I would like to have a check for functions that declare optional return type but alway return non nil type.
So just as a simplified example lets say:
//triggering
func friendOrFoe() -> String? {
return "foe"
}
//triggering
func friendOrFoe() -> String? {
if (Bool.random()) {
return "friend"
} else {
return "foe"
}
}
//non triggering
func friendOrFoe() -> String? {
if (Bool.random()) {
return nil
}
if (Bool.random()) {
return "friend"
} else {
return "foe"
}
}
//non triggering
func friendOrFoe() -> String {
return "foe"
}
//non triggering
func friendOrFoe() -> String {
if (Bool.random()) {
return "friend"
} else {
return "foe"
}
}
This is of course a ridiculously simplified example but I have come across many cases of larger function with a more complicated logic that yet always return a value. But because the function declaration has the return as optional that optional and the logic complexity of using it is propagated through the code.
I have looked and did not get lucky finding a solution to this check. I am a newbe to the intricacies of swiftlint so maybe I did not do a good search. Instinctively I feel like this should be relatively easy to find since the opposite check is already baked into xcode where if you try to return nil and the function declaration is non optional return then you get a warning.
(not on topic but swiftlint has helped me remedy about 6000 warnings in the last months. Love it!)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files, tests, or implementation entry points are named. Start by locating SwiftLint's existing return-type analysis and compare its behavior with the triggering and non-triggering examples; done should be a rule that distinguishes optional-returning functions whose paths never return nil from functions that can return nil.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100