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-9883] Unmutated "inout" should warn like unmutated "var" #52289

Open
stephencelis opened this issue Feb 7, 2019 · 2 comments
Open

[SR-9883] Unmutated "inout" should warn like unmutated "var" #52289

stephencelis opened this issue Feb 7, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@stephencelis
Copy link
Contributor

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

md5: 3c7a5d4404735951bff2a03f808e31d0

Issue Description:

Using "var" and failing to mutate it results in the following warning:

warning: variable 'x' was never mutated; consider changing to 'let' constant

Using "inout" and failing to mutate it results in no warning.

Shouldn't we have a warning here? If I forget to mutate an "inout" variable in a function, I've probably done something wrong.

I stumbled upon this when I defined a function that took an "inout" parameter but ended up copying it internally and mutating the copy.

@belkadan
Copy link
Contributor

belkadan commented Feb 8, 2019

Hm. Sometimes you have to put inout because you're, say, satisfying a protocol requirement. I wouldn't want to add a warning unless we had a way to silence it.

(The most obvious way I can think of is self-assignment, but I feel like normally we'd want to warn on that. The next is to use _ instead of a real param name, but that affects doc comments.)

@stephencelis
Copy link
Contributor Author

I think `_` makes sense, and maybe it's a good thing that the documentation would reflect the fact that this conformance does not mutate things.

If we don't want doc comments to be affected, though, self-assignment with parentheses could make it explicit.

func mut<A>(_ a: inout A) {
  (a = a)
} 

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants