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
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);
}class Bar { Foo foo() => Foo.fromBar(this); Foo convert() { Foo _wrap() => new Foo(this); Foo tofoo() => Foo(this); Foo assign() => Foo.fromBar(this);extension BarX on Bar { Foo view() => Foo(this);class Negatable { Negatable operator ~() => Wrapped(this);Valid
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.
{
"linter": {
"rules": {
"style": {
"use-to-and-as-if-applicable": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-to-and-as-if-applicable: <reason>More style rules
- always-declare-return-types
always_declare_return_typesrecommendedFlags functions and methods declared without a return type.
- always-put-control-body-on-new-line
always_put_control_body_on_new_lineFlags a control-flow body that starts on the same line as its control clause.
- always-put-required-named-parameters-first
always_put_required_named_parameters_firstFlags a required named parameter declared after an optional named one.
- always-specify-types
always_specify_typesRequires explicit type annotations everywhere a type could be inferred.
- annotate-overrides
annotate_overridesrecommendedRequires
@overrideon members that override an inherited declaration. - arguments-ordering
Named arguments follow their resolved parameter declaration order by default.
- avoid-abbreviations-in-doc-comments
avoid_abbreviations_in_doc_commentsFlags Latin abbreviations in doc comments.
- avoid-annotating-with-dynamic
avoid_annotating_with_dynamicFlags parameters explicitly annotated with
dynamic.