prefer-interpolation-to-compose-strings
Same as prefer_interpolation_to_compose_strings in Dart lints
- Group
- Style
- Path
lint/style/prefer-interpolation-to-compose-strings- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 375 of 512
Prefers string interpolation to composing strings with +.
Each + chain (a + b + c, flattened through unparenthesized left
operands) is scanned pair by pair: a pair is reported, from its left
operand to its right one, when exactly one side is a string literal (plain,
interpolated, or adjacent), neither side is a single raw literal (r'...')
nor a toString(...) call with arguments, and the left operand is a
String. The pair after a reported one is skipped. A non-+ binary
expression as a chain's left operand ends the chain (a - b + 'x' checks
only 'x'), and a parenthesized operand is an ordinary operand.
Each left spine is collected iteratively once; preorder visits its prefixes
before any operand changes semantic flow, so the root already covers every
greedy pair a prefix would report.
falcon stays silent when it cannot infer the left operand's type.
Invalid
String greeting(String name, String city) {
return 'Hello, ' + name + ' from ' + city;
}
String wrap(String value) => value;
String nested(String name) {
return 'outer ' + wrap('inner ' + name);
}
String grouped(String name) => ('a' + name) + 'b';
String suffix(String name) => name + '!';
String afterInterpolation(String name, String tail) => 'Hello $name' + tail;String greeting(String name, String city) { return 'Hello, ' + name + ' from ' + city;String greeting(String name, String city) { return 'Hello, ' + name + ' from ' + city;String nested(String name) { return 'outer ' + wrap('inner ' + name);String nested(String name) { return 'outer ' + wrap('inner ' + name);}String grouped(String name) => ('a' + name) + 'b';}String grouped(String name) => ('a' + name) + 'b';String grouped(String name) => ('a' + name) + 'b';String suffix(String name) => name + '!';String suffix(String name) => name + '!';String afterInterpolation(String name, String tail) => 'Hello $name' + tail;Valid
String greeting(String name, String city) => 'Hello, $name from $city';
String adjacent = 'hello ' 'world';
String raw(String value) => r'raw' + value;
class CustomAdd {
CustomAdd operator +(String value) => this;
}
CustomAdd custom(CustomAdd value) => value + 'text';
// Two literals, interpolated or not, are not a composition.
String interpolated(String name) => 'Hello $name' + '!';
class Padded {
@override
String toString({int pad = 0}) => '';
}
String padded(Padded value) => 'x' + value.toString(pad: 1);Configure prefer-interpolation-to-compose-strings
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-interpolation-to-compose-strings": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-interpolation-to-compose-strings: <reason>More style rules
- prefer-is-empty
prefer_is_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isEmptycheck. - prefer-is-not-empty
prefer_is_not_emptyrecommendedFlags
.lengthcomparisons equivalent to a.isNotEmptycheck. - prefer-is-not-operator
prefer_is_not_operatorrecommendedFlags a negated type test
!(x is T)that should use theis!operator. - prefer-iterable-of
prefer-iterable-ofrecommendedFlags
.fromcollection constructors in favor of the.ofconstructor. - prefer-iterable-where-type
prefer_iterable_whereTyperecommendedFlags
.where((e) => e is T), which.whereType<T>()expresses directly. - prefer-last
prefer-lastFlags manual last-element indexing in favor of the
.lastgetter. - prefer-match-file-name
Match a file's sole class or enum name to its snake-case basename.
- prefer-mixin
prefer_mixinrecommendedFlags
withclauses that mix in a plain class instead of a mixin.