no-runtime-type-to-string
Same as no_runtimeType_toString in Dart lints
- Group
- Style
- Path
lint/style/no-runtime-type-to-string- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 295 of 512
Avoid calling toString() on runtimeType.
runtimeType.toString() forces the runtime to keep type names around and is
slow; in production builds the names may also be minified:
class Point {
@override
String toString() => '$runtimeType()'; // BAD
}
Reported for this.runtimeType, super.runtimeType, or an implicit-this
runtimeType getter that is either interpolated or has toString() called
on it. Uses inside asserts, throw expressions, catch clauses, mixins, and
abstract classes are exempt. Extension-target checks are syntactic: an
unqualified named target is checked only if it names a non-abstract class
declared in this file, Object, or one of the extension's type parameters.
Non-named targets, including dynamic, void, function and record types,
are checked. Falcon limitation: other named targets are exempt, including
prefixed targets, external classes and Never; this is not a semantic test
of whether the target is concrete.
Invalid
class Point {
final int x;
Point(this.x);
@override
String toString() => '$runtimeType($x)';
String describe() => runtimeType.toString();
String explicit() => this.runtimeType.toString();
String viaSuper() => super.runtimeType.toString();
String braced() => 'Instance of ${this.runtimeType}';
void log() {
final callback = () => print('$runtimeType');
callback();
}
}
enum Color {
red;
String label() => '$runtimeType.$name';
}
extension on Point {
String tag() => runtimeType.toString();
}
class ExplicitDespiteShadow {
String describe() => this.runtimeType.toString();
void unrelated(String runtimeType) {}
}
class ExemptionRestoration {
ExemptionRestoration()
: assert(runtimeType.toString().isNotEmpty) {
print('$runtimeType');
}
void inspect() {
try {
print('$runtimeType');
} catch (error) {
print('$runtimeType');
} finally {
print('$runtimeType');
}
print('$runtimeType');
}
}
class PrimaryInitializer(int value) {
this : assert(runtimeType.toString().isNotEmpty);
}
abstract class ExemptBeforeConcrete {
String describe() => '$runtimeType';
}
class CheckedAfterAbstract {
String describe() => '$runtimeType';
} @override String toString() => '$runtimeType($x)'; String describe() => runtimeType.toString(); String explicit() => this.runtimeType.toString(); String viaSuper() => super.runtimeType.toString(); String braced() => 'Instance of ${this.runtimeType}'; void log() { final callback = () => print('$runtimeType'); String label() => '$runtimeType.$name';extension on Point { String tag() => runtimeType.toString();class ExplicitDespiteShadow { String describe() => this.runtimeType.toString(); : assert(runtimeType.toString().isNotEmpty) { print('$runtimeType'); try { print('$runtimeType'); } finally { print('$runtimeType'); } print('$runtimeType');class PrimaryInitializer(int value) { this : assert(runtimeType.toString().isNotEmpty);class CheckedAfterAbstract { String describe() => '$runtimeType';Valid
abstract class Base {
@override
String toString() => '$runtimeType';
}
mixin Describe {
String describe() => runtimeType.toString();
}
class Checked {
void check(int value) {
assert(value > 0, '$runtimeType: value must be positive');
if (value > 10) {
throw StateError('${runtimeType.toString()} overflow');
}
try {
print(value);
} catch (e) {
print('$runtimeType failed: $e');
}
}
String other(Object value) => value.runtimeType.toString();
String plain() => toString();
}
class Shadowed {
String describe(String runtimeType) => runtimeType.toString();
}
String topLevel(Object value) => '${value.runtimeType}';
extension on int {
String tag() => runtimeType.toString();
}
class LaterShadow {
String describe() => '$runtimeType';
void unrelated() {
final runtimeType = 'local';
print(runtimeType);
}
}
class InitializerExemptions {
InitializerExemptions()
: assert(runtimeType.toString().isNotEmpty, '$runtimeType');
}
// INVALID-CODE: annotation arguments need not be constant for this syntactic rule.
class IgnoredConstructorMetadata {
@Tag(this.runtimeType.toString())
IgnoredConstructorMetadata();
factory IgnoredConstructorMetadata.redirect() =
Target<void Function(@Tag(this.runtimeType.toString()) int value)>;
}Configure no-runtime-type-to-string
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"no-runtime-type-to-string": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/no-runtime-type-to-string: <reason>More style rules
- non-constant-identifier-names
non_constant_identifier_namesrecommendedFlags non-constant identifiers that are not written in lowerCamelCase.
- omit-local-variable-types
omit_local_variable_typesFlags local variable type annotations that repeat the initializer's type.
- only-throw-errors
only_throw_errorsrecommendedOnly throw instances of classes that extend
ExceptionorError. - package-names
package_namesrecommendedcross-fileRequire package names to be valid
lower_case_with_underscoresidentifiers. - parameter-assignments
parameter_assignmentsDon't reassign parameters of functions, methods, or constructors.
- parameters-ordering
Requires named parameters to follow the explicitly configured alphabetical order.
- pattern-fields-ordering
Flags named object and record pattern fields that violate the configured order.
- prefer-adjacent-string-concatenation
prefer_adjacent_string_concatenationrecommendedFlags two string literals joined with the
+operator.