Navigation Menu

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-9732] Incorrect/Misleading warning for var/let in for loop #52166

Closed
dhoepfl opened this issue Jan 23, 2019 · 1 comment
Closed

[SR-9732] Incorrect/Misleading warning for var/let in for loop #52166

dhoepfl opened this issue Jan 23, 2019 · 1 comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement

Comments

@dhoepfl
Copy link
Contributor

dhoepfl commented Jan 23, 2019

Previous ID SR-9732
Radar None
Original Reporter @dhoepfl
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI
Assignee dingobye (JIRA)
Priority Medium

md5: 20c35c888922c9134ae2df77a55a7779

Issue Description:

Tested using LLVM f63b283c71, Clang 41ac4c4262, Swift 7cb2c9b:

%> cat v.swift
import Foundation

func f(_ data: Data) -> Void {
   for var x in data {
      print("\(x)")
   }
}
%> swift v.swift
v.swift:4:12: warning: variable 'x' was never mutated; consider changing to 'let' constant
   for var x in data {
       ~~~~^

The correct solution would be to remove the "var" (which is what Xcode's Fix-It does).

Following the advice results in:

%> cat l.swift
import Foundation

func f(_ data: Data) -> Void {
   for let x in data {
      print("\(x)")
   }
}
%> swift l.swift
l.swift:4:8: error: 'let' pattern cannot appear nested in an already immutable context
   for let x in data {
       ^

This does not happen in REPL.

(For reference: see my Swift Forum post)

Suggested fixes:

  • Change the warning to "... consider removing 'var'" or

  • allow "let".

Trivia:

I started with the let variant, the error message was not very helpful.

@swift-ci
Copy link
Collaborator

Comment by Ding Ye (JIRA)

#22084

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 diagnostics QoI Bug: Diagnostics Quality of Implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants