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

[SR-11095] Warn on pointless default argument #53488

Open
alexito4 opened this issue Jul 10, 2019 · 0 comments
Open

[SR-11095] Warn on pointless default argument #53488

alexito4 opened this issue Jul 10, 2019 · 0 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@alexito4
Copy link

Previous ID SR-11095
Radar None
Original Reporter @alexito4
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: fba846f6e7aaee6895be1ba7ece60135

relates to:

  • SR-11688 Swift should consider functions with default args ambiguous when there is another function with an identical signature

Issue Description:

When a method in a class that has arguments with default values clashes with other methods in a superclass, the compiler should warn the user that the method won't ever be called with those default arguments.

A common scenario is with init in UIKit code.

class Subview: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        print("init frame")
    }
    
    init(other: Int = 0) {
        super.init(frame: .zero)
        print("init other \(other)")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

let sub = Subview()

In the above example the second init won't ever be called with the default argument, as Swift will always prefer the superclass (UIView) init with zero arguments. This can be misleading and a cause of bugs.

As Jordan pointed out on Twitter the general case is tricky but it may be worth adding some diagnostics to the simple case.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

1 participant