avoid-duplicate-switch-case-conditions
preset: recommended
Warns when several switch cases have the same condition.
Duplicate conditions in 'when' statements can lead to some switch expression cases being skipped, resulting in either unused code or incorrect behavior.
Example
❌ Bad:
void fn(SomeClass param) {
final value = switch (param) {
// LINT: Avoid duplicate switch case conditions. Try removing the duplicate condition or replacing it with a different condition.
Sub() when param.value.isEmpty => 1,
// LINT: Avoid duplicate switch case conditions. Try removing the duplicate condition or replacing it with a different condition.
Sub() when param.value.isEmpty => 2,
_ => 3,
};
}
class SomeClass {
final String value;
const SomeClass(this.value);
}
class Sub extends SomeClass {
const Sub(super.value);
}
✅ Good:
void fn(SomeClass param) {
final value = switch (param) {
Sub() when param.value.isEmpty => 1,
Sub() when param.value.length == 2 => 2, // Correct, different condition
_ => 3,
};
}
class SomeClass {
final String value;
const SomeClass(this.value);
}
class Sub extends SomeClass {
const Sub(super.value);
}