DisableSyntax
This rule reports errors when a "disallowed" syntax is used. This is a syntactic
rule, which means it does not require compilation to run unlike the Disable
rule.
Example:
MyCode.scala:10: error: [DisableSyntax.throw] exceptions should be avoided,
consider encoding the error in the return type instead
throw new IllegalArgumentException
^^^^^
Configuration
By default, this rule does not disable any particular syntax, every setting is opt-in.
Name | Type | Description |
---|---|---|
noVars | Boolean | Report error for usage of vars. |
noThrows | Boolean | Report error for throwing exceptions. |
noNulls | Boolean | Report error for usage of null. |
noReturns | Boolean | Report error for usage of return. |
noWhileLoops | Boolean | Report error for usage of while loops. |
noAsInstanceOf | Boolean | Report error for usage of asInstanceOf[T] casts. |
noIsInstanceOf | Boolean | Report error for usage of isInstanceOf[T] checks. |
noXml | Boolean | Report error on xml literals. |
noDefaultArgs | Boolean | Report error on method parameters with default arguments. |
noFinalVal | Boolean | Report error on `final` modifier for val definitions, see [motivation](https://github.com/sbt/zinc/issues/227) |
noFinalize | Boolean | Reports error when finalize is overridden. |
noValPatterns | Boolean | Report error when pattern matching in val assignment with non-tuple patterns. |
noUniversalEquality | Boolean | Report error on `==` (universal equality) |
noUniversalEqualityMessage | String | Reporter message for noUniversalEquality |
regex | List[Regex] | Report error if the text contents of a source file matches a given regex. |
Defaults
DisableSyntax.noVars = false
DisableSyntax.noThrows = false
DisableSyntax.noNulls = false
DisableSyntax.noReturns = false
DisableSyntax.noWhileLoops = false
DisableSyntax.noAsInstanceOf = false
DisableSyntax.noIsInstanceOf = false
DisableSyntax.noXml = false
DisableSyntax.noDefaultArgs = false
DisableSyntax.noFinalVal = false
DisableSyntax.noFinalize = false
DisableSyntax.noValPatterns = false
DisableSyntax.noUniversalEquality = false
DisableSyntax.noUniversalEqualityMessage = == and != are unsafe since they allow comparing two unrelated types
DisableSyntax.regex = []
Examples
DisableSyntax.noUniversalEquality = true
DisableSyntax.noUniversalEqualityMessage = "use === instead of =="
DisableSyntax.regex = [
{
id = "offensive"
pattern = "[Pp]imp"
message = "Please consider a less offensive word such as 'extension' or 'enrichment'"
}
]
Regex
Regex patterns have 3 available ways to be configured. The example below shows 1 of each way.
DisableSyntax.regex = [
{
id = offensive
pattern = "[Pp]imp"
message = "Please consider a less offensive word than ${0} such as Extension"
}
"Await\\.result"
{
id = magicNumbers
regex = {
pattern = "(?:(?:[,(]\\s)|(?:^\\s*))+(\\d+(\\.\\d+)?)"
captureGroup = 1
}
message = "Numbers ({$1} in this instance) should always have a named parameter attached, or be assigned to a val."
}
]
- The first way has an object providing an
id
,pattern
, andmessage
. - The second way is just the pattern. When this is used, the
id
is set equal to the pattern, and a generic message is provided for you. - The third way allows you to specify what capture-group the problematic piece of code is in, in case your regex is complicated and also matches characters not useful in an error message.
Error Messages
Error messages have access to the capture groups of the regex. To access the
capture groups of the regex, use {$n}
where n
is the index of the capture
group you wish to appear in that part of the message.
You can see this used in the 3rd example.