@@ -1127,22 +1127,16 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
1127
1127
case _ =>
1128
1128
}
1129
1129
1130
- // SI-6276 warn for `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think.
1131
- def checkInfiniteLoop (valOrDef : ValOrDefDef ) {
1132
- def callsSelf = valOrDef.rhs match {
1133
- case t @ (Ident (_) | Select (This (_), _)) =>
1134
- t hasSymbolWhich (_.accessedOrSelf == valOrDef.symbol)
1135
- case _ => false
1130
+ // SI-6276 warn for trivial recursion, such as `def foo = foo` or `val bar: X = bar`, which come up more frequently than you might think.
1131
+ // TODO: Move to abide rule. Also, this does not check that the def is final or not overridden, for example
1132
+ def checkInfiniteLoop (sym : Symbol , rhs : Tree ): Unit =
1133
+ if (! sym.isValueParameter && sym.paramss.isEmpty) {
1134
+ rhs match {
1135
+ case t@ (Ident (_) | Select (This (_), _)) if t hasSymbolWhich (_.accessedOrSelf == sym) =>
1136
+ reporter.warning(rhs.pos, s " ${sym.fullLocationString} does nothing other than call itself recursively " )
1137
+ case _ =>
1138
+ }
1136
1139
}
1137
- val trivialInfiniteLoop = (
1138
- ! valOrDef.isErroneous
1139
- && ! valOrDef.symbol.isValueParameter
1140
- && valOrDef.symbol.paramss.isEmpty
1141
- && callsSelf
1142
- )
1143
- if (trivialInfiniteLoop)
1144
- reporter.warning(valOrDef.rhs.pos, s " ${valOrDef.symbol.fullLocationString} does nothing other than call itself recursively " )
1145
- }
1146
1140
1147
1141
// Transformation ------------------------------------------------------------
1148
1142
@@ -1621,9 +1615,18 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
1621
1615
sym resetFlag DEFERRED
1622
1616
transform(deriveDefDef(tree)(_ => typed(gen.mkSysErrorCall(" native method stub" ))))
1623
1617
1624
- case ValDef (_, _, _, _) | DefDef (_, _, _, _, _, _) =>
1618
+ case Assign (tgt, rhs) =>
1619
+ if (! tree.isErroneous)
1620
+ checkInfiniteLoop(tgt.symbol, rhs)
1621
+ tree
1622
+
1623
+ // NOTE: The fields phase has moved the RHS of ValDefs in classes to an initialization statement in the template.
1624
+ case tree : ValOrDefDef =>
1625
1625
checkDeprecatedOvers(tree)
1626
- checkInfiniteLoop(tree.asInstanceOf [ValOrDefDef ])
1626
+
1627
+ if (! tree.isErroneous)
1628
+ checkInfiniteLoop(tree.symbol, tree.rhs)
1629
+
1627
1630
if (settings.warnNullaryUnit)
1628
1631
checkNullaryMethodReturnType(sym)
1629
1632
if (settings.warnInaccessible) {
0 commit comments