Skip to content

Commit 2b7e8cf

Browse files
committed
Error message for pure expr in stat position
1 parent a3efd48 commit 2b7e8cf

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-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
@@ -135,7 +135,8 @@ public enum ErrorMessageID {
135135
CaseClassCannotExtendEnumID,
136136
ValueClassParameterMayNotBeCallByNameID,
137137
NotAnExtractorID,
138-
MemberWithSameNameAsStaticID
138+
MemberWithSameNameAsStaticID,
139+
PureExpressionInStatementPositionID
139140
;
140141

141142
public int errorNumber() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,4 +2139,14 @@ object messages {
21392139
override def kind: String = "Syntax"
21402140
override def explanation: String = ""
21412141
}
2142+
2143+
case class PureExpressionInStatementPosition(stat: untpd.Tree, exprOwner: Symbol)(implicit ctx: Context)
2144+
extends Message(PureExpressionInStatementPositionID) {
2145+
2146+
val kind = "Potential Issue"
2147+
val msg = "a pure expression does nothing in statement position; you may be omitting necessary parentheses"
2148+
val explanation =
2149+
hl"""The pure expression `$stat` doesn't have any side effect and its result is not assigned elsewhere.
2150+
|It can be removed without changing the semantics of the program. This may indicate an error.""".stripMargin
2151+
}
21422152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ class Typer extends Namer
20132013
val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
20142014
if (!ctx.isAfterTyper && isPureExpr(stat1) &&
20152015
!stat1.tpe.isRef(defn.UnitClass) && !isSelfOrSuperConstrCall(stat1))
2016-
ctx.warning(em"a pure expression does nothing in statement position", stat.pos)
2016+
ctx.warning(PureExpressionInStatementPosition(stat, exprOwner), stat.pos)
20172017
buf += stat1
20182018
traverse(rest)
20192019
case nil =>

0 commit comments

Comments
 (0)