Skip to content

Commit f37a935

Browse files
committed
enhance FinalCaseClasses rule to handle case classes defined inside traits and add corresponding test for SI-4440
1 parent 563f6b4 commit f37a935

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.avsystem.commons
22
package analyzer
33

4+
import com.avsystem.commons.analyzer.Level.Info
5+
46
import scala.tools.nsc.Global
57

68
class FinalCaseClasses(g: Global) extends AnalyzerRule(g, "finalCaseClasses", Level.Warn) {
@@ -9,7 +11,15 @@ class FinalCaseClasses(g: Global) extends AnalyzerRule(g, "finalCaseClasses", Le
911

1012
def analyze(unit: CompilationUnit): Unit = unit.body.foreach {
1113
case cd: ClassDef if !cd.mods.hasFlag(Flag.FINAL) && cd.mods.hasFlag(Flag.CASE) =>
12-
report(cd.pos, "Case classes should be marked as final")
14+
// Skip case classes defined inside traits (SI-4440)
15+
16+
val isInner = cd.symbol.isStatic
17+
18+
if (isInner) {
19+
report(cd.pos, "Case classes should be marked as final")
20+
} else {
21+
report(cd.pos, "Case classes should be marked as final. Due to the SI-4440 bug, it cannot be done here. Consider moving the case class to the companion object", level = Info)
22+
}
1323
case _ =>
1424
}
15-
}
25+
}

analyzer/src/test/scala/com/avsystem/commons/analyzer/FinalCaseClassesTest.scala

+20
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,24 @@ class FinalCaseClassesTest extends AnyFunSuite with AnalyzerTest {
3636
|}
3737
""".stripMargin)
3838
}
39+
40+
// SI-4440 https://github.com/scala/bug/issues/4440
41+
test("should not be affected due to SI-4440"){
42+
assertNoErrors(
43+
//language=scala
44+
"""
45+
|trait Outer {
46+
| case class Inner(x: Int, y: String) {
47+
| def double: Int = x * 2
48+
| }
49+
|}
50+
|
51+
|class Outer2 {
52+
| case class Inner(x: Int, y: String) {
53+
| def double: Int = x * 2
54+
| }
55+
|}
56+
""".stripMargin
57+
)
58+
}
3959
}

0 commit comments

Comments
 (0)