Skip to content

Commit 2855693

Browse files
committed
Warn on misleading indentation in single-case catch
Fixes #14721 Surprisingly, caught two instances in the compiler code base itself.
1 parent 5587767 commit 2855693

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

compiler/src/dotty/tools/backend/jvm/BytecodeWriters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ trait BytecodeWriters {
118118
try
119119
outfile.delete() // don't leave an empty or half-written classfile around after an interrupt
120120
catch case _: Throwable =>
121-
throw ex
121+
throw ex
122122
finally outstream.close()
123123
report.informProgress("wrote '" + label + "' to " + outfile)
124124
}

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
276276
try
277277
outTastyFile.delete() // don't leave an empty or half-written tastyfile around after an interrupt
278278
catch case _: Throwable =>
279-
throw ex
279+
throw ex
280280
finally outstream.close()
281281

282282
val uuid = new TastyHeaderUnpickler(binary()).readHeader()

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,12 @@ object Parsers {
26202620
(pattern(), guard())
26212621
}
26222622
CaseDef(pat, grd, atSpan(accept(ARROW)) {
2623-
if exprOnly then expr() else block()
2623+
if exprOnly then
2624+
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
2625+
warning(i"""misleading indentation: this expression forms part of the preceding catch case,
2626+
|it should be indented for clarity.""")
2627+
expr()
2628+
else block()
26242629
})
26252630
}
26262631

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.util.chaining.given
2+
3+
class C:
4+
def op: Unit = println("op")
5+
def handler: Unit = println("handler")
6+
def test: Unit =
7+
try op
8+
catch case _: NullPointerException =>
9+
handler // error

0 commit comments

Comments
 (0)