realm / realm/SwiftLint

False positive for `redundant_nil_coalescing` for double Optional.

Open
#4,738 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
Describe the bug

False positive for redundant_nil_coalescing check when we have double Optional.

Consider the following code:

import Foundation

func myFirstFunc() -> String?? {
    nil
}
func mySecondFunc() -> String? {
    return myFirstFunc() ?? nil
}

Note that this ?? nil is not redundant because of the double Optional returned by the first function. So this is a false positive for redundant_nil_coalescing.

Complete output when running SwiftLint, including the stack trace and command used
$ ./swiftlint example.swift
Linting Swift files at paths example.swift
Linting 'example.swift' (1/1)
example.swift:7:26: warning: Redundant Nil Coalescing Violation: nil coalescing operator is only evaluated if the lhs is nil, coalescing operator with nil as rhs is redundant (redundant_nil_coalescing)
Done linting! Found 1 violation, 0 serious in 1 file.

Furthermore, when we run

$ ./swiftlint --fix example.swift
Correcting Swift files at paths example.swift
Correcting 'example.swift' (1/1)
example.swift:7:25 Corrected Redundant Nil Coalescing
Done correcting 1 file!

, it removes the ?? nil from the second function, resulting in

func mySecondFunc() -> String? {
    return myFirstFunc()
}

which actually makes the code fail to compile with error

error: value of optional type 'String??' must be unwrapped to a value of type 'String?'
 return myFirstFunc()
 ^
Environment
opt_in_rules:
  - redundant_nil_coalescing
  • Are you using nested configurations?
    If so, paste their relative paths and respective contents.
    • No
  • Do you have a sample that shows the issue? Run echo "[string here]" | swiftlint lint --no-cache --use-stdin --enable-all-rules
    to quickly test if your example is really demonstrating the issue. If your example is more
    complex, you can use swiftlint lint --path [file here] --no-cache --enable-all-rules.
import Foundation

func myFirstFunc() -> String?? {
    nil
}
func mySecondFunc() -> String? {
    // This triggers a violation:
    return myFirstFunc() ?? nil
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the report with the Swift code in example.swift and the redundant_nil_coalescing rule, using the reported swiftlint command. Trace the rule and its correction behavior for String??, then verify that linting no longer reports this case and that swiftlint --fix does not produce code that fails to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.