Skip to content

Commit

Permalink
Suppress WartRemover and Scapegoat warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Apr 11, 2024
1 parent b71778f commit 9348b77
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ final class PatcherMacros(val c: blackbox.Context) extends DerivationPlatform wi
](
tc: Expr[io.scalaland.chimney.dsl.PatcherConfiguration[ImplicitScopeFlags]]
): c.Expr[A] = retypecheck(
// Called by PatcherUsing => prefix is PatcherUsing
cacheDefinition(c.Expr[dsl.PatcherUsing[A, Patch, Tail, Flags]](c.prefix.tree)) { pu =>
Expr.block(
List(Expr.suppressUnused(pu)),
derivePatcherResult[A, Patch, Tail, Flags, ImplicitScopeFlags](
obj = c.Expr[A](q"$pu.obj"),
patch = c.Expr[Patch](q"$pu.objPatch")
suppressWarnings(
// Called by PatcherUsing => prefix is PatcherUsing
cacheDefinition(c.Expr[dsl.PatcherUsing[A, Patch, Tail, Flags]](c.prefix.tree)) { pu =>
Expr.block(
List(Expr.suppressUnused(pu)),
derivePatcherResult[A, Patch, Tail, Flags, ImplicitScopeFlags](
obj = c.Expr[A](q"$pu.obj"),
patch = c.Expr[Patch](q"$pu.objPatch")
)
)
)
}
}
)
)

def derivePatcherWithConfig[
Expand All @@ -39,22 +41,26 @@ final class PatcherMacros(val c: blackbox.Context) extends DerivationPlatform wi
](
tc: Expr[io.scalaland.chimney.dsl.PatcherConfiguration[ImplicitScopeFlags]]
): Expr[Patcher[A, Patch]] = retypecheck(
cacheDefinition(c.Expr[dsl.PatcherDefinition[A, Patch, Tail, InstanceFlags]](c.prefix.tree)) { pu =>
Expr.block(
List(Expr.suppressUnused(pu)),
derivePatcher[A, Patch, Tail, InstanceFlags, ImplicitScopeFlags]
)
}
suppressWarnings(
cacheDefinition(c.Expr[dsl.PatcherDefinition[A, Patch, Tail, InstanceFlags]](c.prefix.tree)) { pu =>
Expr.block(
List(Expr.suppressUnused(pu)),
derivePatcher[A, Patch, Tail, InstanceFlags, ImplicitScopeFlags]
)
}
)
)

def derivePatcherWithDefaults[
A: WeakTypeTag,
Patch: WeakTypeTag
]: Expr[Patcher[A, Patch]] = retypecheck(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
derivePatcher[A, Patch, runtime.PatcherOverrides.Empty, runtime.PatcherFlags.Default, ImplicitScopeFlags]
}
suppressWarnings(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
derivePatcher[A, Patch, runtime.PatcherOverrides.Empty, runtime.PatcherFlags.Default, ImplicitScopeFlags]
}
)
)

