How to suppress Scalastyle rules

Sometimes Scalastyle will bark at you for reasons that aren’t always constructive.  For example, the magic number rule is generally a good one except when you are trying to create an enumeration with a particular indexing.  Because Scala will automatically 0-base the enumeration entries, if you wanted to 1-base them, you will need to reindex them.  But then this means you need to set them as below:

object MyEnumeration extends Enumeration {
  type MyEnumeration = Value
  val `First` = Value(1)
  val `Second` = Value(2)
  val `Third` = Value(3)
}

As you can see, the numbers 1, 2, 3 are considered ‘magic numbers’.

So the way to suppress the rule is to a) find the offending rule id (in this case magic.number) then b) make the following change:

object MyEnumeration extends Enumeration {
  // scalastyle:off magic.number
  type MyEnumeration = Value
  val `First` = Value(1)
  val `Second` = Value(2)
  val `Third` = Value(3)
 // scalastyle:on magic.number
}

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *