Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "pattern var x in method equals is never used" warning in generated code #108

Merged
merged 2 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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