-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Either lint unused or warn discarded or warn pure #10641
Conversation
Is there a different recommendation we could substitute? |
Every warning is nowarnable. Unfortunately, Scala 3 has different syntax and categories and messages, so there is no specific advice that cross-compiles. |
What is the recommended mitigation in Scala 3? I'm asking for a couple reasons, one is that I'd like the PR description to document the current situation for users, the other is that maybe we could do something on the Scala 3 side. |
and
TIL dotty doesn't warn nonunit-statement for value discard. (The conversion moves a result expression to statement position, even the user did not "write it like that".) |
Would be good if we could limit it to one warning. |
Well, that may be true now. IIRC they used to have slightly different triggers.
|
I don't mean to be difficult, but I don't feel comfortable merging this unless we can, at the same time, provide users extremely clear guidance about how to proceed, including in cross-built projects. Changing the behavior without providing that seems like churn to me. |
@SethTisue they will have to supply version-specific This PR doesn't change behavior, only the "advice". |
Relatedly, proposal to try That behavior is adopted in refchecks, per Lukas's review comment above. The previous message for the case of "discarded pure expression" is amended. It used to be a separate warning, but after the move from typer to refchecks, appears in the wrong order (because top-down traversal). |
dab82c6
to
1b33c8a
Compare
1b33c8a
to
2daadc5
Compare
TIL I also need to |
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 otherwise, thank you!
@@ -1908,6 +1908,13 @@ abstract class RefChecks extends Transform { | |||
&& !treeInfo.hasExplicitUnit(t) // suppressed by explicit expr: Unit | |||
&& !isJavaApplication(t) // Java methods are inherently side-effecting | |||
) | |||
def checkDiscardValue(t: Tree): Boolean = | |||
t.hasAttachment[DiscardedValue.type] && { | |||
t.removeAttachment[DiscardedValue.type] |
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.
there's getAndRemoveAttachment[DiscardedValue.type].exists
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 think I was avoiding Option. This corner of the API is bothersome. Edit: but I would welcome guidance or reassurance.
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, retronym had already tired of the API, so I will use his improvement. getAndRemove
isn't very efficient, aside from allocating. It would be nice to have remove(element): Boolean
with just an indicator that it was removed. I did not touch the calls to update, which remove before adding (presumably to ensure only one attachment of a given type).
val discard = if (adapted) "; a value can be silently discarded when Unit is expected" else "" | ||
val msg = | ||
if (t.hasAttachment[DiscardedExpr.type]) { | ||
t.removeAttachment[DiscardedExpr.type] |
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.
also getAndRemoveAttachment
2daadc5
to
8be2220
Compare
Squashed, rebased, used retronym API for getAndRemove. Also realized I don't have a workflow for removing |
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.
👍 maybe we can improve the attachments API so that the convenient thing doesn't come with caveats
If they ask for
-Wnonunit-statement
, warn; otherwise, if they ask for-Wvalue-discard
, warn for that.If they don't ask, warn about pure expressions that aren't useful.
Move the value discard messaging to refchecks, in order to coordinate. Follow up to #8995.
Scala 3 won't mitigate warning value discard under
expr: Unit
syntax, so don't advertise it.The mitigation is not removed, for convenience, and because no one writes normal code like that.
To see how to suppress warnings, use
-Wconf:any:warning-verbose
in Scala 2 and-Wconf:any:verbose
in Scala 3.Extracted from stalled #10476