|
| 1 | +package com.exasol.common.sbt |
| 2 | + |
| 3 | +import sbt._ |
| 4 | +import wartremover.Wart |
| 5 | +import wartremover.Warts |
| 6 | + |
| 7 | +/** Compiler related settings (flags, warts, lints) */ |
| 8 | +object Compilation { |
| 9 | + |
| 10 | + def compilerFlagsFn(scalaVersion: String): Seq[String] = |
| 11 | + CrossVersion.partialVersion(scalaVersion) match { |
| 12 | + case Some((2, 13)) => CompilerFlags.filter(_ != "-Xfuture") |
| 13 | + case Some((2, 12)) => CompilerFlags ++ Scala12CompilerFlags |
| 14 | + case Some((2, 11)) => CompilerFlags |
| 15 | + case _ => CompilerFlags |
| 16 | + } |
| 17 | + |
| 18 | + def consoleFlagsFn(scalaVersion: String): Seq[String] = |
| 19 | + compilerFlagsFn(scalaVersion).filterNot( |
| 20 | + Set( |
| 21 | + "-Xfatal-warnings", |
| 22 | + "-Ywarn-unused-import", |
| 23 | + "-Ywarn-unused:imports" |
| 24 | + ) |
| 25 | + ) |
| 26 | + |
| 27 | + // format: off |
| 28 | + /** Compiler flags specific to Scala version 2.12.x */ |
| 29 | + private val Scala12CompilerFlags: Seq[String] = Seq( |
| 30 | + "-Xlint:constant", // Evaluation of a constant arithmetic expression results in an error. |
| 31 | + "-Xlint:by-name-right-associative", // By-name parameter of right associative operator. |
| 32 | + "-Xlint:nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'. |
| 33 | + "-Xlint:unsound-match", // Pattern match may not be typesafe. |
| 34 | + "-Yno-adapted-args", // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver. |
| 35 | + "-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined. |
| 36 | + "-Ywarn-inaccessible", // Warn about inaccessible types in method signatures. |
| 37 | + "-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`. |
| 38 | + "-Ywarn-nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'. |
| 39 | + "-Ywarn-nullary-unit", // Warn when nullary methods return Unit. |
| 40 | + "-Ywarn-unused:implicits", // Warn if an implicit parameter is unused. |
| 41 | + "-Ywarn-unused:imports", // Warn if an import selector is not referenced. |
| 42 | + "-Ywarn-unused:locals", // Warn if a local definition is unused. |
| 43 | + // "-Ywarn-unused:params", // Warn if a value parameter is unused. |
| 44 | + "-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused. |
| 45 | + "-Ywarn-unused:privates", // Warn if a private member is unused. |
| 46 | + "-Ypartial-unification" // Enable partial unification in type constructor inference |
| 47 | + ) |
| 48 | + |
| 49 | + /** |
| 50 | + * Compiler flags specific to Scala versions 2.10.x and 2.11.x |
| 51 | + * |
| 52 | + * From tpolecat, https://tpolecat.github.io/2017/04/25/scalac-flags.html |
| 53 | + */ |
| 54 | + private val CompilerFlags: Seq[String] = Seq( |
| 55 | + "-encoding", "utf-8", // Specify character encoding used by source files. |
| 56 | + "-deprecation", // Emit warning and location for usages of deprecated APIs. |
| 57 | + "-explaintypes", // Explain type errors in more detail. |
| 58 | + "-feature", // Emit warning and location for usages of features that should be imported explicitly. |
| 59 | + "-language:existentials", // Existential types (besides wildcard types) can be written and inferred |
| 60 | + "-language:experimental.macros", // Allow macro definition (besides implementation and application) |
| 61 | + "-language:higherKinds", // Allow higher-kinded types |
| 62 | + "-language:implicitConversions", // Allow definition of implicit functions called views |
| 63 | + "-unchecked", // Enable additional warnings where generated code depends on assumptions. |
| 64 | + "-Xcheckinit", // Wrap field accessors to throw an exception on uninitialized access. |
| 65 | + "-Xfatal-warnings", // Fail the compilation if there are any warnings. |
| 66 | + "-Xfuture", // Turn on future language features. |
| 67 | + "-Xlint:adapted-args", // Warn if an argument list is modified to match the receiver. |
| 68 | + "-Xlint:delayedinit-select", // Selecting member of DelayedInit. |
| 69 | + "-Xlint:doc-detached", // A Scaladoc comment appears to be detached from its element. |
| 70 | + "-Xlint:inaccessible", // Warn about inaccessible types in method signatures. |
| 71 | + "-Xlint:infer-any", // Warn when a type argument is inferred to be `Any`. |
| 72 | + "-Xlint:missing-interpolator", // A string literal appears to be missing an interpolator id. |
| 73 | + "-Xlint:nullary-unit", // Warn when nullary methods return Unit. |
| 74 | + "-Xlint:option-implicit", // Option.apply used implicit view. |
| 75 | + "-Xlint:package-object-classes", // Class or object defined in package object. |
| 76 | + "-Xlint:poly-implicit-overload", // Parameterized overloaded implicit methods are not visible as view bounds. |
| 77 | + "-Xlint:private-shadow", // A private field (or class parameter) shadows a superclass field. |
| 78 | + "-Xlint:stars-align", // Pattern sequence wildcard must align with sequence component. |
| 79 | + "-Xlint:type-parameter-shadow", // A local type parameter shadows a type already in scope. |
| 80 | + "-Ywarn-dead-code", // Warn when dead code is identified. |
| 81 | + "-Ywarn-numeric-widen", // Warn when numerics are widened. |
| 82 | + "-Ywarn-value-discard" // Warn when non-Unit expression results are unused. |
| 83 | + ) |
| 84 | + // format: on |
| 85 | + |
| 86 | + val JavacCompilerFlags: Seq[String] = Seq( |
| 87 | + "-encoding", |
| 88 | + "UTF-8", |
| 89 | + "-deprecation", |
| 90 | + "-parameters", |
| 91 | + "-Xlint:all" |
| 92 | + ) |
| 93 | + |
| 94 | + private def contribWart(name: String) = |
| 95 | + Wart.custom(s"org.wartremover.contrib.warts.$name") |
| 96 | + |
| 97 | + private val ExtraWartremoverFlags = Seq( |
| 98 | + contribWart("Apply"), |
| 99 | + contribWart("ExposedTuples"), |
| 100 | + contribWart("MissingOverride"), |
| 101 | + contribWart("NoNeedForMonad"), |
| 102 | + contribWart("OldTime"), |
| 103 | + contribWart("SealedCaseClass"), |
| 104 | + contribWart("SomeApply"), |
| 105 | + contribWart("SymbolicName"), |
| 106 | + contribWart("UnintendedLaziness"), |
| 107 | + contribWart("UnsafeInheritance") |
| 108 | + ) |
| 109 | + |
| 110 | + val WartremoverFlags: Seq[Wart] = ExtraWartremoverFlags ++ Warts.allBut( |
| 111 | + Wart.Any, |
| 112 | + Wart.AsInstanceOf, |
| 113 | + Wart.Equals, |
| 114 | + Wart.IsInstanceOf, |
| 115 | + Wart.Null, |
| 116 | + Wart.MutableDataStructures, |
| 117 | + Wart.Overloading, |
| 118 | + Wart.Throw, |
| 119 | + Wart.Var, |
| 120 | + Wart.While |
| 121 | + ) |
| 122 | + |
| 123 | + val WartremoverTestFlags: Seq[Wart] = ExtraWartremoverFlags ++ Warts.allBut( |
| 124 | + Wart.Any, |
| 125 | + Wart.IsInstanceOf, |
| 126 | + Wart.Overloading, |
| 127 | + Wart.NonUnitStatements, |
| 128 | + Wart.Null, |
| 129 | + Wart.Var |
| 130 | + ) |
| 131 | + |
| 132 | +} |
0 commit comments