View on GitHub

Scalastyle: Implemented Rules

There are 73 rules which are currently implemented:

Checker Description
org.scalastyle.file.FileLengthChecker Check the number of lines in a file
org.scalastyle.file.FileLineLengthChecker Check the number of characters in a line
org.scalastyle.file.FileTabChecker Check that there are no tabs in a file
org.scalastyle.file.HeaderMatchesChecker Check the first lines of each file matches the text
org.scalastyle.file.IndentationChecker Checks that lines are indented by a multiple of the tab size
org.scalastyle.file.NewLineAtEofChecker Checks that a file ends with a newline character
org.scalastyle.file.NoNewLineAtEofChecker Checks that a file does not end with a newline character
org.scalastyle.file.RegexChecker Checks that a regular expression cannot be matched, if found reports this
org.scalastyle.file.WhitespaceEndOfLineChecker Check that there is no trailing whitespace at the end of lines
org.scalastyle.scalariform.BlockImportChecker Checks that block imports are not used.
org.scalastyle.scalariform.CaseBraceChecker Checks that braces aren’t used in case clauses
org.scalastyle.scalariform.ClassNamesChecker Check that class names match a regular expression
org.scalastyle.scalariform.ClassTypeParameterChecker Checks that type parameter to a class matches a regular expression
org.scalastyle.scalariform.CovariantEqualsChecker Check that classes and objects do not define equals without overriding equals(java.lang.Object).
org.scalastyle.scalariform.CurliesImportChecker Checks that curlies imports are not used.
org.scalastyle.scalariform.CyclomaticComplexityChecker Checks that the cyclomatic complexity of a method does exceed a value
org.scalastyle.scalariform.DeprecatedJavaChecker Checks that Java @Deprecated is not used, Scala @deprecated should be used instead
org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker Disallow space after certain token(s)
org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker Disallow space before certain token(s)
org.scalastyle.scalariform.EmptyClassChecker If a class/trait has no members, the braces are unnecessary
org.scalastyle.scalariform.EmptyInterpolatedStringChecker The interpolation for this string literal is not necessary
org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker Ensure single space after certain token(s)
org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker Ensure single space before certain token(s)
org.scalastyle.scalariform.EqualsHashCodeChecker Check that if a class implements either equals or hashCode, it should implement the other
org.scalastyle.scalariform.FieldNamesChecker Check that field names match a regular expression
org.scalastyle.scalariform.ForBraceChecker Checks that braces are used in for comprehensions
org.scalastyle.scalariform.ForLoopChecker Checks that parentheses are used in for loops
org.scalastyle.scalariform.IfBraceChecker Checks that if statements have braces
org.scalastyle.scalariform.IllegalImportsChecker Check that a class does not import certain classes
org.scalastyle.scalariform.ImportGroupingChecker Checks that imports are grouped together, not throughout the file
org.scalastyle.scalariform.ImportOrderChecker Checks that imports are grouped and ordered according to the style configuration.
org.scalastyle.scalariform.LowercasePatternMatchChecker Checks that a case statement pattern match is not lower case, as this can cause confusion
org.scalastyle.scalariform.MagicNumberChecker Checks for use of magic numbers
org.scalastyle.scalariform.MethodArgumentNamesChecker Check that method argument names match a regular expression
org.scalastyle.scalariform.MethodLengthChecker Checks that methods do not exceed a maximum length
org.scalastyle.scalariform.MethodNamesChecker Check that method names match a regular expression
org.scalastyle.scalariform.MultipleStringLiteralsChecker Checks that a string literal does not appear multiple times
org.scalastyle.scalariform.NamedArgumentChecker Checks that argument literals are named
org.scalastyle.scalariform.NoCloneChecker Check that classes and objects do not define the clone() method
org.scalastyle.scalariform.NoFinalizeChecker Check that classes and objects do not define the finalize() method
org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker No whitespace after left bracket ‘[’
org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker No whitespace before left bracket ‘[’
org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker No whitespace before right bracket ‘’]’’
org.scalastyle.scalariform.NonASCIICharacterChecker Some editors are unfriendly to non ASCII characters.
org.scalastyle.scalariform.NotImplementedErrorUsage Checks that the code does not have ??? operators.
org.scalastyle.scalariform.NullChecker Check that null is not used
org.scalastyle.scalariform.NumberOfMethodsInTypeChecker Check that a class / trait / object does not have too many methods
org.scalastyle.scalariform.NumberOfTypesChecker Checks that there are not too many types declared in a file
org.scalastyle.scalariform.ObjectNamesChecker Check that object names match a regular expression
org.scalastyle.scalariform.OverrideJavaChecker Checks that Java @Override is not used
org.scalastyle.scalariform.PackageNamesChecker Check that package names match a regular expression
org.scalastyle.scalariform.PackageObjectNamesChecker Check that package object names match a regular expression
org.scalastyle.scalariform.ParameterNumberChecker Maximum number of parameters for a method
org.scalastyle.scalariform.PatternMatchAlignChecker Check that pattern match arrows align
org.scalastyle.scalariform.ProcedureDeclarationChecker Use a : Unit = for procedure declarations
org.scalastyle.scalariform.PublicMethodsHaveTypeChecker Check that a method has an explicit return type, it is not inferred
org.scalastyle.scalariform.RedundantIfChecker Checks that if expressions are not redundant, i.e. easily replaced by a variant of the condition
org.scalastyle.scalariform.ReturnChecker Check that return is not used
org.scalastyle.scalariform.ScalaDocChecker Checks that the ScalaDoc on documentable members is well-formed
org.scalastyle.scalariform.SimplifyBooleanExpressionChecker Boolean expression can be simplified
org.scalastyle.scalariform.SpaceAfterCommentStartChecker Checks a space after the start of the comment.
org.scalastyle.scalariform.SpacesAfterPlusChecker Check that the plus sign is followed by a space
org.scalastyle.scalariform.SpacesBeforePlusChecker Check that the plus sign is preceded by a space
org.scalastyle.scalariform.StructuralTypeChecker Check that structural types are not used.
org.scalastyle.scalariform.TodoCommentChecker Check for use of TODO/FIXME single line comments
org.scalastyle.scalariform.TokenChecker Checks that a regular expression cannot be matched in a token, if found reports this
org.scalastyle.scalariform.UnderscoreImportChecker Avoid wildcard imports
org.scalastyle.scalariform.UppercaseLChecker Checks that if a long literal is used, then an uppercase L is used
org.scalastyle.scalariform.VarFieldChecker Checks that classes and objects do not define mutable fields
org.scalastyle.scalariform.VarLocalChecker Checks that functions do not define mutable variables
org.scalastyle.scalariform.WhileBraceChecker Checks that while body have braces
org.scalastyle.scalariform.WhileChecker Checks that while is not used
org.scalastyle.scalariform.XmlLiteralChecker Check that XML literals are not used

org.scalastyle.file.FileLengthChecker - Check the number of lines in a file

Justification

Files which are too long can be hard to read and understand.

Parameters

ParameterDescriptionTypeDefault Value
maxFileLength Maximum file length integer 1500

Example configuration

<check enabled="true" class="org.scalastyle.file.FileLengthChecker" level="warning">
 <parameters>
  <parameter name="maxFileLength">800</parameter>
 </parameters>
</check>

org.scalastyle.file.FileLineLengthChecker - Check the number of characters in a line

Justification

Lines that are too long can be hard to read and horizontal scrolling is annoying.

Parameters

ParameterDescriptionTypeDefault Value
maxLineLength Maximum line length integer 160
tabSize Tab size integer 4
ignoreImports Ignore import statements boolean false

Example configuration

<check enabled="true" class="org.scalastyle.file.FileLineLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLineLength">100</parameter>
  <parameter name="tabSize">2</parameter>
  <parameter name="ignoreImports">true</parameter>
 </parameters>
</check>

org.scalastyle.file.FileTabChecker - Check that there are no tabs in a file

Justification

Some say that tabs are evil.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.file.FileTabChecker" level="warning"/>

org.scalastyle.file.HeaderMatchesChecker - Check the first lines of each file matches the text

Justification

A lot of projects require a header with a copyright notice, or they require a license in each file. This does a simple text comparison between the header and the first lines of the file. You can have multiple lines, but make sure you surround the text with a CDATA section. You can also specify a regular expression, as long as you set the regex parameter to true.

Parameters

ParameterDescriptionTypeDefault Value
header Header string
regex Header Regex boolean false

Example configuration

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">false</parameter>
  <parameter name="header">// Copyright \(C\) 2011-2012 the original author or authors.</parameter>
 </parameters>
</check>

or

<check enabled="true" class="org.scalastyle.file.HeaderMatchesChecker" level="warning">
 <parameters>
  <parameter name="regex">true</parameter>
  <parameter name="header">// Copyright \(C\) (?:\d{4}-)?\d{4} the original author or authors.</parameter>
 </parameters>
</check>

org.scalastyle.file.IndentationChecker - Checks that lines are indented by a multiple of the tab size

Justification

Code that is not indented consistently can be hard to read.

Parameters

ParameterDescriptionTypeDefault Value
tabSize Tab size integer 2
methodParamIndentSize Multi-line method parameter spacing integer 2
classParamIndentSize Multi-line class parameter spacing integer 4

Example configuration

<check enabled="true" class="org.scalastyle.file.IndentationChecker" level="warning">
 <parameters>
  <parameter name="tabSize">2</parameter>
  <parameter name="methodParamIndentSize">2</parameter>
  <parameter name="classParamIndentSize">4</parameter>
 </parameters>
</check>

org.scalastyle.file.NewLineAtEofChecker - Checks that a file ends with a newline character

Justification

Some version control systems don’t cope well with files which don’t end with a newline character.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.file.NewLineAtEofChecker" level="warning"/>

org.scalastyle.file.NoNewLineAtEofChecker - Checks that a file does not end with a newline character

Justification

Because Mirco Dotta wanted it.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.file.NoNewLineAtEofChecker" level="warning"/>

org.scalastyle.file.RegexChecker - Checks that a regular expression cannot be matched, if found reports this

Justification

Some checks can be carried out with a regular expression.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string
line Line boolean false

Example configuration

<check enabled="true" class="org.scalastyle.file.RegexChecker" level="warning">
 <parameters>
  <parameter name="regex">(?m)^\s*$(\r|)\n^\s*$(\r|)\n</parameter>
  <parameter name="line">false</parameter>
 </parameters>
 <customMessage>No double blank lines</customMessage>
</check>

org.scalastyle.file.WhitespaceEndOfLineChecker - Check that there is no trailing whitespace at the end of lines

Justification

Whitespace at the end of a line can cause problems when diffing between files or between versions.

Parameters

ParameterDescriptionTypeDefault Value
ignoreWhitespaceLines Ignore lines with just whitespace boolean false

Example configuration

<check enabled="true" class="org.scalastyle.file.WhitespaceEndOfLineChecker" level="warning">
 <parameters>
  <parameter default="false" type="boolean" name="ignoreWhitespaceLines"/>
 </parameters>
</check>

org.scalastyle.scalariform.BlockImportChecker - Checks that block imports are not used.

Justification

Block imports (e.g. import a.{b, c}) can lead to annoying merge errors in large code bases that are maintained by lot of developers. This rule allows to ensure that only single imports are used in order to minimize merge errors in import declarations.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.BlockImportChecker" level="warning"/>

org.scalastyle.scalariform.CaseBraceChecker - Checks that braces aren’t used in case clauses

Justification

Braces aren’t required in case clauses. They should be omitted according to Scala Style Guide.

Parameters

No parameters

Example configuration

<check enabled="true" level="warning" class="org.scalastyle.scalariform.CaseBraceChecker"/>

org.scalastyle.scalariform.ClassNamesChecker - Check that class names match a regular expression

Justification

The Scala style guide recommends that class names conform to certain standards.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[A-Z][A-Za-z]*$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ClassNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ClassTypeParameterChecker - Checks that type parameter to a class matches a regular expression

Justification

Scala generic type names are generally single upper case letters. This check checks for classes and traits.

Note that this check only checks the innermost type parameter, to allow for List[T].

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[A-Z_]$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ClassTypeParameterChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z_]$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.CovariantEqualsChecker - Check that classes and objects do not define equals without overriding equals(java.lang.Object).

