Better warning on unreferenced variable
| Originator: | dpassage | ||
| Number: | rdar://17224539 | Date Originated: | 6/8/2014 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode Version 6.0 (6A215l) |
| Classification: | Bug | Reproducible: | Always |
Summary:
Working with Swift, I defined the following method on a class:
func drawRandomCard() -> Card? {
var randomCard: Card? = nil
if cards.count > 0 {
let index = Int(arc4random()) % cards.count
let randomCard = cards[index]
cards.removeAtIndex(index)
}
return randomCard
}
This code has a bug: inside the "if" statement, I use "let" to define a new variable named "randomCard", which shadows the previous declaration above; as a result, it always returns nil.
Much better and more helpful would be if the compiler warned me about assigning a variable and then not using it inside the "if" block
Steps to Reproduce:
Compile the code above
Expected Results:
Xcode/llvm to warn about assigning an unused variable
Actual Results:
Code compiles with no warnings
Version:
Xcode Version 6.0 (6A215l)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!