Skip to content

Commit 0258539

Browse files
committed
fix: enhance transientDefault handling to warn when no default value is provided
1 parent 38edf3b commit 0258539

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.avsystem.commons.serialization
2+
3+
import org.scalatest.wordspec.AnyWordSpec
4+
5+
final class NotUsedTransientDefault extends AnyWordSpec {
6+
7+
"transientDefault annotation" should {
8+
"compile without warnings when default value provided" in {
9+
assertCompiles(
10+
//language=Scala
11+
s"""
12+
|case class X(@transientDefault a: String = "default")
13+
|val codec = GenCodec.materialize[X]
14+
|""".stripMargin
15+
)
16+
}
17+
"not compile without warnings when no default value provided" in {
18+
assertDoesNotCompile(
19+
//language=Scala
20+
s"""
21+
|case class X(@transientDefault a: String)
22+
|val codec = GenCodec.materialize[X]
23+
|""".stripMargin
24+
)
25+
}
26+
}
27+
}

macros/src/main/scala/com/avsystem/commons/macros/serialization/GenCodecMacros.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
131131
}
132132
}
133133

134-
def isTransientDefault(param: ApplyParam): Boolean =
135-
param.defaultValue.nonEmpty && hasAnnotation(param.sym, TransientDefaultAnnotType)
134+
def isTransientDefault(param: ApplyParam, warnIfDefaultNotProvided: Boolean = false): Boolean =
135+
(hasAnnotation(param.sym, TransientDefaultAnnotType), param.defaultValue.nonEmpty, warnIfDefaultNotProvided) match {
136+
case (true, false, true) =>
137+
c.warning(param.sym.pos, s"@transientDefault has no effect on parameter ${param.sym.name} because it has no default value")
138+
false
139+
case (hasAnnotation, noDefaultValue, _) => hasAnnotation && noDefaultValue
140+
}
136141

137142
def isOptimizedPrimitive(param: ApplyParam): Boolean = {
138143
val vt = param.valueType
@@ -251,13 +256,13 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
251256
}
252257

253258
def anyParamHasTransientDefault: Boolean =
254-
params.exists(isTransientDefault)
259+
params.exists(isTransientDefault(_))
255260

256261
def isOptionLike(p: ApplyParam): Boolean =
257262
p.optionLike.nonEmpty
258263

259264
def mayBeTransient(p: ApplyParam): Boolean =
260-
isOptionLike(p) || isTransientDefault(p)
265+
isOptionLike(p) || isTransientDefault(p, warnIfDefaultNotProvided = true)
261266

262267
def transientValue(p: ApplyParam): Tree = p.optionLike match {
263268
case Some(optionLike) => q"${optionLike.reference(Nil)}.none"
@@ -614,7 +619,7 @@ class GenCodecMacros(ctx: blackbox.Context) extends CodecMacroCommons(ctx) with
614619
val deps = new mutable.ListBuffer[Tree]
615620

616621
ttpe.members.iterator.foreach { getter =>
617-
if(getter.isMethod && isJavaGetter(getter.asMethod)) {
622+
if (getter.isMethod && isJavaGetter(getter.asMethod)) {
618623
val propType = getter.typeSignatureIn(ttpe).finalResultType
619624
val getterName = getter.name.decodedName.toString
620625
val setterName = getterName.replaceFirst("^(get|is)", "set")

0 commit comments

Comments
 (0)