Justification

Mistakenly defining a covariant equals() method without overriding method equals(java.lang.Object) can produce unexpected runtime behaviour.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.CovariantEqualsChecker" level="warning"/>

org.scalastyle.scalariform.CurliesImportChecker - Checks that curlies imports are not used.

Justification

Curlies imports (e.g. import a.{b, c}) can lead to annoying merge errors in large code bases that are maintained by lot of developers. This rule allows to ensure that only single imports, no renaming and no hiding imports are used in order to minimize merge errors in import declarations.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.CurliesImportChecker" level="warning"/>

org.scalastyle.scalariform.CyclomaticComplexityChecker - Checks that the cyclomatic complexity of a method does exceed a value

Justification

If the code is too complex, then this can make code hard to read.

Parameters

ParameterDescriptionTypeDefault Value
maximum Maximum integer 10
countCases Count Cases boolean true

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.CyclomaticComplexityChecker" level="warning">
 <parameters>
  <parameter name="maximum">10</parameter>
  <parameter name="countCases">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DeprecatedJavaChecker - Checks that Java @Deprecated is not used, Scala @deprecated should be used instead

Justification

You should be using the Scala @deprecated instead.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.DeprecatedJavaChecker" level="warning"/>

org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker - Disallow space after certain token(s)

Justification

