Skip to content

Commit ffcbf6b

Browse files
committed
Avoid dead code warning in any macro
Value classes will give dead code warning on `ArgumentMatchers.any` because type annotation is missing. This commit fetches the internal type of the value class and adds the annotation.
1 parent 77a2fc4 commit ffcbf6b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

macro/src/main/scala/org/mockito/matchers/MacroMatchers.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ object MacroMatchers {
2323
val isValueClass = typeSymbol.isClass && typeSymbol.asClass.isDerivedValueClass
2424
val isCaseValueClass = isValueClass && typeSymbol.asClass.isCaseClass
2525

26+
lazy val innerType =
27+
tpe.members
28+
.filter(_.isConstructor)
29+
.flatMap(_.asMethod.paramLists)
30+
.flatMap(_.map(_.typeSignature))
31+
.head match {
32+
case TypeRef(_, s, _) => s.name.toTypeName
33+
}
34+
2635
val r = if (isCaseValueClass) c.Expr[AnyMatcher[T]] {
2736
q"""
2837
new _root_.org.mockito.matchers.AnyMatcher[$tpe] {
29-
override def any: $tpe = ${tpe.companion.decl(TermName("apply"))}(_root_.org.mockito.ArgumentMatchers.any())
38+
override def any: $tpe = ${tpe.companion.decl(TermName("apply"))}(_root_.org.mockito.ArgumentMatchers.any[$innerType]())
3039
}
3140
"""
3241
} else if (isValueClass) c.Expr[AnyMatcher[T]] {
3342
q"""
3443
new _root_.org.mockito.matchers.AnyMatcher[$tpe] {
35-
override def any: $tpe = new $tpe(_root_.org.mockito.ArgumentMatchers.any())
44+
override def any: $tpe = new $tpe(_root_.org.mockito.ArgumentMatchers.any[$innerType]())
3645
}
3746
"""
3847
} else

0 commit comments

Comments
 (0)