Skip to content

Commit

Permalink
Potentially annoying lints for -Wperformance
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 2, 2022
1 parent 2e197ac commit f47930a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/compiler/scala/tools/nsc/settings/Warnings.scala
Expand Up @@ -98,7 +98,7 @@ trait Warnings {
|to prevent the shell from expanding patterns.""".stripMargin),
prepend = true)

// Non-lint warnings. -- TODO turn into MultiChoiceEnumeration
// Non-lint warnings.
val warnMacros = ChoiceSetting(
name = "-Wmacros",
helpArg = "mode",
Expand All @@ -117,6 +117,20 @@ trait Warnings {
val warnNumericWiden = BooleanSetting("-Wnumeric-widen", "Warn when numerics are widened.") withAbbreviation "-Ywarn-numeric-widen"
val warnOctalLiteral = BooleanSetting("-Woctal-literal", "Warn on obsolete octal syntax.") withAbbreviation "-Ywarn-octal-literal"

object PerformanceWarnings extends MultiChoiceEnumeration {
val Captured = Choice("captured", "Modification of var in closure causes boxing.")
val NonlocalReturn = Choice("nonlocal-return", "A return statement used an exception for flow control.")
}
val warnPerformance = MultiChoiceSetting(
name = "-Wperformance",
helpArg = "warning",
descr = "Enable or disable specific lints for performance",
domain = PerformanceWarnings,
default = Some(List("_"))
)
def warnCaptured = warnPerformance.contains(PerformanceWarnings.Captured)
def warnNonlocalReturn = warnPerformance.contains(PerformanceWarnings.NonlocalReturn)

object UnusedWarnings extends MultiChoiceEnumeration {
val Imports = Choice("imports", "Warn if an import selector is not referenced.")
val PatVars = Choice("patvars", "Warn if a variable bound in a pattern is unused.")
Expand Down Expand Up @@ -182,7 +196,6 @@ trait Warnings {
val StrictUnsealedPatMat = LintWarning("strict-unsealed-patmat", "Pattern match on an unsealed class without a catch-all.")
val Constant = LintWarning("constant", "Evaluation of a constant arithmetic expression resulted in an error.")
val Unused = LintWarning("unused", "Enable -Wunused:imports,privates,locals,implicits,nowarn.")
val NonlocalReturn = LintWarning("nonlocal-return", "A return statement used an exception for flow control.")
val ImplicitNotFound = LintWarning("implicit-not-found", "Check @implicitNotFound and @implicitAmbiguous messages.")
val Serial = LintWarning("serial", "@SerialVersionUID on traits and non-serializable classes.")
val ValPattern = LintWarning("valpattern", "Enable pattern checks in val definitions.")
Expand All @@ -194,7 +207,6 @@ trait Warnings {
val UnitSpecialization = LintWarning("unit-special", "Warn for specialization of Unit in parameter position.")
val MultiargInfix = LintWarning("multiarg-infix", "Infix operator was defined or used with multiarg operand.")
val ImplicitRecursion = LintWarning("implicit-recursion", "Implicit resolves to an enclosing definition.")
val Captured = LintWarning("captured", "Modification of var in closure causes boxing.")

def allLintWarnings = values.toSeq.asInstanceOf[Seq[LintWarning]]
}
Expand All @@ -215,9 +227,7 @@ trait Warnings {
def warnStrictUnsealedPatMat = lint contains StrictUnsealedPatMat
def warnStarsAlign = lint contains StarsAlign
def warnConstant = lint contains Constant
def warnCaptured = lint contains Captured
def lintUnused = lint contains Unused
def warnNonlocalReturn = lint contains NonlocalReturn
def lintImplicitNotFound = lint contains ImplicitNotFound
def warnSerialization = lint contains Serial
def lintValPatterns = lint contains ValPattern
Expand Down
5 changes: 4 additions & 1 deletion test/files/neg/xlint-captured.check
@@ -1,6 +1,9 @@
xlint-captured.scala:10: warning: return statement uses an exception to pass control to the caller of the enclosing named method f
def f(): Unit = List(42).foreach(i => if (i > 27) return)
^
xlint-captured.scala:5: warning: Modification of variable c within a closure causes it to be boxed.
var c = a // nok
^
error: No warnings can be incurred under -Werror.
1 warning
2 warnings
1 error
4 changes: 3 additions & 1 deletion test/files/neg/xlint-captured.scala
@@ -1,9 +1,11 @@
// scalac: -Werror -Xlint:captured
// scalac: -Werror -Wperformance
object Test {
var a, b = 0 // ok
def mkStrangeCounter(): Int => Int = {
var c = a // nok
object _d { var d = b }; import _d._ // ok
e => { c += a; d += b; a *= b; b -= c; c ^ d }
}

def f(): Unit = List(42).foreach(i => if (i > 27) return)
}

0 comments on commit f47930a

Please sign in to comment.