realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.65k stars 2.22k forks source link

`superfluous_else` false positive when using `if #available(…) else` to avoid deprecation warning #5833

Open rgoldberg opened 1 week ago

rgoldberg commented 1 week ago

New Issue Checklist

Bug Description

superfluous_else outputs a false positive when using if #available(…) else to avoid deprecation warning:

if #available(macOS 10.10, *) {
    // do something
    return
} else {
    DispatchQueue.global(priority: .high).async {
        // do something
    }
}

The else is necessary to avoid 2 deprecation warnings from DispatchQueue.global(priority: .high).async {}.

That isn't my real use case; the deprecated function I use require arguments to be initialized & configured, so I just chose a very simple deprecated function at random as a much simpler example of the problem.

Presumably the same problem applies to #unavailable or other similar directives (i.e. if #<something> {} else {}.

Environment

SimplyDanny commented 5 days ago

I see this as a shortcoming in the Swift compiler. It should check if a certain statement referencing deprecated declarations is actually reachable in the relevant context.

In your example, whether the else block is there or not doesn't make a semantical difference. Do you agree?

rgoldberg commented 5 days ago

While I agree that it's a shortcoming in the compiler, I think that it might make sense to add a switch to ignore else clauses if #available or other similar directives are in any chained if condition.

People might not be able to upgrade their compiler to versions that receive a fix (due to macOS version requirements for new Xcode versions, or otherwise) but might be able to upgrade their SwiftLint.

SimplyDanny commented 4 days ago

Fair point. So let's treat this as a bug and update the behavior when/if the compiler received this feature depending on the Swift version in use.