Code inspection: Redundant switch expression arms
This inspection identifies redundant or unreachable switch arms within a switch expression. Such scenarios typically occur when certain arms are overridden by other matching patterns or when their execution conditions overlap.
The inspection flags these redundant arms and recommends removing them to enhance code clarity and maintainability.
In the example below, the arm int => false
is unnecessary because the _ => false
arm will handle all cases that must return false
.
bool IsString(object o)
{
return o switch
{
string => true,
int => false,
_ => false
};
}
bool IsString(object o)
{
return o switch
{
string => true,
_ => false
};
}
By removing redundant switch arms, the resulting code becomes simpler, improves clarity, and adheres to best practices.
Last modified: 26 March 2025