A catch tests the type of the caught exception with is, where a more specific catch or a filter would do
What it finds
catch (Exception e) { if (e is IOException) ... else throw; } catches everything, then sorts. A catch (IOException) clause, or a filter when (e is IOException), leaves other exceptions alone, keeps their stack unwound only once and states the intent. The rule reports is tests of the caught variable directly inside the catch block.
From the rule's source, type_tested_in_catch.rs.
using System;using System.IO; class Copier{ void Copy() { try { Run(); } catch (Exception e) { if (e is IOException)tx0 reports: the caught exception is sorted by type inside the catch { return; } throw; } } void Run() { }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:type-tested-in-catch" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:type-tested-in-catch] reason="..."tx0 explain tx0/csharp:type-tested-in-catch shows the same in the terminal. Found a false positive? Report it.