Scalafix

Scalafix

  • User guide
  • Developer guide
  • Browse sources
  • GitHub

›Rules

Usage

  • Installation
  • Configuration
  • Suppressing rules

Rules

  • Built-in rules
  • DisableSyntax
  • ExplicitResultTypes
  • LeakingImplicitClassVal
  • NoAutoTupling
  • NoValInForComprehension
  • ProcedureSyntax
  • RedundantSyntax
  • RemoveUnused
  • Using external rules
  • Community rules

Misc

  • Related projects
Edit

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.

NameTypeDescription
noVarsBooleanReport error for usage of vars.
noThrowsBooleanReport error for throwing exceptions.
noNullsBooleanReport error for usage of null.
noReturnsBooleanReport error for usage of return.
noWhileLoopsBooleanReport error for usage of while loops.
noAsInstanceOfBooleanReport error for usage of asInstanceOf[T] casts.
noIsInstanceOfBooleanReport error for usage of isInstanceOf[T] checks.
noXmlBooleanReport error on xml literals.
noDefaultArgsBooleanReport error on method parameters with default arguments.
noFinalValBooleanReport error on `final` modifier for val definitions, see [motivation](https://github.com/sbt/zinc/issues/227)
noFinalizeBooleanReports error when finalize is overridden.
noValPatternsBooleanReport error when pattern matching in val assignment with non-tuple patterns.
noUniversalEqualityBooleanReport error on `==` (universal equality)
noUniversalEqualityMessageStringReporter message for noUniversalEquality
regexList[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."
  }
]
  1. The first way has an object providing an id, pattern, and message.
  2. 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.
  3. 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.

← Built-in rulesExplicitResultTypes →
  • Configuration
    • Defaults
    • Examples
  • Regex
    • Error Messages
Scalafix
Docs
Get startedRulesExtend Scalafix
Community
Chat on DiscordDiscuss on Scala Users
More
GitHub
Copyright © 2023 Scala Center