Skip to content

Commit 0cfec24

Browse files
committed
Fix non-exhaustive matches
- update non-exhaustive matches to be a bit more self documenting as opposed to just using the default case. - use NoSuccess.I so we don't have to specify Error and Failure cases - remove explicit Collection.Seq in main and use ArraySeq for Array to Seq conversion - replaceAllLiterally -> replace - adding default cases to non-exhaustive matches - Right(Unit) deprecated in favor of Right(()) - deprecated Seq.union in favor of ++: - remove unused import - update TestListSerialization to work for 2.12 and 2.13 errors DAFFODIL-2152
1 parent accd89f commit 0cfec24

File tree

16 files changed

+141
-111
lines changed

16 files changed

+141
-111
lines changed

daffodil-cli/src/main/scala/org/apache/daffodil/cli/Main.scala

+29-25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import javax.xml.parsers.SAXParserFactory
3535
import javax.xml.transform.TransformerException
3636
import javax.xml.transform.TransformerFactory
3737
import javax.xml.transform.stream.StreamResult
38+
import scala.collection.compat.immutable.ArraySeq
3839
import scala.concurrent.Await
3940
import scala.concurrent.ExecutionContext
4041
import scala.concurrent.Future
@@ -440,13 +441,13 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
440441
validateOpt(debug, infile) {
441442
case (Some(_), Some("-")) | (Some(_), None) =>
442443
Left("Input must not be stdin during interactive debugging")
443-
case _ => Right(Unit)
444+
case _ => Right(())
444445
}
445446

446447
validateOpt(parser, validate) {
447448
case (Some(_), Some(ValidationMode.Full)) =>
448449
Left("The validation mode must be 'limited' or 'off' when using a saved parser.")
449-
case _ => Right(Unit)
450+
case _ => Right(())
450451
}
451452

452453
validateOpt(infosetType, stream, schema) {
@@ -456,7 +457,7 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
456457
Left("Streaming mode is not currently supported with EXI infosets.")
457458
case (Some(InfosetType.EXISA), _, None) =>
458459
Left("A schema must be specified to use schema-aware compression with EXI")
459-
case _ => Right(Unit)
460+
case _ => Right(())
460461
}
461462
}
462463

@@ -566,7 +567,7 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
566567
validateOpt(debug, infile) {
567568
case (Some(_), Some("-")) | (Some(_), None) =>
568569
Left("Input must not be stdin during interactive debugging")
569-
case _ => Right(Unit)
570+
case _ => Right(())
570571
}
571572

572573
validateOpt(infosetType, stream, schema) {
@@ -576,7 +577,7 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
576577
Left("Streaming mode is not currently supported with EXI infosets.")
577578
case (Some(InfosetType.EXISA), _, None) =>
578579
Left("A schema must be specified to use schema-aware compression with EXI")
579-
case _ => Right(Unit)
580+
case _ => Right(())
580581
}
581582
}
582583

@@ -787,7 +788,7 @@ class CLIConf(arguments: Array[String], stdout: PrintStream, stderr: PrintStream
787788
validateOpt(infosetType, schema) {
788789
case (Some(InfosetType.EXISA), None) =>
789790
Left("A schema must be specified to use schema-aware compression with EXI")
790-
case _ => Right(Unit)
791+
case _ => Right(())
791792
}
792793
}
793794

@@ -991,8 +992,8 @@ class Main(
991992
val bindingsWithCorrectValues =
992993
bindings.filter(b => inBoth.exists(p => b.hashCode == p.hashCode))
993994

994-
val bindingsMinusUpdates = bindingsMinusBoth.union(bindingsToOverrideMinusBoth)
995-
val bindingsWithUpdates = bindingsMinusUpdates.union(bindingsWithCorrectValues)
995+
val bindingsMinusUpdates = bindingsMinusBoth ++: bindingsToOverrideMinusBoth
996+
val bindingsWithUpdates = bindingsMinusUpdates ++: bindingsWithCorrectValues
996997

997998
bindingsWithUpdates
998999
}
@@ -1428,23 +1429,26 @@ class Main(
14281429
forPerformance = true
14291430
)
14301431

