Skip to content

Commit

Permalink
Merge pull request #108 from razvan-panda/master
Browse files Browse the repository at this point in the history
Fix "pattern var x in method equals is never used" warning in generated code
  • Loading branch information
eed3si9n authored Oct 21, 2017
2 parents 67a2a73 + a40b7af commit 76c9bae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
17 changes: 8 additions & 9 deletions library/src/main/scala/sbt/contraband/ScalaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,16 @@ class ScalaCodeGen(javaLazy: String, javaOptional: String, instantiateJavaOption

private def genEquals(cl: RecordLikeDefinition) = {
val allFields = cl.fields filter { _.arguments.isEmpty }
val comparisonCode =
if (allFields exists (_.fieldType.isLazyType)) {
"super.equals(o) // We have lazy members, so use object identity to avoid circularity."
} else if (allFields.isEmpty) {
"true"
} else {
allFields map (f => s"(this.${bq(f.name)} == x.${bq(f.name)})") mkString " && "
}
val (x, comparisonCode) =
if (allFields exists (_.fieldType.isLazyType))
("_", "super.equals(o) // We have lazy members, so use object identity to avoid circularity.")
else if (allFields.isEmpty)
("_", "true")
else
("x", allFields map (f => s"(this.${bq(f.name)} == x.${bq(f.name)})") mkString " && ")

s"""override def equals(o: Any): Boolean = o match {
| case x: ${cl.name} => $comparisonCode
| case $x: ${cl.name} => $comparisonCode
| case _ => false
|}""".stripMargin
}
Expand Down
8 changes: 4 additions & 4 deletions library/src/test/scala/JsonScalaCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
"""/** example of nested protocols */
|sealed abstract class nestedProtocolExample() extends Serializable {
| override def equals(o: Any): Boolean = o match {
| case x: nestedProtocolExample => true
| case _: nestedProtocolExample => true
| case _ => false
| }
| override def hashCode: Int = {
Expand All @@ -124,7 +124,7 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|
|sealed abstract class nestedProtocol() extends nestedProtocolExample() with Serializable {
| override def equals(o: Any): Boolean = o match {
| case x: nestedProtocol => true
| case _: nestedProtocol => true
| case _ => false
| }
| override def hashCode: Int = {
Expand All @@ -140,7 +140,7 @@ class JsonScalaCodeGenSpec extends GCodeGenSpec("Scala") {
|
|final class ChildRecord private () extends nestedProtocol() with Serializable {
| override def equals(o: Any): Boolean = o match {
| case x: ChildRecord => true
| case _: ChildRecord => true
| case _ => false
| }
| override def hashCode: Int = {
Expand Down Expand Up @@ -353,7 +353,7 @@ object primitiveTypesExample2 {
| lazy val lazyArrayInteger: Vector[Int] = _lazyArrayInteger
| lazy val lazyOptionInteger: Option[Int] = _lazyOptionInteger
| override def equals(o: Any): Boolean = o match {
| case x: primitiveTypesExample => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
| case _: primitiveTypesExample => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
| case _ => false
| }
| override def hashCode: Int = {
Expand Down
12 changes: 6 additions & 6 deletions library/src/test/scala/JsonSchemaExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ sealed abstract class Greetings(
lazy val message: String = _message
override def equals(o: Any): Boolean = o match {
case x: Greetings => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: Greetings => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand All @@ -450,7 +450,7 @@ final class SimpleGreeting private (
private def this(message: => String) = this(message, new com.example.GreetingHeader(new java.util.Date(), "Unknown"))
override def equals(o: Any): Boolean = o match {
case x: SimpleGreeting => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: SimpleGreeting => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand Down Expand Up @@ -482,7 +482,7 @@ sealed abstract class GreetingExtra(
override def equals(o: Any): Boolean = o match {
case x: GreetingExtra => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: GreetingExtra => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand All @@ -503,7 +503,7 @@ final class GreetingExtraImpl private (
private def this(message: com.example.Lazy[String], extra: Array[String], x: String) = this(message, new com.example.GreetingHeader(new java.util.Date(), "Unknown"), extra, x)
override def equals(o: Any): Boolean = o match {
case x: GreetingExtraImpl => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: GreetingExtraImpl => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand Down Expand Up @@ -542,7 +542,7 @@ final class GreetingWithAttachments private (
private def this(message: => String, attachments: Vector[java.io.File]) = this(message, new com.example.GreetingHeader(new java.util.Date(), "Unknown"), attachments)
override def equals(o: Any): Boolean = o match {
case x: GreetingWithAttachments => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: GreetingWithAttachments => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand Down Expand Up @@ -580,7 +580,7 @@ final class GreetingHeader private (
/** Creation date */
lazy val created: java.util.Date = _created
override def equals(o: Any): Boolean = o match {
case x: GreetingHeader => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _: GreetingHeader => super.equals(o) // We have lazy members, so use object identity to avoid circularity.
case _ => false
}
override def hashCode: Int = {
Expand Down

0 comments on commit 76c9bae

Please sign in to comment.