Skip to content

Commit 60b9638

Browse files
authored
Merge pull request #368 from green-code-initiative/vlj/GCI90
2 parents 5e5b079 + 84109d3 commit 60b9638

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- GCI90 C#: Use `Cast` instead of `Select` to cast.
11+
1012
### Added
1113

1214
### Changed

RULES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Some are applicable for different technologies.
6666
| GCI87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
6767
| GCI88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
6868
| GCI89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited data | ||||||||
69+
| GCI90 | Use `Cast` instead of `Select` to cast | `Cast` is optimized for this scenario and should be used | ||||||||
6970
| GCI91 | Use `Where` before `OrderBy` | Filter elements before sorting them for improved efficiency | ||||||||
7071
| GCI92 | Use string.Length instead of comparison with empty string | 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. | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
7172
| GCI93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | ||||||||

src/main/rules/GCI90/GCI90.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"title": "Use Cast instead of Select to cast",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant\/Issue",
7+
"constantCost": "5min"
8+
},
9+
"tags": [
10+
"eco-design",
11+
"creedengo",
12+
"performance",
13+
"bad-practice"
14+
],
15+
"defaultSeverity": "Major"
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
:!sectids:
2+
3+
Use Cast instead of Select to cast.
4+
5+
== Why is this an issue ?
6+
7+
Cast is more efficient than Select for casting operations, its usage leads to better performance.
8+
9+
=== When can it be ignored ?
10+
11+
This rule shouldn't be ignored.
12+
13+
== Non compliant example
14+
15+
[source, cs]
16+
----
17+
void Test(IEnumerable<string> items)
18+
{
19+
var asObjects = items.Select(x => (object)x);
20+
}
21+
----
22+
23+
== Compliant example
24+
25+
[source, cs]
26+
----
27+
void Test(IEnumerable<string> items)
28+
{
29+
var asObjects = items.Cast<object>();
30+
}
31+
----

src/main/rules/GCI91/GCI91.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
"bad-practice"
1414
],
1515
"defaultSeverity": "Major"
16-
}
16+
}

0 commit comments

Comments
 (0)