80 lines
2.6 KiB
Plaintext
80 lines
2.6 KiB
Plaintext
![]() |
Prefer adjacent string concatenation over using the `+` operator.
|
||
|
|
||
|
== Why is this an issue?
|
||
|
|
||
|
Given two string literals, Dart allows concatenation:
|
||
|
|
||
|
1. via the `+` operator over strings, e.g. `'Hello' + 'World'`
|
||
|
2. via adjacent string literals, e.g. `'Hello' 'World'`
|
||
|
|
||
|
The `+` operator concatenates any two expressions of type `String`, irrespective of whether the two expressions are literals or not. This means that all the following expressions are valid:
|
||
|
|
||
|
* `a literal` + `another literal`
|
||
|
* `a literal` + aVariable
|
||
|
* aVariable + `a literal`
|
||
|
|
||
|
On the other hand, adjacent string literals are a specific form of concatenation that only works with string literals and interpolated strings, which means that only the first two of the following expressions are valid:
|
||
|
|
||
|
* `a literal` `another literal`
|
||
|
* `a literal` `an interpolated ${"string"}`
|
||
|
* `a literal` aVariable
|
||
|
* aVariable `a literal`
|
||
|
|
||
|
Because concatenation of strings with variable elements should be done via https://dart.dev/language/built-in-types#strings[string interpolation] (as encouraged by S3512), consistently using adjacent string literals for concatenation of string without the variable elements (i.e. literals only) makes the code more readable and homogeneous, since a single syntax is used for all concatenations.
|
||
|
|
||
|
== How to fix it
|
||
|
|
||
|
Remove the `+` operator to have string literals adjacent to each other.
|
||
|
|
||
|
=== Code examples
|
||
|
|
||
|
==== Noncompliant code example
|
||
|
|
||
|
[source,dart,diff-id=1,diff-type=noncompliant]
|
||
|
----
|
||
|
String message = 'Hello' + 'World';
|
||
|
----
|
||
|
|
||
|
==== Compliant solution
|
||
|
|
||
|
[source,dart,diff-id=1,diff-type=compliant]
|
||
|
----
|
||
|
String message = 'Hello' 'World';
|
||
|
----
|
||
|
|
||
|
== Resources
|
||
|
|
||
|
=== Documentation
|
||
|
|
||
|
* Dart Docs - https://dart.dev/tools/linter-rules/prefer_adjacent_string_concatenation[Dart Linter rule - prefer_adjacent_string_concatenation]
|
||
|
* Dart Docs - https://dart.dev/guides/language/language-tour#strings[Dart Language Tour - Strings]
|
||
|
|
||
|
=== Related rules
|
||
|
|
||
|
* S3512 - Interpolation should be used instead of String concatenation
|
||
|
|
||
|
|
||
|
ifdef::env-github,rspecator-view[]
|
||
|
|
||
|
'''
|
||
|
== Implementation Specification
|
||
|
(visible only on this page)
|
||
|
|
||
|
=== Message
|
||
|
|
||
|
* String literals shouldn't be concatenated by the '+' operator.
|
||
|
|
||
|
=== Highlighting
|
||
|
|
||
|
* The `+` operator in the string concatenation.
|
||
|
|
||
|
If a non-parenthesized sequence of concatenations is detected, a single issue is reported: e.g. in `'a' + 'b' + 'c'`, a single issue, on the first `+` operator, is reported.
|
||
|
|
||
|
In parenthesized expressions, only innermost concatenations are reported: e.g. in `'a' + ('b' + 'c')`, only the inner `+` operator is reported.
|
||
|
|
||
|
'''
|
||
|
== Comments And Links
|
||
|
(visible only on this page)
|
||
|
|
||
|
endif::env-github,rspecator-view[]
|