Skip to content

Commit bfcaf8a

Browse files
committed
Refactor explicit generics rule to extend checks to constructors
1 parent bec4e5a commit bfcaf8a

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

analyzer/src/main/scala/com/avsystem/commons/analyzer/ExplicitGenerics.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ class ExplicitGenerics(g: Global) extends AnalyzerRule(g, "explicitGenerics") {
99

1010
lazy val explicitGenericsAnnotTpe = classType("com.avsystem.commons.annotation.explicitGenerics")
1111

12-
def analyze(unit: CompilationUnit) = if (explicitGenericsAnnotTpe != NoType) {
12+
13+
private def fail(pos: Position, symbol: Symbol): Unit =
14+
report(pos, s"$symbol requires that its type arguments are explicit (not inferred)")
15+
16+
def analyze(unit: CompilationUnit): Unit = if (explicitGenericsAnnotTpe != NoType) {
1317
def requiresExplicitGenerics(sym: Symbol): Boolean =
1418
sym != NoSymbol && (sym :: sym.overrides).flatMap(_.annotations).exists(_.tree.tpe <:< explicitGenericsAnnotTpe)
1519

@@ -22,7 +26,18 @@ class ExplicitGenerics(g: Global) extends AnalyzerRule(g, "explicitGenerics") {
2226
case _ => false
2327
}
2428
if (inferredTypeParams) {
25-
report(t.pos, s"${pre.symbol} requires that its type arguments are explicit (not inferred)")
29+
fail(t.pos, pre.symbol)
30+
}
31+
case n@New(tpt) if requiresExplicitGenerics(tpt.tpe.typeSymbol) =>
32+
val explicitTypeArgsProvided = tpt match {
33+
case tt: TypeTree => tt.original match {
34+
case AppliedTypeTree(_, args) if args.nonEmpty => true
35+
case _ => false
36+
}
37+
case _ => false
38+
}
39+
if (!explicitTypeArgsProvided) {
40+
fail(n.pos, tpt.tpe.typeSymbol)
2641
}
2742
case _ =>
2843
}

0 commit comments

Comments
 (0)