prefer-is-not-operator

Same as prefer_is_not_operator in Dart lints

Group
Style
Path
lint/style/prefer-is-not-operator
Default
On (recommended)
Origin
Dart lints
Rule
378 of 512
recommended

Flags a negated type test !(x is T) that should use the is! operator.

Dart provides the is! operator precisely so negated type checks read directly as x is! T instead of wrapping the test in parentheses and negating it, which is easier to misread and to accidentally mis-parenthesize. The rule fires on a logical-not applied to a non-negated is expression; a test that already uses is! produces no negation to flag.

Invalid

example.dartdart
void f(Object x) {
  if (!(x is int)) {}
  var a = !(x is String);
  final b = !(x is List<int>);
  final c = !(x is double);
  print(!(x is bool));
  print('$a $b $c');
}
Prefer using the `is!` operator.
1void f(Object x) {
2 if (!(x is int)) {}
Prefer using the `is!` operator.
2 if (!(x is int)) {}
3 var a = !(x is String);
Prefer using the `is!` operator.
3 var a = !(x is String);
4 final b = !(x is List<int>);
Prefer using the `is!` operator.
4 final b = !(x is List<int>);
5 final c = !(x is double);
Prefer using the `is!` operator.
5 final c = !(x is double);
6 print(!(x is bool));

Valid

example.dartdart
void f(Object x) {
  if (x is! int) {}
  var a = x is String;
  final b = !(x is! double);
  var c = !someBool(x);
  print(x is bool);
  print('$a $b $c');
}

bool someBool(Object x) => true;

Configure prefer-is-not-operator

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-is-not-operator": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-is-not-operator: <reason>
  • prefer-iterable-ofprefer-iterable-ofrecommended

    Flags .from collection constructors in favor of the .of constructor.

  • prefer-iterable-where-typeprefer_iterable_whereTyperecommended

    Flags .where((e) => e is T), which .whereType<T>() expresses directly.

  • prefer-lastprefer-last

    Flags manual last-element indexing in favor of the .last getter.

  • prefer-match-file-name

    Match a file's sole class or enum name to its snake-case basename.

  • prefer-mixinprefer_mixinrecommended

    Flags with clauses that mix in a plain class instead of a mixin.

  • prefer-multi-bloc-providerbloc

    Flatten directly nested package:flutter_bloc BlocProviders with MultiBlocProvider.

  • prefer-multi-providerprovider

    Prefer a flat MultiProvider to directly nested provider widgets.

  • prefer-named-boolean-parameters

    Flags explicit positional bool parameters in private declarations and unconstrained closures.