Correct formatting can help readability.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker - Disallow space before certain token(s)

Justification

Correct formatting can help readability.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.DisallowSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, COMMA, RPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EmptyClassChecker - If a class/trait has no members, the braces are unnecessary

Justification

If a class / trait has no members, then braces are unnecessary, and can be removed.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.EmptyClassChecker" level="warning"/>

org.scalastyle.scalariform.EmptyInterpolatedStringChecker - The interpolation for this string literal is not necessary

Justification

Empty interpolated strings are harder to read and not necessary.

Parameters

No parameters

Example configuration

<check enabled="true" level="warning" class="org.scalastyle.scalariform.EmptyInterpolatedStringChecker"/>

org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker - Ensure single space after certain token(s)

Justification

Correct formatting can help readability.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceAfterTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">COLON, IF</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker - Ensure single space before certain token(s)

Justification

Correct formatting can help readability.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.EnsureSingleSpaceBeforeTokenChecker" level="warning">
 <parameters>
  <parameter name="tokens">LPAREN</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.EqualsHashCodeChecker - Check that if a class implements either equals or hashCode, it should implement the other

Justification

Defining either equals or hashCode in a class without defining the is a known source of bugs. Usually, when you define one, you should also define the other.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.EqualsHashCodeChecker" level="warning"/>

