Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
rgoldberg opened this issue Oct 21, 2024 · 3 comments · Fixed by #5857
Closed
2 tasks done
Assignees
Labels
bug Unexpected and reproducible misbehavior. good first issue Issue to be taken up by new contributors yet unfamiliar with the project. swift-compiler An issue caused by a shortcoming in the Swift compiler.

Comments

@rgoldberg
Copy link

rgoldberg commented Oct 21, 2024

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

  • SwiftLint version: 0.57.0
  • Installation method used: Homebrew
@SimplyDanny
Copy link
Collaborator

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
Copy link
Author

rgoldberg commented Oct 24, 2024

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
Copy link
Collaborator

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.

@SimplyDanny SimplyDanny added bug Unexpected and reproducible misbehavior. good first issue Issue to be taken up by new contributors yet unfamiliar with the project. labels Oct 25, 2024
@SimplyDanny SimplyDanny self-assigned this Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected and reproducible misbehavior. good first issue Issue to be taken up by new contributors yet unfamiliar with the project. swift-compiler An issue caused by a shortcoming in the Swift compiler.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants