Skip to content

Commit

Permalink
Scalaformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
t1b00 committed Aug 16, 2024
1 parent 42cfc3d commit 397ccba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
29 changes: 12 additions & 17 deletions input/src/main/scala/fix/UnreachableCatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 13 additions & 14 deletions rules/src/main/scala/fix/UnreachableCatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 397ccba

Please sign in to comment.