avoid-constant-switches
- Group
- Correctness
- Path
lint/correctness/avoid-constant-switches- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 40 of 512
Flags switch statements and expressions with a statically constant subject. Replace the switch with the matching branch.
Invalid
const selected = 2;
void example() {
switch (selected) {
case 2: print('two');
default: print('other');
}
final value = switch ('yes') {
'yes' => 1,
_ => 0,
};
switch (1 + 2) {
case 3: print(value);
}
}
enum Choice { first, second }
class Settings {
static const selected = 2;
}
void enumAndStatic() {
switch (Choice.first) {
case Choice.first: break;
case Choice.second: break;
}
switch (Settings.selected) {
case 2: break;
default: break;
}
final value = switch (Choice.second) {
Choice.first => 1,
Choice.second => 2,
};
print(value);
}void example() { switch (selected) { } final value = switch ('yes') { }; switch (1 + 2) {void enumAndStatic() { switch (Choice.first) { } switch (Settings.selected) { } final value = switch (Choice.second) {Valid
const selected = 2;
void example(int selected) {
switch (selected) {
case 2: print('two');
default: print('other');
}
final value = switch (selected) {
2 => 'two',
_ => 'other',
};
print(value);
}
void runtimeAndPatterns(Object value, List<int> values) {
for (final selected in values) {
switch (selected) {
case 2: break;
default: break;
}
}
if (value case int selected) {
final result = switch (selected) { 2 => true, _ => false };
print(result);
}
final result = [
for (final selected in values)
switch (selected) { 2 => true, _ => false },
];
const local = 2;
// Captured locals have no environment-dependency provenance.
switch (local) {
case 2: break;
default: break;
}
switch (const int.fromEnvironment('NUMBER')) {
case 2: break;
default: break;
}
print(result);
}
const environment = String.fromEnvironment('MODE');
const derived = environment == 'debug';
void environmentSwitch() {
switch (derived) {
case true: break;
case false: break;
}
}
const shadowedGetter = 1;
class GetterShadow {
int get shadowedGetter => DateTime.now().millisecondsSinceEpoch;
void check() {
switch (shadowedGetter) {
case 1: break;
default: break;
}
}
}
class InheritedGetterShadow extends GetterShadow {
void inheritedCheck() {
switch (shadowedGetter) {
case 1: break;
default: break;
}
}
}
void shadowedMethod() {}
class MethodShadow {
void shadowedMethod() {}
void check() {
switch (shadowedMethod) {
default: break;
}
}
}
const extensionFlag = true;
extension on String {
bool get extensionFlag => isEmpty;
void checkUnnamedExtension() {
switch (extensionFlag) {
case true: break;
case false: break;
}
}
}
extension NamedFlag on List<int> {
bool get extensionFlag => isEmpty;
void checkNamedExtension() {
switch (extensionFlag) {
case true: break;
case false: break;
}
}
}
class FreshValue { const FreshValue(); }
void freshAllocationSwitches() {
switch (<int>[] == <int>[]) { case true: break; default: break; }
switch (<int>{} != <int>{}) { case true: break; default: break; }
switch (identical(<int, int>{}, <int, int>{})) { case true: break; default: break; }
switch (FreshValue() == FreshValue()) { case true: break; default: break; }
final result = switch (identical((<int>[],), (<int>[],))) { true => 1, _ => 2 };
print(result);
}
void nestedRecordSwitch() {
switch ((<int>[],) == (<int>[],)) { case true: break; default: break; }
}Configure avoid-constant-switches
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-constant-switches": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-constant-switches: <reason>More correctness rules
- avoid-contradictory-expressionsrecommended
Flags pairs of conditions that are always false with
&&or true with||. - avoid-disposing-late-fieldsflutter
Avoid releasing late State fields that may be unset or initialized only by cleanup.
- avoid-duplicate-bloc-event-handlersrecommendedbloc
Flags later registrations of the same event type in a Bloc constructor.
- avoid-duplicate-cascades
Flags repeated field or index assignment targets within one cascade.
- avoid-duplicate-collection-elements
Flags repeated scalar literals and stable local reads in list and set literals.
- avoid-duplicate-exports
Flags export directives whose visible names are already covered by another export.
- avoid-duplicate-factories
Flags factories in one type with the same implementation, ignoring factory names and alpha-renaming parameters by position.
- avoid-duplicate-initializers
Flags final/const variables with identical initializers in the same scope.