diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-09-09 16:59:43 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-09-09 18:20:01 +0200 |
commit | 24b0711f01dcb410ffd0454881f7a96073f92e16 (patch) | |
tree | da48f345062d67de9a6a344db556253d1d864bbc /src | |
parent | adf2d3632b07eef4fc2303aef994e66584a73f49 (diff) | |
download | scala-24b0711f01dcb410ffd0454881f7a96073f92e16.tar.gz scala-24b0711f01dcb410ffd0454881f7a96073f92e16.tar.bz2 scala-24b0711f01dcb410ffd0454881f7a96073f92e16.zip |
SI-6276 Warn on def or val that trivially loops infinitely
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/RefChecks.scala | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 166bb2d18c..dd4f13fd7b 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -1197,6 +1197,18 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R case _ => } + // SI-6276 warn for `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think. + def checkInfiniteLoop(valOrDef: ValOrDefDef) { + val trivialInifiniteLoop = ( + !valOrDef.isErroneous + && !valOrDef.symbol.isValueParameter + && valOrDef.symbol.paramss.isEmpty + && valOrDef.rhs.hasSymbolWhich(_.accessedOrSelf == valOrDef.symbol) + ) + if (trivialInifiniteLoop) + unit.warning(valOrDef.rhs.pos, s"${valOrDef.symbol.fullLocationString} does nothing other than call itself recursively") + } + // Transformation ------------------------------------------------------------ /* Convert a reference to a case factory of type `tpe` to a new of the class it produces. */ @@ -1640,6 +1652,7 @@ abstract class RefChecks extends InfoTransform with reflect.internal.transform.R case ValDef(_, _, _, _) | DefDef(_, _, _, _, _, _) => checkDeprecatedOvers(tree) + checkInfiniteLoop(tree.asInstanceOf[ValOrDefDef]) if (settings.warnNullaryUnit.value) checkNullaryMethodReturnType(sym) if (settings.warnInaccessible.value) { |