-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Extend Optional-As-Any Warning to String Interpolation Segments #5110
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
Conversation
Basic extension of the optional-to-any AST walker to incorporate warnings for the as-of-now up and coming Swift evolution proposal.
@swift-ci please smoke test. |
3c86749
to
31a2141
Compare
print("Always some, Always some, Always some: \(o as Int?)") // No warning | ||
print("Always some, Always some, Always some: \(o.debugDescription)") // No warning. | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename the test case to match the new functionality? e.g.:
diag_unintended_optional_behavior.swift
or
diag_unintended_optional_uses.swift
@@ -2411,6 +2411,14 @@ NOTE(force_optional_to_any,none, | |||
"force-unwrap the value to avoid this warning", ()) | |||
NOTE(silence_optional_to_any,none, | |||
"explicitly cast to Any with 'as Any' to silence this warning", ()) | |||
WARNING(optional_in_string_interpolation_segment,none, | |||
"optional value in string interpolation can have unintended behavior", | |||
()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"unintended behavior" still seems to vague to me.
Can you try to find something more specific to use here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a middle ground between the two diagnostics:
string interpolation produces a debug description for an optional value; did you mean to make this explicit?
|
||
if (auto segmentTy = segment->getType()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the 'if' here is absolutely necessary.
|
||
if (auto segmentTy = segment->getType()) { | ||
if (auto baseTy = segmentTy->getOptionalObjectType()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the spirit of http://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code, can you assign baseTy before the if
and then invert the test and make it continue, unindenting the following code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually baseTy is only used for the message here, so perhaps you could just skip having that and instead use segmentTy and not append the '?' below?
f81bcae
to
d7a3ffa
Compare
@rudkx That should do it. |
d7a3ffa
to
cb8b8b1
Compare
@swift-ci please smoke test. |
@swift-ci Please smoke test Linux |
Thanks @shahmishal |
auto segmentTy = segment->getType(); | ||
if (auto parenType = dyn_cast<ParenType>(segmentTy.getPointer())) { | ||
segmentTy = parenType->getUnderlyingType(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessary. The call to getOptionalObjectType() strips the parens, and if you rework the diagnostics to print Types rather than StringRef I believe it strips parens as well.
Here is a quick tweak I just tried, to see what I mean: https://github.com/rudkx/swift/commit/6a43b76aeb5548d9f80bba98fdcbe4d5d58c79e7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's what I was looking for. Thanks!
cb8b8b1
to
0243497
Compare
@swift-ci please smoke test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@swift-ci Please smoke test |
Gonna give the Swift evolution discussion a chance to settle down a bit before I commit this. |
⛵️ |
Since both fix-its result in the same behavior, I'm inclined to say we should just pick one and drop the other note. |
Arguably we should still offer the |
Then i'd prefer to keep the |
|
That's a good point. |
Hm, that's a good idea. I'll amend the diagnostic and shoot ya'll another pull request. |
Thanks, this looks great! 😄 Minor recommendations:
|
Thanks!
|
Hiding tremendous warnings. Sad. swiftlang/swift#5110
This just fills my log code full of (String(describing:someOptional) and Xcode Fix-it is broken doesnt appear all the time especially if theres two " ...(optional1) (optional2). |
@clearbrian Please file an SR about specific bugs in Swift related to Optional-as-Any diagnostics, and radars about Xcode. It may just be that you're trying to apply stale fixits, it may be something deeper, but I can't tell from this comment alone. In addition, the diagnostic should offer more ways to fix or silence this warning than the "long way" of going through the initializer. If you rely on logging a lot of optionals, and that optionality is relevant to the log, then consider an |
This is an example of fixing a problem by introducing another. |
@CodaFi Is there any way to silence these warnings without adding so many useless lines of code to my project? Like a compiler flag or something? |
The fixit offers you multiple single-line avenues for doing just that. A cast with |
This issue is not the most appropriate avenue of discussion about this feature. Refer to the Swift evolution thread linked at the root for details about its inclusion and the problem it solves. I'm locking this discussion. |
An implementation of warnings for optional values in string interpolation segments. We now suggest that the user either
.debugDescription
T?
To explicitly request a debug string representation of an optional. Further discussion of this patch can be found on the list.
Resolves SR-1882.