org.scalastyle.scalariform.FieldNamesChecker - Check that field names match a regular expression

Justification

A consistent naming convention for field names can make code easier to read and understand

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[a-z][A-Za-z0-9]*$
objectFieldRegex Regular expression for constants string ^[A-Z][A-Za-z0-9]*$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.FieldNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="objectFieldRegex">^[A-Z][A-Za-z0-9]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ForBraceChecker - Checks that braces are used in for comprehensions

Justification

Usage of braces (rather than parentheses) within a for comprehension mean that you don’t have to specify a semi-colon at the end of every line:

for {      // braces
  t <- List(1,2,3)
  if (i % 2 == 0)
} yield t

is preferred to

for (      // parentheses
  t <- List(1,2,3);
  if (i % 2 == 0)
) yield t

To fix it, replace the () with {}. And then remove the ; at the end of the lines.

Description

The singleLineAllowed property allows for constructions of the type:

for (i <- List(1,2,3)) yield i

Parameters

ParameterDescriptionTypeDefault Value
singleLineAllowed Allow parentheses for single-line for boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ForBraceChecker" level="warning">
 <parameters>
  <parameter name="singleLineAllowed">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ForLoopChecker - Checks that parentheses are used in for loops

Justification

For-comprehensions which lack a yield clause is actually a loop rather than a functional comprehension and it is usually more readable to string the generators together between parentheses rather than using the syntactically-confusing } { construct:

for (x <- board.rows; y <- board.files) { printf(“(%d, %d)”, x, y) }

is preferred to

for { x <- board.rows y <- board.files } { printf(“(%d, %d)”, x, y) }

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ForLoopChecker" level="warning"/>

org.scalastyle.scalariform.IfBraceChecker - Checks that if statements have braces

Justification

Some people find if clauses with braces easier to read.

Description

The singleLineAllowed property allows if constructions of the type:

if (bool_expression) expression1 else expression2

The doubleLineAllowed property allows if constructions of the type:

if (bool_expression) expression1
else expression2

Note: If you intend to enable only if expressions in the format below, disable the IfBraceChecker altogether.

if (bool_expression)
  expression1
else
  expression2

Parameters

ParameterDescriptionTypeDefault Value
singleLineAllowed Single Line Allowed boolean true
doubleLineAllowed Double Line Allowed boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.IfBraceChecker" level="warning">
 <parameters>
  <parameter name="singleLineAllowed">true</parameter>
  <parameter name="doubleLineAllowed">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.IllegalImportsChecker - Check that a class does not import certain classes

Justification

Use of some classes can be discouraged for a project. For instance, use of sun._ is generally discouraged because they are internal to the JDK and can be changed.

Parameters

ParameterDescriptionTypeDefault Value
illegalImports Illegal Imports string sun._,java.awt._

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.IllegalImportsChecker" level="warning"/>

org.scalastyle.scalariform.ImportGroupingChecker - Checks that imports are grouped together, not throughout the file

Justification

If imports are spread throughout the file, knowing what is in scope at any one place can be difficult to work out.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ImportGroupingChecker" level="warning"/>

org.scalastyle.scalariform.ImportOrderChecker - Checks that imports are grouped and ordered according to the style configuration.

Justification

Consistent import ordering improves code readability and reduces unrelated changes in patches.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ImportOrderChecker" level="warning">
 <parameters>
  <parameter name="groups">java,scala,others</parameter>
  <parameter name="group.java">javax?\..+</parameter>
  <parameter name="group.scala">scala\..+</parameter>
  <parameter name="group.others">.+</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.LowercasePatternMatchChecker - Checks that a case statement pattern match is not lower case, as this can cause confusion

Justification

A lower case pattern match clause with no other tokens is the same as _; this is not true for patterns which start with an upper case letter. This can cause confusion, and may not be what was intended:

val foo = "foo"
val Bar = "bar"
"bar" match { case Bar => "we got bar" }   // result = "we got bar"
"bar" match { case foo => "we got foo" }   // result = "we got foo"
"bar" match { case `foo` => "we got foo" } // result = MatchError

This checker raises a warning with the second match. To fix it, use an identifier which starts with an upper case letter (best), use case _ or, if you wish to refer to the value, add a type : Any

val lc = "lc"
"something" match { case lc: Any => "lc" } // result = "lc"
"something" match { case _ => "lc" } // result = "lc"

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.LowercasePatternMatchChecker" level="warning"/>

org.scalastyle.scalariform.MagicNumberChecker - Checks for use of magic numbers

Justification

Replacing a magic number with a named constant can make code easier to read and understand, and can avoid some subtle bugs.

Description

A simple assignment to a val is not considered to be a magic number, for example:

val foo = 4

is not a magic number, but

var foo = 4

is considered to be a magic number.

Parameters

ParameterDescriptionTypeDefault Value
ignore Ignore string -1,0,1,2

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.MagicNumberChecker" level="warning">
 <parameters>
  <parameter name="ignore">-1,0,1,2,3</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodArgumentNamesChecker - Check that method argument names match a regular expression

Justification

The Scala style guide recommends that method argument names conform to certain standards.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[a-z][A-Za-z0-9]*$
ignoreRegex Regular expression to ignore string ^$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.MethodArgumentNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z0-9]*$</parameter>
  <parameter name="ignoreRegex">^$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodLengthChecker - Checks that methods do not exceed a maximum length

