-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EC92] [C#] Use string Length (#342)
Co-authored-by: Baptiste HENRY <[email protected]> Co-authored-by: Vianney de Bellabre <[email protected]>
- Loading branch information
1 parent
bfe52cc
commit 52885f4
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
ecocode-rules-specifications/src/main/rules/EC92/EC92.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"title": "Use string.Length instead of comparison with empty string", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"eco-design", | ||
"ecocode", | ||
"performance", | ||
"bad-practice" | ||
], | ||
"defaultSeverity": "Minor" | ||
} |
39 changes: 39 additions & 0 deletions
39
ecocode-rules-specifications/src/main/rules/EC92/csharp/EC92.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
:!sectids: | ||
|
||
Use string.Length instead of comparison with empty string | ||
|
||
## Why is this an issue ? | ||
|
||
Comparing a string to an empty string is unnecessary and can be replaced by a call to `string.Length`, which is more performant and more readable. | ||
|
||
### When can it be ignored ? | ||
|
||
This rule should not be ignored. | ||
|
||
## Non-compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public void Main() | ||
{ | ||
string s = ""; | ||
if (s == "") // Non-compliant | ||
{ | ||
Console.WriteLine("Empty string"); | ||
} | ||
} | ||
---- | ||
|
||
## Compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public void Main() | ||
{ | ||
string s = ""; | ||
if (s.Length == 0) // Compliant | ||
{ | ||
Console.WriteLine("Empty string"); | ||
} | ||
} | ||
---- |