Skip to content

Commit b55d17e

Browse files
committed
Error message for pure expr in stat position
1 parent 506d699 commit b55d17e

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public enum ErrorMessageID {
134134
TermMemberNeedsNeedsResultTypeForImplicitSearchID,
135135
CaseClassCannotExtendEnumID,
136136
ValueClassParameterMayNotBeCallByNameID,
137-
NotAnExtractorID
137+
NotAnExtractorID,
138+
PureExpressionInStatementPositionID
138139
;
139140

140141
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,4 +2131,12 @@ object messages {
21312131
|For this reason, you can also define patterns through `unapplySeq` which returns `Option[Seq[T]]`.
21322132
|This mechanism is used for instance in pattern `case List(x1, ..., xn)`""".stripMargin
21332133
}
2134+
2135+
case class PureExpressionInStatementPosition(stat: untpd.Tree, exprOwner: Symbol)(implicit ctx: Context) extends Message(PureExpressionInStatementPositionID) {
2136+
val kind = "Potential Issue"
2137+
val msg = "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
2138+
val explanation =
2139+
hl"""The pure expression `$stat` doesn't have any side effect and its result is not assigned elsewhere.
2140+
|It can be removed without changing the semantics of the program. This may indicate an error.""".stripMargin
2141+
}
21342142
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ class Typer extends Namer
20262026
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
20272027
if (!ctx.isAfterTyper && isPureExpr(stat1) &&
20282028
!stat1.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(stat1))
2029-
ctx.warning(em"a pure expression does nothing in statement position", stat.pos)
2029+
ctx.warning(PureExpressionInStatementPosition(stat, exprOwner), stat.pos)
20302030
buf += stat1
20312031
traverse(rest)
20322032
case nil =>

0 commit comments

Comments
 (0)