Justification

Long methods can be hard to read and understand.

Parameters

ParameterDescriptionTypeDefault Value
maxLength Maximum length integer 50
ignoreComments Ignore comments boolean false
ignoreEmpty Ignore empty lines boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.MethodLengthChecker" level="warning">
 <parameters>
  <parameter name="maxLength">50</parameter>
  <parameter name="ignoreComments">false</parameter>
  <parameter name="ignoreEmpty">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MethodNamesChecker - Check that method names match a regular expression

Justification

The Scala style guide recommends that method names conform to certain standards. If the methods are overriding another method, and the overridden method cannot be changed, then use the ignoreOverride parameter.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[a-z][A-Za-z0-9]*(_=)?$
ignoreRegex Regular expression to ignore string ^$
ignoreOverride Ignore override boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.MethodNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Za-z]*$</parameter>
  <parameter name="ignoreRegex">^.*$</parameter>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.MultipleStringLiteralsChecker - Checks that a string literal does not appear multiple times

Justification

Code duplication makes maintenance more difficult, so it can be better to replace the multiple occurrences with a constant.

Parameters

ParameterDescriptionTypeDefault Value
allowed Maximum occurences allowed integer 1
ignoreRegex Ignore regular expression string ^""$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.MultipleStringLiteralsChecker" level="warning">
 <parameters>
  <parameter name="allowed">1</parameter>
  <parameter name="ignoreRegex">^\&quot;\&quot;$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NamedArgumentChecker - Checks that argument literals are named

