avoid-double-and-int-checks

Same as avoid_double_and_int_checks in Dart lints

Group
Style
Path
lint/style/avoid-double-and-int-checks
Default
On (recommended)
Origin
Dart lints
Rule
43 of 512
recommended

Avoid checking a value for double and then for int.

When compiled to JavaScript every number is a double, so integral values pass an is double test and an else if (x is int) branch is never reached there:

if (x is double) {
  handleDouble(x);
} else if (x is int) { // BAD
  handleInt(x);
}

if (x is int) { // GOOD: check `int` first
  handleInt(x);
} else if (x is double) {
  handleDouble(x);
}

Reported when an if (x is double) statement's else branch is directly if (x is int) on the same parameter or local variable, with both types resolving to the non-nullable dart:core classes. Like the official lint, this matches is! tests too; it compares the types, not the test polarity.

Invalid

example.dartdart
void handle(num x) {
  if (x is double) {
    print('double');
  } else if (x is int) {
    print('int');
  }
}

void handleDynamic(dynamic value) {
  if (value is double) {
    print('double');
  } else if (value is int) {
    print('int');
  } else {
    print('other');
  }
}

void handleLocal(Object input) {
  final number = input;
  if (number is double) {
    print('double');
  } else if (number is int) {
    print('int');
  }
}

void handleOptional([Object? x]) {
  if (x is double) print('double'); else if (x is int) print('int');
}

void handleNamed({required num x}) {
  if (x is double) {
    print('double');
  } else if (x is int) {
    print('int');
  }
}

final closure = (num x) {
  if (x is double) {
    return 1;
  } else if (x is int) {
    return 2;
  }
  return 0;
};

void negated(num x) {
  if (x is! double) {
    print('not double');
  } else if (x is int) {
    print('int');
  }
}

void negatedElse(num x) {
  if (x is double) {
    print('double');
  } else if (x is! int) {
    print('not int');
  }
}

class ShadowedFieldChecks {
  num field = 1;

  void parameter(num field) {
    if (field is double) {
      print('double');
    } else if (field is int) {
      print('int');
    }
  }

  void local() {
    final field = this.field;
    if (field is double) {
      print('double');
    } else if (field is int) {
      print('int');
    }
  }
}
Explicit check for `double` or `int`.
3 print('double');
4 } else if (x is int) {
Explicit check for `double` or `int`.
11 print('double');
12 } else if (value is int) {
Explicit check for `double` or `int`.
22 print('double');
23 } else if (number is int) {
Explicit check for `double` or `int`.
28void handleOptional([Object? x]) {
29 if (x is double) print('double'); else if (x is int) print('int');
Explicit check for `double` or `int`.
34 print('double');
35 } else if (x is int) {
Explicit check for `double` or `int`.
42 return 1;
43 } else if (x is int) {
Explicit check for `double` or `int`.
51 print('not double');
52 } else if (x is int) {
Explicit check for `double` or `int`.
59 print('double');
60 } else if (x is! int) {
Explicit check for `double` or `int`.
70 print('double');
71 } else if (field is int) {
Explicit check for `double` or `int`.
79 print('double');
80 } else if (field is int) {

Valid

example.dartdart
num field = 1;

void intFirst(num x) {
  if (x is int) {
    print('int');
  } else if (x is double) {
    print('double');
  }
}

void differentVariables(num x, num y) {
  if (x is double) {
    print('double');
  } else if (y is int) {
    print('int');
  }
}

void notElseIf(num x) {
  if (x is double) {
    print('double');
  }
  if (x is int) {
    print('int');
  }
}

void fieldCheck() {
  if (field is double) {
    print('double');
  } else if (field is int) {
    print('int');
  }
}

void nullableTypes(Object? x) {
  if (x is double?) {
    print('double');
  } else if (x is int?) {
    print('int');
  }
}


void nestedBlock(num x) {
  if (x is double) {
    print('double');
  } else {
    if (x is int) {
      print('int');
    }
  }
}

class FieldChecks {
  num field = 1;

  void check() {
    if (field is double) {
      print('double');
    } else if (field is int) {
      print('int');
    }
  }
}

Configure avoid-double-and-int-checks

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-double-and-int-checks": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-double-and-int-checks: <reason>