File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed
src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -1707,7 +1707,10 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
1707
1707
case Thicket (stats) :: rest =>
1708
1708
traverse(stats ++ rest)
1709
1709
case stat :: rest =>
1710
- buf += typed(stat)(ctx.exprContext(stat, exprOwner))
1710
+ val stat1 = typed(stat)(ctx.exprContext(stat, exprOwner))
1711
+ if (! ctx.isAfterTyper && isPureExpr(stat1))
1712
+ ctx.warning(em " a pure expression does nothing in statement position " , stat.pos)
1713
+ buf += stat1
1711
1714
traverse(rest)
1712
1715
case nil =>
1713
1716
buf.toList
Original file line number Diff line number Diff line change @@ -163,6 +163,7 @@ class CompilationTests extends ParallelTesting {
163
163
compileFile(" ../tests/neg/customArgs/noimports2.scala" , defaultOptions.and(" -Yno-imports" )) +
164
164
compileFile(" ../tests/neg/customArgs/overloadsOnAbstractTypes.scala" , allowDoubleBindings) +
165
165
compileFile(" ../tests/neg/customArgs/xfatalWarnings.scala" , defaultOptions.and(" -Xfatal-warnings" )) +
166
+ compileFile(" ../tests/neg/customArgs/pureStatement.scala" , defaultOptions.and(" -Xfatal-warnings" )) +
166
167
compileFile(" ../tests/neg/customArgs/phantom-overload.scala" , allowDoubleBindings) +
167
168
compileFile(" ../tests/neg/tailcall/t1672b.scala" , defaultOptions) +
168
169
compileFile(" ../tests/neg/tailcall/t3275.scala" , defaultOptions) +
Original file line number Diff line number Diff line change
1
+ class IOCapability
2
+
3
+ object Test {
4
+ " " // error: pure expression does nothing in statement position
5
+
6
+ locally {
7
+ " " // error: pure expression does nothing in statement position
8
+
9
+ println(" " )
10
+
11
+ 42 // error: pure expression does nothing in statement position
12
+
13
+ ((x : Int ) => println(" hi" )) // error: pure expression does nothing in statement position
14
+
15
+ ()
16
+ }
17
+
18
+ // Forgot to mark `ev` implicit!
19
+ def doSideEffects (x : Int )(ev : IOCapability ) = {
20
+ println(" x: " + x)
21
+ }
22
+
23
+ implicit val cap : IOCapability = new IOCapability
24
+
25
+ 2 // error: pure expression does nothing in statement position
26
+
27
+ doSideEffects(1 ) // error: pure expression does nothing in statement position
28
+ }
You can’t perform that action at this time.
0 commit comments