private def resolveImplicitScopeConfigAndMuteUnusedWarnings[A: Type](
Expand Down Expand Up @@ -98,4 +104,16 @@ final class PatcherMacros(val c: blackbox.Context) extends DerivationPlatform wi
catch {
case scala.reflect.macros.TypecheckException(_, msg) => c.abort(c.enclosingPosition, msg)
}

private def suppressWarnings[A: Type](expr: c.Expr[A]): c.Expr[A] = {
// Scala 3 generate prefix$macro$[n] while Scala 2 prefix[n] and we want to align the behavior
val result = c.internal.reificationSupport.freshTermName("result$macro$")
c.Expr[A](
q"""{
@_root_.java.lang.SuppressWarnings(_root_.scala.Array("org.wartremover.warts.All", "all"))
val $result = $expr
$result
}"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,37 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
](
tc: Expr[io.scalaland.chimney.dsl.TransformerConfiguration[ImplicitScopeFlags]]
): Expr[To] = retypecheck(
// Called by TransformerInto => prefix is TransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.TransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { ti =>
Expr.block(
List(Expr.suppressUnused(tc)),
deriveTotalTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$ti.source"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$ti.td.runtimeData")
suppressWarnings(
// Called by TransformerInto => prefix is TransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.TransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { ti =>
Expr.block(
List(Expr.suppressUnused(tc)),
deriveTotalTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$ti.source"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$ti.td.runtimeData")
)
)
)
}
}
)
)

def deriveTotalTransformerWithDefaults[
From: WeakTypeTag,
To: WeakTypeTag
]: Expr[Transformer[From, To]] = retypecheck(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
deriveTotalTransformer[
From,
To,
runtime.TransformerOverrides.Empty,
runtime.TransformerFlags.Default,
ImplicitScopeFlags
](ChimneyExpr.RuntimeDataStore.empty)
}
suppressWarnings(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
deriveTotalTransformer[
From,
To,
runtime.TransformerOverrides.Empty,
runtime.TransformerFlags.Default,
ImplicitScopeFlags
](ChimneyExpr.RuntimeDataStore.empty)
}
)
)

def deriveTotalTransformerWithConfig[
Expand All @@ -57,11 +61,13 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
ImplicitScopeFlags <: runtime.TransformerFlags: WeakTypeTag
](tc: Expr[io.scalaland.chimney.dsl.TransformerConfiguration[ImplicitScopeFlags]]): Expr[Transformer[From, To]] =
retypecheck(
Expr.block(
List(Expr.suppressUnused(tc)),
deriveTotalTransformer[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
// Called by TransformerDefinition => prefix is TransformerDefinition
c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"${c.prefix.tree}.runtimeData")
suppressWarnings(
Expr.block(
List(Expr.suppressUnused(tc)),
deriveTotalTransformer[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
// Called by TransformerDefinition => prefix is TransformerDefinition
c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"${c.prefix.tree}.runtimeData")
)
)
)
)
Expand All @@ -74,18 +80,20 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
ImplicitScopeFlags <: runtime.TransformerFlags: WeakTypeTag
](tc: Expr[io.scalaland.chimney.dsl.TransformerConfiguration[ImplicitScopeFlags]]): Expr[partial.Result[To]] =
retypecheck(
// Called by PartialTransformerInto => prefix is PartialTransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.PartialTransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { pti =>
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$pti.source"),
failFast = c.Expr[Boolean](q"false"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$pti.td.runtimeData")
suppressWarnings(
// Called by PartialTransformerInto => prefix is PartialTransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.PartialTransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { pti =>
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$pti.source"),
failFast = c.Expr[Boolean](q"false"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$pti.td.runtimeData")
)
)
)
}
}
)
)

def derivePartialTransformationWithConfigFailFast[
Expand All @@ -96,35 +104,39 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
ImplicitScopeFlags <: runtime.TransformerFlags: WeakTypeTag
](tc: Expr[io.scalaland.chimney.dsl.TransformerConfiguration[ImplicitScopeFlags]]): Expr[partial.Result[To]] =
retypecheck(
// Called by PartialTransformerInto => prefix is PartialTransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.PartialTransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { pti =>
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$pti.source"),
failFast = c.Expr[Boolean](q"true"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$pti.td.runtimeData")
suppressWarnings(
// Called by PartialTransformerInto => prefix is PartialTransformerInto
// We're caching it because it is used twice: once for RuntimeDataStore and once for source
cacheDefinition(c.Expr[dsl.PartialTransformerInto[From, To, Tail, InstanceFlags]](c.prefix.tree)) { pti =>
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformationResult[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
src = c.Expr[From](q"$pti.source"),
failFast = c.Expr[Boolean](q"true"),
runtimeDataStore = c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"$pti.td.runtimeData")
)
)
)
}
}
)
)

def derivePartialTransformerWithDefaults[
From: WeakTypeTag,
To: WeakTypeTag
]: c.universe.Expr[PartialTransformer[From, To]] =
retypecheck(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying
derivePartialTransformer[
From,
To,
runtime.TransformerOverrides.Empty,
runtime.TransformerFlags.Default,
implicitScopeFlagsType.Underlying
](ChimneyExpr.RuntimeDataStore.empty)
}
suppressWarnings(
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying
derivePartialTransformer[
From,
To,
runtime.TransformerOverrides.Empty,
runtime.TransformerFlags.Default,
implicitScopeFlagsType.Underlying
](ChimneyExpr.RuntimeDataStore.empty)
}
)
)

def derivePartialTransformerWithConfig[
Expand All @@ -136,11 +148,13 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
](
tc: Expr[io.scalaland.chimney.dsl.TransformerConfiguration[ImplicitScopeFlags]]
): Expr[PartialTransformer[From, To]] = retypecheck(
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformer[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
// Called by PartialTransformerDefinition => prefix is PartialTransformerDefinition
c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"${c.prefix.tree}.runtimeData")
suppressWarnings(
Expr.block(
List(Expr.suppressUnused(tc)),
derivePartialTransformer[From, To, Tail, InstanceFlags, ImplicitScopeFlags](
// Called by PartialTransformerDefinition => prefix is PartialTransformerDefinition
c.Expr[dsl.TransformerDefinitionCommons.RuntimeDataStore](q"${c.prefix.tree}.runtimeData")
)
)
)
)
Expand Down Expand Up @@ -186,4 +200,16 @@ final class TransformerMacros(val c: blackbox.Context) extends DerivationPlatfor
catch {
case scala.reflect.macros.TypecheckException(_, msg) => c.abort(c.enclosingPosition, msg)
}

private def suppressWarnings[A: Type](expr: c.Expr[A]): c.Expr[A] = {
// Scala 3 generate prefix$macro$[n] while Scala 2 prefix[n] and we want to align the behavior
val result = c.internal.reificationSupport.freshTermName("result$macro$")
c.Expr[A](
q"""{
@_root_.java.lang.SuppressWarnings(_root_.scala.Array("org.wartremover.warts.All", "all"))
val $result = $expr
$result
}"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ final class PatcherMacros(q: Quotes) extends DerivationPlatform(q) with Gateway
Overrides <: runtime.PatcherOverrides: Type,
Flags <: runtime.PatcherFlags: Type,
ImplicitScopeFlags <: runtime.PatcherFlags: Type
]: Expr[Patcher[A, Patch]] =
]: Expr[Patcher[A, Patch]] = suppressWarnings {
derivePatcher[A, Patch, Overrides, Flags, ImplicitScopeFlags]
}

def derivePatcherWithDefaults[
A: Type,
Patch: Type
]: Expr[Patcher[A, Patch]] =
]: Expr[Patcher[A, Patch]] = suppressWarnings {
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
derivePatcher[A, Patch, runtime.PatcherOverrides.Empty, runtime.PatcherFlags.Default, ImplicitScopeFlags]
}
}

