Skip to content

Commit

Permalink
Merge pull request #368 from green-code-initiative/vlj/GCI90
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 authored Jan 19, 2025
2 parents 5e5b079 + 84109d3 commit 60b9638
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- GCI90 C#: Use `Cast` instead of `Select` to cast.

### Added

### Changed
Expand Down
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Some are applicable for different technologies.
| GCI87 | Use collection indexer | Collection indexers should be used instead of Linq, when available | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| GCI88 | Dispose resource asynchronously | Resources that implement `IAsyncDisposable` should be disposed asynchronously | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| GCI89 | Avoid using function cache without limit | If a function has decorators without max size cache, the program will store unlimited data | ||||||||
| GCI90 | Use `Cast` instead of `Select` to cast | `Cast` is optimized for this scenario and should be used | ||||||||
| GCI91 | Use `Where` before `OrderBy` | Filter elements before sorting them for improved efficiency | ||||||||
| 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. | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| GCI93 | Return `Task` directly | Consider returning a `Task` directly instead of a single `await` | ||||||||
Expand Down
16 changes: 16 additions & 0 deletions src/main/rules/GCI90/GCI90.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Use Cast instead of Select to cast",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"creedengo",
"performance",
"bad-practice"
],
"defaultSeverity": "Major"
}
31 changes: 31 additions & 0 deletions src/main/rules/GCI90/csharp/GCI90.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
:!sectids:

Use Cast instead of Select to cast.

== Why is this an issue ?

Cast is more efficient than Select for casting operations, its usage leads to better performance.

=== When can it be ignored ?

This rule shouldn't be ignored.

== Non compliant example

[source, cs]
----
void Test(IEnumerable<string> items)
{
var asObjects = items.Select(x => (object)x);
}
----

== Compliant example

[source, cs]
----
void Test(IEnumerable<string> items)
{
var asObjects = items.Cast<object>();
}
----
2 changes: 1 addition & 1 deletion src/main/rules/GCI91/GCI91.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"bad-practice"
],
"defaultSeverity": "Major"
}
}

0 comments on commit 60b9638

Please sign in to comment.