Need Swift compiler warning when "var" is used when "let" is appropriate

Originator:kristopherdjohnson
Number:rdar://19309621 Date Originated:19-Dec-2014 01:18 PM
Status:Open Resolved:
Product:Developer Tools Product Version:6.1.1 (6A2008a)
Classification:Enhancement Reproducible:Not applicable
 
Summary:
Swift provides two keywords, "let" and "var", which are used to declare immutable data elements and mutable data elements respectively.  Good Swift style is to use "let" whenever possible, to avoid undesired mutation effects and make intention clear.

It would be helpful if the Swift compiler could warn the programmer when a variable or instance data member is declared with "var" but is never modified. This would encourage programmers to use "let" when appropriate and catch mistakes.

Steps to Reproduce:
For example, a programmer may write code like this:

    func readInputLine() -> String {
        var myLine: String = lowLevelRead()
        log("read line: \(myLine)")
        return myLine
    }


Expected Results:
Compiler should provide a warning like

"line 47: warning: 'myLine' declared to be mutable, but is never modified. Consider using 'let' instead of 'var'"

Actual Results:
Currently the Swift compiler does not provide such a warning.

Version:
Xcode Version 6.1.1 (6A2008a)

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!