private def resolveImplicitScopeConfigAndMuteUnusedWarnings[A: Type](
useImplicitScopeFlags: ?<[runtime.PatcherFlags] => Expr[A]
Expand All @@ -46,6 +48,12 @@ final class PatcherMacros(q: Quotes) extends DerivationPlatform(q) with Gateway
useImplicitScopeFlags(implicitScopeFlagsType)
)
}

private def suppressWarnings[A: Type](expr: Expr[A]): Expr[A] = '{
@SuppressWarnings(Array("org.wartremover.warts.All", "all"))
val result = ${ expr }
result
}
}

object PatcherMacros {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.scalaland.chimney.internal.compiletime.DefinitionsPlatform
import io.scalaland.chimney.internal.runtime
import io.scalaland.chimney.partial

import scala.quoted.{Expr, Quotes, Type}
import scala.quoted.{Expr, Quotes, Type, Varargs}

final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gateway {

Expand All @@ -20,13 +20,14 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
ImplicitScopeFlags <: runtime.TransformerFlags: Type
](
td: Expr[TransformerDefinition[From, To, Overrides, Flags]]
): Expr[Transformer[From, To]] =
): Expr[Transformer[From, To]] = suppressWarnings {
deriveTotalTransformer[From, To, Overrides, Flags, ImplicitScopeFlags](runtimeDataStore = '{ ${ td }.runtimeData })
}

def deriveTotalTransformerWithDefaults[
From: Type,
To: Type
]: Expr[Transformer[From, To]] =
]: Expr[Transformer[From, To]] = suppressWarnings {
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
deriveTotalTransformer[
Expand All @@ -37,11 +38,12 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
ImplicitScopeFlags
](runtimeDataStore = ChimneyExpr.RuntimeDataStore.empty)
}
}

def derivePartialTransformerWithDefaults[
From: Type,
To: Type
]: Expr[PartialTransformer[From, To]] =
]: Expr[PartialTransformer[From, To]] = suppressWarnings {
resolveImplicitScopeConfigAndMuteUnusedWarnings { implicitScopeFlagsType =>
import implicitScopeFlagsType.Underlying as ImplicitScopeFlags
derivePartialTransformer[
Expand All @@ -52,6 +54,7 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
ImplicitScopeFlags
](runtimeDataStore = ChimneyExpr.RuntimeDataStore.empty)
}
}

def derivePartialTransformerWithConfig[
From: Type,
Expand All @@ -61,10 +64,11 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
ImplicitScopeFlags <: runtime.TransformerFlags: Type
](
td: Expr[PartialTransformerDefinition[From, To, Overrides, Flags]]
): Expr[PartialTransformer[From, To]] =
): Expr[PartialTransformer[From, To]] = suppressWarnings {
derivePartialTransformer[From, To, Overrides, Flags, ImplicitScopeFlags](runtimeDataStore = '{
${ td }.runtimeData
})
}

private def resolveImplicitScopeConfigAndMuteUnusedWarnings[A: Type](
useImplicitScopeFlags: ?<[runtime.TransformerFlags] => Expr[A]
Expand All @@ -85,6 +89,12 @@ final class TransformerMacros(q: Quotes) extends DerivationPlatform(q) with Gate
useImplicitScopeFlags(implicitScopeFlagsType)
)
}

private def suppressWarnings[A: Type](expr: Expr[A]): Expr[A] = '{
@SuppressWarnings(Array("org.wartremover.warts.All", "all"))
val result = ${ expr }
result
}
}

object TransformerMacros {
Expand Down

0 comments on commit 9348b77

Please sign in to comment.