Conversion applied to a value that already has that type
ID |
dart.performance.noop_primitive_operations |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Redundant Operation |
Language |
Dart |
Description
Reports a conversion call whose receiver already has the target type: toString on a string literal or on an expression that just produced a String, toInt on an integer literal or on an expression that just produced an int, and toDouble on a double literal or on an expression that just produced a double.
Rationale
The call cannot change the value, so it is pure noise for the reader and one more frame for the compiler to inline away. Worse, it hides intent: a reader who sees .toString() assumes the receiver was not a string and starts looking for the conversion that is not there, and a redundant .toInt() left over after a refactoring is indistinguishable from a real narrowing that has to be preserved. Removing them makes the conversions that do matter visible.
The following code illustrates the pattern detected by this rule:
class Report {
// FLAGGED: Conversion applied to a value that already has that type
String title() => 'weekly'.toString();
}