use-to-and-as-if-applicable

Same as use_to_and_as_if_applicable in Dart lints

Group
Style
Path
lint/style/use-to-and-as-if-applicable
Default
Off, opt in
Origin
Dart lints
Rule
509 of 512

Flags conversion methods not named to… or as….

A parameterless method that just wraps the receiver in a new object — return Other(this); — is a conversion. Dart convention names such methods toX() when they copy state into a new object and asX() when they return a view backed by the original, so readers know what the call costs. The diagnostic covers the method name or operator token.

class Bar {
  Foo foo() => Foo.fromBar(this);    // flagged
  Foo toFoo() => Foo.fromBar(this);  // preferred
}

Non-getter methods and operators with no parameters, a non-void return type, and a body consisting of one constructor call taking only this are checked. Methods overriding inherited methods are exempt. Constructor calls cannot be resolved when Dart's newer new(...) declaration syntax failed to parse.

Invalid

example.dartdart
class Foo {
  final Bar bar;
  Foo(this.bar);
  Foo.fromBar(this.bar);
}

class Bar {
  Foo foo() => Foo.fromBar(this);

  Foo convert() {
    return Foo(this);
  }

  Foo _wrap() => new Foo(this);

  Foo tofoo() => Foo(this);

  Foo assign() => Foo.fromBar(this);

  void use() => _wrap();
}

extension BarX on Bar {
  Foo view() => Foo(this);
}

class Negatable {
  Negatable operator ~() => Wrapped(this);
}
class Wrapped extends Negatable {
  Wrapped(Negatable value);
}
Start the name of the method with `to` or `as`.
7class Bar {
8 Foo foo() => Foo.fromBar(this);
Start the name of the method with `to` or `as`.
9
10 Foo convert() {
Start the name of the method with `to` or `as`.
13
14 Foo _wrap() => new Foo(this);
Start the name of the method with `to` or `as`.
15
16 Foo tofoo() => Foo(this);
Start the name of the method with `to` or `as`.
17
18 Foo assign() => Foo.fromBar(this);
Start the name of the method with `to` or `as`.
23extension BarX on Bar {
24 Foo view() => Foo(this);
Start the name of the method with `to` or `as`.
27class Negatable {
28 Negatable operator ~() => Wrapped(this);

Valid

example.dartdart
class Foo {
  final Bar bar;
  Foo(this.bar);
  Foo.fromBar(this.bar);
  static Foo make(Bar bar) => Foo(bar);
}

class Bar {
  Foo toFoo() => Foo.fromBar(this);
  Foo asFoo() => Foo(this);
  Foo _toFoo() => Foo(this);
  Foo _asFoo() => Foo(this);

  // Takes a parameter.
  Foo combine(Bar other) => Foo(other);

  // Getters are not checked.
  Foo get foo => Foo(this);

  // Static method call, not a constructor.
  Foo make() => Foo.make(this);

  // More than the receiver is passed.
  Pair pair() => Pair(this, 1);

  // More than one statement.
  Foo build() {
    print('building');
    return Foo(this);
  }
}

class Pair {
  Pair(Bar bar, int n);
}

// Overrides an inherited method.
abstract class Convertible {
  Foo convert();
}

class Baz extends Bar implements Convertible {
  @override
  Foo convert() => Foo(this);
}

Configure use-to-and-as-if-applicable

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "use-to-and-as-if-applicable": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/use-to-and-as-if-applicable: <reason>