Skip to content

Commit

Permalink
Create rule S7095: Unnecessary braces in string interpolation should …
Browse files Browse the repository at this point in the history
…be removed (unnecessary_brace_in_string_interps) (#4325)

Co-authored-by: antonioaversa <[email protected]>
  • Loading branch information
github-actions[bot] and antonioaversa authored Sep 26, 2024
1 parent 8fe5b5c commit 4f54acf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rules/S7095/dart/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Unnecessary braces in string interpolation should be removed",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "1min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7095",
"sqKey": "S7095",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "FORMATTED"
}
}
70 changes: 70 additions & 0 deletions rules/S7095/dart/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Unnecessary braces in string interpolation should be removed.

== Why is this an issue?

Dart https://dart.dev/language/built-in-types#strings[strings] support interpolation, which allows you to embed expressions within a string.

[source,dart]
----
String expression = 'words';
print('A string containing ${expression}');
----

When the expression is a simple identifier, and the identifier is followed in the string interpolation by a character which is not allowed in an identifier (e.g. whitespace, `:`, `-` but not a letter, digit, ``++_++``, ...), you can omit the curly braces.

This makes the code more readable and concise.

[source,dart]
----
String expression = 'words';
print('A string containing $expression');
----

== How to fix it

Remove the curly braces.

=== Code examples

==== Noncompliant code example

[source,dart,diff-id=1,diff-type=noncompliant]
----
String expression = 'words';
print('A string containing ${expression}');
----

==== Compliant solution

[source,dart,diff-id=1,diff-type=compliant]
----
String expression = 'words';
print('A string containing $expression');
----

== Resources

=== Documentation

* Dart Docs - https://dart.dev/tools/linter-rules/unnecessary_brace_in_string_interps[Dart Linter rule - unnecessary_brace_in_string_interps]
* Dart Docs - https://dart.dev/language/built-in-types#strings[Language - Strings]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Unnecessary braces in a string interpolation.

=== Highlighting

The interpolation expression, including the ``++$++`` sign and the braces: e.g. ``++${expression}++`` in ``++'A string containing ${expression}'++``.

'''
== Comments And Links
(visible only on this page)

endif::env-github,rspecator-view[]
2 changes: 2 additions & 0 deletions rules/S7095/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 4f54acf

Please sign in to comment.