Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 5.09 KB

Analyzer.md

File metadata and controls

58 lines (44 loc) · 5.09 KB

commons-analyzer

This module contains a simple static analyzer for Scala, implemented as a compiler plugin.

Here's how to configure it in build.sbt:

val avsCommonsVersion: String = ???
addCompilerPlugin("com.avsystem.commons" %% "commons-analyzer" % avsCommonsVersion)

Analyzer runs after typechecker inside the Scala compiler and applies its rules on every file. Every rule can be disabled or enabled to yield a compilation error, warning or info. It is recommended to use warnings with -Werror option for the Scala compiler enabled and @nowarn annotation for warning suppression.

Here's a list of currently supported rules:

Name Default level Description
importJavaUtil warning Rejects direct imports of java.util package.
valueEnumExhaustiveMatch warning Enables (limited) exhaustive pattern match checking for ValueEnums.
any2stringadd off Disables any2stringadd (concatenating arbitrary values with strings using +).
bincompat warning Enables @bincompat checking
showAst error Handles @showAst.
findUsages warning Issues a message for every occurrence of any of the symbols listed in the argument of this rule
varargsAtLeast warning Enables @atLeast checking
macroPrivate warning Enables @macroPrivate checking
explicitGenerics warning Enables @explicitGenerics checking
discardedMonixTask warning Makes sure that expressions evaluating to Monix Tasks are not accidentally discarded by conversion to Unit
throwableObjects warning Makes sure that objects are never used as Throwables (unless they have stack traces disabled)
constantDeclarations off Checks if constants are always declared as final vals with UpperCamelCase and no explicit type annotation for literal values
basePackage warning Checks if all sources are within the specified base package

Rules may be enabled and disabled in build.sbt with Scala compiler options:

scalacOptions += "-P:AVSystemAnalyzer:<level><ruleName>[:<arg>],<level><ruleName>[:<arg>],..."

<ruleName> must be one of the rule names listed above or _ to apply to all rules.

<level> may be one of:

  • - to disable rule
  • * for "info" level
  • empty for "warning" level
  • + for "error" level

<arg> is an argument specific for given rule

For example, this options sets all rules on "error" level except for constantDeclarations which is disabled and discardedMonixTask which is lowered to "warning" level. The basePackage rule is also lowered to "warning" level and com.avsystem.commons is specified as the base package.

scalacOptions += "-P:AVSystemAnalyzer:+_,-constantDeclarations,discardedMonixTask,basePackage:com.avsystem.commons