diff --git a/input/src/main/scala/fix/UnreachableCatch.scala b/input/src/main/scala/fix/UnreachableCatch.scala index af9c5ca..b3797be 100644 --- a/input/src/main/scala/fix/UnreachableCatch.scala +++ b/input/src/main/scala/fix/UnreachableCatch.scala @@ -10,34 +10,29 @@ object UnreachableCatch { @nowarn // Some unreachable warning may appear thanks to Scala built-in warnings. // Since we are testing the rule, we can ignore them. def test(): Unit = { - try { - } catch { - case _ : Throwable => // scalafix: ok; - case e : Exception => // assert: UnreachableCatch + try {} catch { + case _: Throwable => // scalafix: ok; + case e: Exception => // assert: UnreachableCatch } // Test case 2 with unreachability due to guard - try { - } catch { - case e: RuntimeException => // scalafix: ok; + try {} catch { + case e: RuntimeException => // scalafix: ok; case e: RuntimeException if e.getMessage.contains("foo") => // assert: UnreachableCatch } - try { - } catch { - case e : RuntimeException => // scalafix: ok; - case f : Exception => // scalafix: ok; - case x : Throwable => // scalafix: ok; + try {} catch { + case e: RuntimeException => // scalafix: ok; + case f: Exception => // scalafix: ok; + case x: Throwable => // scalafix: ok; } - try { - } catch { + try {} catch { case e: RuntimeException if e.getMessage.contains("foo") => // scalafix: ok; - case e: RuntimeException => // scalafix: ok; + case e: RuntimeException => // scalafix: ok; } - try { - } catch { + try {} catch { case e: RuntimeException if e.getMessage.contains("foo") => // scalafix: ok; case e: RuntimeException if e.getMessage.contains("bar") => // scalafix: ok; } diff --git a/rules/src/main/scala/fix/UnreachableCatch.scala b/rules/src/main/scala/fix/UnreachableCatch.scala index 9b3cf9d..bc47348 100644 --- a/rules/src/main/scala/fix/UnreachableCatch.scala +++ b/rules/src/main/scala/fix/UnreachableCatch.scala @@ -19,26 +19,25 @@ class UnreachableCatch extends SemanticRule("UnreachableCatch") { LintSeverity.Warning ) - override def fix(implicit doc: SemanticDocument): Patch = { doc.tree.collect { case Term.Try(_, cases, _) => val types = mutable.HashSet[String]() cases.collect { - case c @ Case(Pat.Typed(_, tpe), guard, _) => - if(types.exists(t => Util.inheritsFrom(tpe.symbol, t))) Patch.lint(diag(c.pos)) - else { - tpe.symbol.info.foreach(_.signature match { - case TypeSignature(_, _, TypeRef(_, symbol, _)) if guard.isEmpty => types.add(symbol.value) // Extract upperbound symbol of type - // We only add it if there is no guard because if we have twice the same type they trivially inherit from each other. - // By adding only if there is no guard, we will flag later ones with guards as unreachable, because they will be. See test case 2 - case _ => () - }) - Patch.empty - } - case _ => Patch.empty - } + case c @ Case(Pat.Typed(_, tpe), guard, _) => + if (types.exists(t => Util.inheritsFrom(tpe.symbol, t))) Patch.lint(diag(c.pos)) + else { + tpe.symbol.info.foreach(_.signature match { + case TypeSignature(_, _, TypeRef(_, symbol, _)) if guard.isEmpty => types.add(symbol.value) // Extract upperbound symbol of type + // We only add it if there is no guard because if we have twice the same type they trivially inherit from each other. + // By adding only if there is no guard, we will flag later ones with guards as unreachable, because they will be. See test case 2 + case _ => () + }) + Patch.empty + } + case _ => Patch.empty + } }.flatten.asPatch } }