1431-
val dataSeq: Seq[Either[AnyRef, Array[Byte]]] = files.map { filePath =>
1432-
// For performance testing, we want everything in memory so as to
1433-
// remove I/O from consideration. Additionally, for both parse
1434-
// and unparse we need immutable inputs since we could parse the
1435-
// same input data multiple times in different performance runs.
1436-
// So read the file data into an Array[Byte], and use that for
1437-
// everything.
1438-
val input = (new FileInputStream(filePath))
1439-
val dataSize = filePath.length()
1440-
val bytes = new Array[Byte](dataSize.toInt)
1441-
input.read(bytes)
1442-
val data = performanceOpts.unparse() match {
1443-
case true => Left(infosetHandler.dataToInfoset(bytes))
1444-
case false => Right(bytes)
1445-
}
1446-
data
1447-
}
1432+
val dataSeq: Seq[Either[AnyRef, Array[Byte]]] =
1433+
ArraySeq
1434+
.unsafeWrapArray(files)
1435+
.map { filePath =>
1436+
// For performance testing, we want everything in memory so as to
1437+
// remove I/O from consideration. Additionally, for both parse
1438+
// and unparse we need immutable inputs since we could parse the
1439+
// same input data multiple times in different performance runs.
1440+
// So read the file data into an Array[Byte], and use that for
1441+
// everything.
1442+
val input = (new FileInputStream(filePath))
1443+
val dataSize = filePath.length()
1444+
val bytes = new Array[Byte](dataSize.toInt)
1445+
input.read(bytes)
1446+
val data = performanceOpts.unparse() match {
1447+
case true => Left(infosetHandler.dataToInfoset(bytes))
1448+
case false => Right(bytes)
1449+
}
1450+
data
1451+
}
14481452