Justification

Nameless literals make code harder to understand (consider updateEntity(1, true) and updateEntity(id = 1, enabled = true)).

Parameters

ParameterDescriptionTypeDefault Value
checkString Check string literals boolean false
ignoreMethod Ignore methods by pattern string ^set.+$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NamedArgumentChecker" level="warning">
 <parameters>
  <parameter name="checkString">false</parameter>
  <parameter name="ignoreMethod">^set.+$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NoCloneChecker - Check that classes and objects do not define the clone() method

Justification

The clone method is difficult to get right. You can use the copy constructor of case classes rather than implementing clone. For more information on clone(), see Effective Java by Joshua Bloch pages.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NoCloneChecker" level="warning"/>

org.scalastyle.scalariform.NoFinalizeChecker - Check that classes and objects do not define the finalize() method

Justification

finalize() is called when the object is garbage collected, and garbage collection is not guaranteed to happen. It is therefore unwise to rely on code in finalize() method.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NoFinalizeChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker - No whitespace after left bracket ‘[’

Justification

If there is whitespace after a left bracket, this can be confusing to the reader

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceAfterLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker - No whitespace before left bracket ‘[’

Justification

If there is whitespace before a left bracket, this can be confusing to the reader

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeLeftBracketChecker" level="warning"/>

org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker - No whitespace before right bracket ‘’]’’

Justification

If there is whitespace before a right bracket, this can be confusing to the reader

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NoWhitespaceBeforeRightBracketChecker" level="warning"/>

org.scalastyle.scalariform.NonASCIICharacterChecker - Some editors are unfriendly to non ASCII characters.

Justification

Scala allows unicode characters as operators and some editors misbehave when they see non-ascii character. In a project collaborated by a community of developers. This check can be helpful in such situations.

"value".match {
case "value" => println("matched")
...
}

is preferred to

"value".match {
case "value" ⇒ println("matched")
...
}

To fix it, replace the (unicode operator)⇒ with =>.

Parameters

ParameterDescriptionTypeDefault Value
allowStringLiterals Allow non-ASCII scripts in string literals. boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NonASCIICharacterChecker" level="warning">
 <parameters>
  <parameter name="allowStringLiterals">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NotImplementedErrorUsage - Checks that the code does not have ??? operators.

Justification

The ??? operator denotes that an implementation is missing. This rule helps to avoid potential runtime errors because of not implemented code.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NotImplementedErrorUsage" level="warning"/>

org.scalastyle.scalariform.NullChecker - Check that null is not used

Justification

Scala discourages use of nulls, preferring Option.

Parameters

ParameterDescriptionTypeDefault Value
allowNullChecks Allow null checks boolean true

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NullChecker" level="warning">
 <parameters>
  <parameter name="allowNullChecks">true</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfMethodsInTypeChecker - Check that a class / trait / object does not have too many methods

Justification

If a type declares too many methods, this can be an indication of bad design.

Parameters

ParameterDescriptionTypeDefault Value
maxMethods Maximum methods integer 30

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NumberOfMethodsInTypeChecker" level="warning">
 <parameters>
  <parameter name="maxMethods">30</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.NumberOfTypesChecker - Checks that there are not too many types declared in a file

Justification

If there are too many classes/objects defined in a single file, this can cause the code to be difficult to understand.

Parameters

ParameterDescriptionTypeDefault Value
maxTypes Maximum Number integer 20

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.NumberOfTypesChecker" level="warning">
 <parameters>
  <parameter name="maxTypes">20</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ObjectNamesChecker - Check that object names match a regular expression

Justification

The Scala style guide recommends that object names conform to certain standards.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[A-Z][A-Za-z]*$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[A-Z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.OverrideJavaChecker - Checks that Java @Override is not used

Justification

You should be using the Scala override keyword instead.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.OverrideJavaChecker" level="warning"/>

org.scalastyle.scalariform.PackageNamesChecker - Check that package names match a regular expression

Justification

The Scala style guide recommends that package names conform to certain standards.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[a-z]+$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.PackageNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z]+$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PackageObjectNamesChecker - Check that package object names match a regular expression

Justification

The Scala style guide recommends that package object names conform to certain standards.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^[a-z][A-Za-z]*$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.PackageObjectNamesChecker" level="warning">
 <parameters>
  <parameter name="regex">^[a-z][A-Za-z]*$</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.ParameterNumberChecker - Maximum number of parameters for a method

Justification

A method which has more than a certain number of parameters can be hard to understand.

Parameters

ParameterDescriptionTypeDefault Value
maxParameters Maximum Number integer 8

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ParameterNumberChecker" level="warning">
 <parameters>
  <parameter name="maxParameters">8</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.PatternMatchAlignChecker - Check that pattern match arrows align

Justification

Correct formatting can help readability.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.PatternMatchAlignChecker" level="warning"/>

org.scalastyle.scalariform.ProcedureDeclarationChecker - Use a : Unit = for procedure declarations

Justification

A procedure style declaration can cause confusion - the developer may have simply forgotten to add a ‘=’, and now their method returns Unit rather than the inferred type:

def foo() { println("hello"); 5 }
def foo() = { println("hello"); 5 }

This checker raises a warning with the first line. To fix it, use an explicit return type, or add a ‘=’ before the body.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ProcedureDeclarationChecker" level="warning"/>

org.scalastyle.scalariform.PublicMethodsHaveTypeChecker - Check that a method has an explicit return type, it is not inferred

Justification

A public method declared on a type is effectively an API declaration. Explicitly declaring a return type means that other code which depends on that type won’t break unexpectedly.

Parameters

ParameterDescriptionTypeDefault Value
ignoreOverride Ignore overridden methods boolean false

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.PublicMethodsHaveTypeChecker" level="warning">
 <parameters>
  <parameter name="ignoreOverride">false</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.RedundantIfChecker - Checks that if expressions are not redundant, i.e. easily replaced by a variant of the condition

Justification

If expressions with boolean constants in both branches can be eliminated without affecting readability. Prefer simply cond to if (cond) true else false and !cond to if (cond) false else true.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.RedundantIfChecker" level="warning"/>

org.scalastyle.scalariform.ReturnChecker - Check that return is not used

Justification

Use of return is not usually necessary in Scala. In fact, use of return can discourage a functional style of programming.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ReturnChecker" level="warning"/>

org.scalastyle.scalariform.ScalaDocChecker - Checks that the ScalaDoc on documentable members is well-formed

Justification

Scaladoc is generally considered a good thing. Within reason.

Description

Ignore tokens is a comma separated string that may include the following : PatDefOrDcl (variables), TmplDef (classes, traits), TypeDefOrDcl (type definitions), FunDefOrDcl (functions) Supported indentation styles are “scaladoc” (for ScalaDoc-style comments, with two spaces before the asterisk), “javadoc” (for JavaDoc-style comments, with a single space before the asterisk) or “anydoc” to support any style (any number of spaces before the asterisk). For backwards compatibility, if left empty, “anydoc” will be assumed.

Parameters

ParameterDescriptionTypeDefault Value
ignoreRegex Regular expression string ^$
ignoreTokenTypes Comma Separated String string ^$
ignoreOverride Ignore override boolean false
indentStyle Force indent style string anydoc

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.ScalaDocChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">(.*Spec$)|(.*SpecIT$)</parameter>
  <parameter name="ignoreTokenTypes">PatDefOrDcl,TypeDefOrDcl,FunDefOrDcl,TmplDef</parameter>
  <parameter name="ignoreOverride">false</parameter>
  <parameter name="indentStyle">anydoc</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.SimplifyBooleanExpressionChecker - Boolean expression can be simplified

Justification

A boolean expression which can be simplified can make code easier to read.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.SimplifyBooleanExpressionChecker" level="warning"/>

org.scalastyle.scalariform.SpaceAfterCommentStartChecker - Checks a space after the start of the comment.

Justification

To bring consistency with how comments should be formatted, leave a space right after the beginning of the comment.

package foobar

object Foobar {
/**WRONG
*
*/
/** Correct*/
val d = 2 /*Wrong*/ //Wrong
/**
*Correct
*/
val e = 3/** Correct*/ // Correct
}

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.SpaceAfterCommentStartChecker" level="warning"/>

org.scalastyle.scalariform.SpacesAfterPlusChecker - Check that the plus sign is followed by a space

Justification

An expression with spaces around + can be easier to read

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.SpacesAfterPlusChecker" level="warning"/>

org.scalastyle.scalariform.SpacesBeforePlusChecker - Check that the plus sign is preceded by a space

Justification

An expression with spaces around + can be easier to read

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.SpacesBeforePlusChecker" level="warning"/>

org.scalastyle.scalariform.StructuralTypeChecker - Check that structural types are not used.

Justification

Structural types in Scala can use reflection - this can have unexpected performance consequences. Warning: This check can also wrongly pick up type lamdbas and other such constructs. This checker should be used with care. You always have the alternative of the scalac checking for structural types.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.StructuralTypeChecker" level="warning"/>

org.scalastyle.scalariform.TodoCommentChecker - Check for use of TODO/FIXME single line comments

Justification

Some projects may consider TODO or FIXME comments in a code bad style. They would rather you fix the problem.

Parameters

ParameterDescriptionTypeDefault Value
words Word list string TODO|FIXME

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.TodoCommentChecker" level="warning">
 <parameters>
  <parameter default="TODO|FIXME" type="string" name="words"/>
 </parameters>
</check>

org.scalastyle.scalariform.TokenChecker - Checks that a regular expression cannot be matched in a token, if found reports this

Justification

Some checks can be carried by just the presence of a particular token.

Parameters

ParameterDescriptionTypeDefault Value
regex Regular expression string ^$

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.TokenChecker" level="warning">
 <parameters>
  <parameter name="regex">^[ai]sInstanceOf$</parameter>
 </parameters>
 <customMessage>Avoid casting.</customMessage>
</check>

org.scalastyle.scalariform.UnderscoreImportChecker - Avoid wildcard imports

Justification

Importing all classes from a package or static members from a class leads to tight coupling between packages or classes and might lead to problems when a new version of a library introduces name clashes.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.UnderscoreImportChecker" level="warning">
 <parameters>
  <parameter name="ignoreRegex">collection\.JavaConverters\._|scala\.concurrent\.duration\._</parameter>
 </parameters>
</check>

org.scalastyle.scalariform.UppercaseLChecker - Checks that if a long literal is used, then an uppercase L is used

Justification

A lowercase L (l) can look similar to a number 1 with some fonts.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.UppercaseLChecker" level="warning"/>

org.scalastyle.scalariform.VarFieldChecker - Checks that classes and objects do not define mutable fields

Justification

var (mutable fields) are deprecated if you’re using a strict functional style

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.VarFieldChecker" level="warning"/>

org.scalastyle.scalariform.VarLocalChecker - Checks that functions do not define mutable variables

Justification

vars (mutable local variables) loops are deprecated if you’re using a strict functional style

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.VarLocalChecker" level="warning"/>

org.scalastyle.scalariform.WhileBraceChecker - Checks that while body have braces

Justification

While cannot be used in a pure-functional manner, that’s why it’s recommended to never omit braces according to Scala Style Guide.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.WhileBraceChecker" level="warning"/>

org.scalastyle.scalariform.WhileChecker - Checks that while is not used

Justification

while loops are deprecated if you’re using a strict functional style

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.WhileChecker" level="warning"/>

org.scalastyle.scalariform.XmlLiteralChecker - Check that XML literals are not used

Justification

Some projects prefer not to have XML literals. They could use a templating engine instead.

Parameters

No parameters

Example configuration

<check enabled="true" class="org.scalastyle.scalariform.XmlLiteralChecker" level="warning"/>