Skip to content

Commit e64fc49

Browse files
authored
Merge pull request #14738 from dotty-staging/fix-14721
Warn on misleading indentation in single-case catch
2 parents a2cd2c5 + a7ffc8e commit e64fc49

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ trait BytecodeWriters {
117117
catch case ex: ClosedByInterruptException =>
118118
try
119119
outfile.delete() // don't leave an empty or half-written classfile around after an interrupt
120-
catch case _: Throwable =>
120+
catch
121+
case _: Throwable =>
121122
throw ex
122123
finally outstream.close()
123124
report.informProgress("wrote '" + label + "' to " + outfile)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
275275
catch case ex: ClosedByInterruptException =>
276276
try
277277
outTastyFile.delete() // don't leave an empty or half-written tastyfile around after an interrupt
278-
catch case _: Throwable =>
278+
catch
279+
case _: Throwable =>
279280
throw ex
280281
finally outstream.close()
281282

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,14 @@ object Parsers {
26172617
(pattern(), guard())
26182618
}
26192619
CaseDef(pat, grd, atSpan(accept(ARROW)) {
2620-
if exprOnly then expr() else block()
2620+
if exprOnly then
2621+
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
2622+
warning(i"""Misleading indentation: this expression forms part of the preceding catch case.
2623+
|If this is intended, it should be indented for clarity.
2624+
|Otherwise, if the handler is intended to be empty, use a multi-line catch with
2625+
|an indented case.""")
2626+
expr()
2627+
else block()
26212628
})
26222629
}
26232630

compiler/test/dotty/tools/repl/ShadowingTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object ShadowingTests:
3333
val subdir = dir.resolve(name)
3434
try Files.createDirectory(subdir)
3535
catch case _: java.nio.file.FileAlreadyExistsException =>
36-
assert(Files.isDirectory(subdir), s"failed to create shadowed subdirectory $subdir")
36+
assert(Files.isDirectory(subdir), s"failed to create shadowed subdirectory $subdir")
3737
subdir
3838

3939
// The directory on the classpath containing artifacts to be shadowed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class C:
3+
def op: Unit = println("op")
4+
def handler: Unit = println("handler")
5+
def test: Unit =
6+
try op
7+
catch case _: NullPointerException =>
8+
handler // error

0 commit comments

Comments
 (0)