Code inspection: Heuristically unreachable code
Among other things, ReSharper analyzes values and nullability and integer arithmetics, and uses the results of these analyses to identify code that never executes.
In the example below, this inspection reports Console.WriteLine("Error");
as unreachable, because the program can only enter the foreach
loop if the list
is not null. Therefore, the condition list == null
will always be false inside this loop, and as a result, any code within this condition will never execute.
class Sample
{
void Test(IEnumerable<string> list)
{
foreach (var str in list)
{
if (list == null)
Console.WriteLine("Error"); // Code is unreachable
}
}
}
Last modified: 27 May 2024