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-2421] incorrect reporting of "Immutable value 'contents' was never used" #45026

Closed
swift-ci opened this issue Aug 19, 2016 · 3 comments
Closed
Assignees
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2421
Radar None
Original Reporter ingconti (JIRA User)
Type Improvement
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug
Assignee @hartbit
Priority Medium

md5: 20a9ff0f4cc5784b4f22cf5ca5094487

Issue Description:

make a simple call like that:

func APISample(){
    let contents: NSString?
    let filePath = ""
        do {
            contents = try NSString(contentsOfFile: filePath, encoding: String.Encoding.utf8.rawValue)
        } catch _ {
            contents = nil
        }
}

you will get a warning:

....swift:62:7: Immutable value 'contents' was never used; consider replacing with '_' or removing it

If you perform the suggestion you get:

func APISample(){
    let _: NSString?
    let filePath = ""
    do {
        contents = try NSString(contentsOfFile: filePath, encoding: String.Encoding.utf8.rawValue)
    } catch _ {
        contents = nil
    }
}

you got:
:65:4: Use of unresolved identifier 'contents'

Worse:

If You copy the SAME code outside, it works!

let contents: NSString?
let filePath = ""
do {
    contents = try NSString(contentsOfFile: filePath, encoding: String.Encoding.utf8.rawValue)
} catch _ {
    contents = nil
}


func APISample(){
    let contents: NSString?
    let filePath = ""
    do {
        contents = try NSString(contentsOfFile: filePath, encoding: String.Encoding.utf8.rawValue)
    } catch _ {
        contents = nil
    }
}
@belkadan
Copy link
Contributor

I guess the fix-it should make sure to remove all assignments as well.

@belkadan
Copy link
Contributor

Or we could just offer the message with no fix-it if there are assignments.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

Fixed by #8175

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 good first issue Good for newcomers improvement
Projects
None yet
Development

No branches or pull requests

3 participants