14491453
val inputs = (0 until performanceOpts.number()).map { n =>
14501454
val index = n % dataSeq.length

daffodil-codegen-c/src/main/scala/org/apache/daffodil/codegen/c/generators/CodeGeneratorState.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ class CodeGeneratorState(private val root: ElementBase) {
10451045
// Go up the stack that many times to get that struct's C type
10461046
val C = structs(nUpDirs).C
10471047
// Convert the up dirs to parents
1048-
val parents = upDirs.replaceAllLiterally("../", "parent->").stripSuffix("->")
1048+
val parents = upDirs.replace("../", "parent->").stripSuffix("->")
10491049
// Convert exprPath to a parents-> indirection
10501050
s"""(($C *)instance->_base.$parents)->$afterUpDirs"""
10511051
} else {

daffodil-core/src/main/scala/org/apache/daffodil/core/dpath/DFDLExpressionParser.scala

+16-11
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,30 @@ class DFDLPathExpressionParser[T <: AnyRef](
181181
expressionAST.init()
182182
expressionAST
183183
}
184-
case NoSuccess(msg, next) => {
185-
// blech - no easy way to just grab up to 30 chars from a Reader[Char]
186-
var nextRdr = next
187-
val nextString = new StringBuilder()
188-
var i = 0
189-
while (!nextRdr.atEnd && i < 30) {
190-
nextString.append(nextRdr.first)
191-
nextRdr = nextRdr.rest
192-
i += 1
193-
}
184+
case NoSuccess.I(msg, next) => {
185+
val nextString = convertNextToString(next)
194186
context.SDE(
195187
"Unable to parse expression. Message: %s\nNext: %s.",
196188
msg,
197-
nextString.toString()
189+
nextString
198190
)
199191
}
200192
}
201193
}
202194

195+
// blech - no easy way to just grab up to 30 chars from a Reader[Char]
196+
def convertNextToString(next: Input): String = {
197+
var nextRdr = next
198+
var i = 0
199+
val nextString = new StringBuilder()
200+
while (!nextRdr.atEnd && i < 30) {
201+
nextString.append(nextRdr.first)
202+
nextRdr = nextRdr.rest
203+
i += 1
204+
}
205+
nextString.toString()
206+
}
207+
203208
def wrapAsSuccess[T](p: => Parser[T]): Parser[ParseResult[T]] = Parser { in =>
204209
p(in) match {
205210
case ns: NoSuccess => Success(ns, in)

daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/DFDLDefineVariable.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ final class DFDLNewVariableInstance(node: Node, decl: AnnotatedSchemaComponent)
158158
final lazy val value = (attrValue, eltValue) match {
159159
case (None, v) if (v != "") => v
160160
case (Some(v), "") => v
161-
case (Some(v), ev) if (ev != "") =>
161+
case (Some(v), _) =>
162162
decl.SDE("Cannot have both a value attribute and an element value: %s", node)
163-
case (None, "") =>
163+
case (None, _) =>
164164
decl.SDE("Must have either a value attribute or an element value: %s", node)
165165
}
166166

@@ -182,9 +182,9 @@ final class DFDLSetVariable(node: Node, decl: AnnotatedSchemaComponent)
182182
final lazy val value = (attrValue, eltValue) match {
183183
case (None, v) if (v != "") => v
184184
case (Some(v), "") => v
185-
case (Some(v), ev) if (ev != "") =>
185+
case (Some(v), _) =>
186186
decl.SDE("Cannot have both a value attribute and an element value: %s", node)
187-
case (None, "") =>
187+
case (None, _) =>
188188
decl.SDE("Must have either a value attribute or an element value: %s", node)
189189
}
190190

daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/RestrictionUnion.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ final class Restriction private (xmlArg: Node, val simpleTypeDef: SimpleTypeDefB
168168
if (hasPattern) {
169169
val lPattern = localBaseFacets.filter { case (f, v) => f == Facet.pattern }
170170
val rPattern = remoteBaseFacets.filter { case (f, v) => f == Facet.pattern }
171-
val cPattern = lPattern.union(rPattern)
171+
val cPattern = lPattern ++: rPattern
172172
cPattern.foreach(x => combined.enqueue(x))
173173
}
174174
if (hasLength) {

daffodil-core/src/main/scala/org/apache/daffodil/core/dsom/SchemaSet.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,7 @@ final class SchemaSet private (
550550
}
551551

552552
lazy val allDefinedVariables = schemas
553-
.flatMap(_.defineVariables)
554-
.union(predefinedVars)
553+
.flatMap(_.defineVariables) ++: predefinedVars
555554

556555
private lazy val checkUnusedProperties = LV(Symbol("hasUnusedProperties")) {
557556
root.checkUnusedProperties

daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/ElementBaseGrammarMixin.scala

+6-2
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ trait ElementBaseGrammarMixin
12691269
)
12701270
LiteralValueNilOfSpecifiedLength(this)
12711271
}
1272-
case LengthKind.Implicit if isComplexType =>
1272+
case LengthKind.Implicit =>
12731273
LiteralValueNilOfSpecifiedLength(this)
12741274
case LengthKind.Prefixed => LiteralValueNilOfSpecifiedLength(this)
12751275
case LengthKind.EndOfParent if isComplexType =>
@@ -1286,7 +1286,7 @@ trait ElementBaseGrammarMixin
12861286
lengthKind match {
12871287
case LengthKind.Explicit => LiteralCharacterNilOfSpecifiedLength(this)
12881288
case LengthKind.Implicit if isSimpleType => LiteralCharacterNilOfSpecifiedLength(this)
1289-
case LengthKind.Implicit if isComplexType =>
1289+
case LengthKind.Implicit =>
12901290
Assert.invariantFailed("literal nil complex types aren't handled here.")
12911291
case LengthKind.Prefixed =>
12921292
SDE("nilKind='literalCharacter' is not valid for lengthKind='prefixed'")
@@ -1392,6 +1392,10 @@ trait ElementBaseGrammarMixin
13921392
notYetImplemented("lengthKind='endOfParent' for complex type")
13931393
case LengthKind.EndOfParent =>
13941394
notYetImplemented("lengthKind='endOfParent' for simple type")
1395+
case LengthKind.Delimited | LengthKind.Implicit =>
1396+
Assert.impossibleCase(
1397+
"Delimited and ComplexType Implicit cases should not be reached"
1398+
)
13951399
}
13961400
}
13971401
}

daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/Padded.scala

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ trait PaddingInfoMixin {
4949
eBase.textTrimKind match {
5050
case TextTrimKind.None => (MaybeChar.Nope, TextJustificationType.None)
5151
case TextTrimKind.PadChar if eBase.isSimpleType => padCharAndJustificationForType
52+
case TextTrimKind.PadChar =>
53+
eBase.SDE("padChar textTrimKind not supported for complexType")
5254
}
5355

5456
lazy val (unparsingPadChar: MaybeChar, justificationPad) = {

daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/PrimitivesTextNumber.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ case class ConvertTextStandardNumberPrim(e: ElementBase)
454454
checkPosNegNumPartSyntax(beforeV + "V" + afterV, negNum)
455455
afterV.length
456456
}
457-
case None =>
457+
case Some(_) | None =>
458458
e.SDE(
459459
s"""The dfdl:textNumberPattern '%s' contains 'V' (virtual decimal point).
460460
| Other than the sign indicators, it can contain only

daffodil-core/src/main/scala/org/apache/daffodil/core/grammar/primitives/SequenceChild.scala

+5-7
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,10 @@ class ScalarOrderedSequenceChild(sq: SequenceTermBase, term: Term, groupIndex: I
392392
import SeparatedSequenceChildBehavior._
393393

394394
lazy val sequenceChildParser: SequenceChildParser = {
395-
val HasSep = true
396-
val UnSep = false
397-
val res = (term, sq.hasSeparator) match {
398-
case (_, _) if !term.isRepresented =>
395+
val res = term match {
396+
case _ if !term.isRepresented =>
399397
new NonRepresentedSequenceChildParser(childParser, srd, trd)
400-
case (_: ElementBase, HasSep) =>
398+
case (_: ElementBase) if sq.hasSeparator =>
401399
new ScalarOrderedElementSeparatedSequenceChildParser(
402400
childParser,
403401
srd,
@@ -406,7 +404,7 @@ class ScalarOrderedSequenceChild(sq: SequenceTermBase, term: Term, groupIndex: I
406404
sq.separatorPosition,
407405
separatedHelper
408406
)
409-
case (_: ModelGroup, HasSep) => {
407+
case (_: ModelGroup) if sq.hasSeparator => {
410408
Assert.invariant(term.isInstanceOf[ModelGroup])
411409
new GroupSeparatedSequenceChildParser(
412410
childParser,
@@ -417,7 +415,7 @@ class ScalarOrderedSequenceChild(sq: SequenceTermBase, term: Term, groupIndex: I
417415
separatedHelper
418416
)
419417
}
420-
case (_, UnSep) =>
418+
case _ if !sq.hasSeparator =>
421419
new ScalarOrderedUnseparatedSequenceChildParser(
422420
childParser,
423421
srd,

daffodil-core/src/main/scala/org/apache/daffodil/core/util/FuzzData.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ abstract class FuzzData(
6464
*/
6565
def runLength: Int
6666

67-
override def hasNext() = true // we can keep fuzzing forever
67+
override def hasNext = true // we can keep fuzzing forever
6868

6969
override def next(): Array[Byte] = {
7070
val data = originalData.clone()

daffodil-core/src/test/scala/org/apache/daffodil/core/externalvars/TestExternalVariables.scala

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
package org.apache.daffodil.core.externalvars
1919

20-
import org.apache.daffodil.lib.Implicits._
21-
22-
import org.junit.Assert._
23-
import org.junit.Test; object INoWarn2 { ImplicitsSuppressUnusedImportWarning() }
2420
import scala.util.Success
2521

2622
import org.apache.daffodil.lib.xml._
2723

24+
import org.junit.Assert._
2825
import org.junit.Test
2926

3027
class TestExternalVariables {

0 commit comments

Comments
 (0)