From 301e3c6f64d66e2d5a399b430854476d35e77055 Mon Sep 17 00:00:00 2001 From: Vianney de Bellabre Date: Sat, 18 Jan 2025 18:21:28 +0100 Subject: [PATCH 1/3] GCI90 --- CHANGELOG.md | 2 ++ RULES.md | 1 + src/main/rules/GCI90/GCI90.json | 16 +++++++++++ src/main/rules/GCI90/csharp/GCI90.asciidoc | 31 ++++++++++++++++++++++ src/main/rules/GCI91/GCI91.json | 4 +-- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/rules/GCI90/GCI90.json create mode 100644 src/main/rules/GCI90/csharp/GCI90.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index c631d0cc..4a4a9196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/RULES.md b/RULES.md index 3d8c5098..0cbcdc7c 100644 --- a/RULES.md +++ b/RULES.md @@ -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 | | ❓ | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | +| GCI91 | 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` | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ | diff --git a/src/main/rules/GCI90/GCI90.json b/src/main/rules/GCI90/GCI90.json new file mode 100644 index 00000000..4bff7a4c --- /dev/null +++ b/src/main/rules/GCI90/GCI90.json @@ -0,0 +1,16 @@ +{ + "title": "Use Where before OrderBy", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "creedengo", + "performance", + "bad-practice" + ], + "defaultSeverity": "Major" +} \ No newline at end of file diff --git a/src/main/rules/GCI90/csharp/GCI90.asciidoc b/src/main/rules/GCI90/csharp/GCI90.asciidoc new file mode 100644 index 00000000..474ec10b --- /dev/null +++ b/src/main/rules/GCI90/csharp/GCI90.asciidoc @@ -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, it's usage leads to better performance. + +=== When can it be ignored ? + +This rule shouldn't be ignored. + +== Non compliant example + +[source, cs] +---- +void Test(IEnumerable items) +{ + var asObjects = items.Select(x => (object)x); +} +---- + +== Compliant example + +[source, cs] +---- +void Test(IEnumerable items) +{ + var asObjects = items.Cast(); +} +---- diff --git a/src/main/rules/GCI91/GCI91.json b/src/main/rules/GCI91/GCI91.json index 4bff7a4c..31fe98ac 100644 --- a/src/main/rules/GCI91/GCI91.json +++ b/src/main/rules/GCI91/GCI91.json @@ -1,5 +1,5 @@ { - "title": "Use Where before OrderBy", + "title": "Use Cast instead of Select to cast", "type": "CODE_SMELL", "status": "ready", "remediation": { @@ -13,4 +13,4 @@ "bad-practice" ], "defaultSeverity": "Major" -} \ No newline at end of file +} From 4e5640c8bb9df55899ca258b0073eadc35f7acf7 Mon Sep 17 00:00:00 2001 From: Vianney de Bellabre Date: Sat, 18 Jan 2025 18:23:28 +0100 Subject: [PATCH 2/3] Fix --- src/main/rules/GCI90/GCI90.json | 4 ++-- src/main/rules/GCI91/GCI91.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/rules/GCI90/GCI90.json b/src/main/rules/GCI90/GCI90.json index 4bff7a4c..31fe98ac 100644 --- a/src/main/rules/GCI90/GCI90.json +++ b/src/main/rules/GCI90/GCI90.json @@ -1,5 +1,5 @@ { - "title": "Use Where before OrderBy", + "title": "Use Cast instead of Select to cast", "type": "CODE_SMELL", "status": "ready", "remediation": { @@ -13,4 +13,4 @@ "bad-practice" ], "defaultSeverity": "Major" -} \ No newline at end of file +} diff --git a/src/main/rules/GCI91/GCI91.json b/src/main/rules/GCI91/GCI91.json index 31fe98ac..00f09ead 100644 --- a/src/main/rules/GCI91/GCI91.json +++ b/src/main/rules/GCI91/GCI91.json @@ -1,5 +1,5 @@ { - "title": "Use Cast instead of Select to cast", + "title": "Use Where before OrderBy", "type": "CODE_SMELL", "status": "ready", "remediation": { From 84109d3235bd85452dedc0308a0d4eaa62148b5a Mon Sep 17 00:00:00 2001 From: Vianney de Bellabre Date: Sun, 19 Jan 2025 02:20:44 +0100 Subject: [PATCH 3/3] Fix --- RULES.md | 2 +- src/main/rules/GCI90/csharp/GCI90.asciidoc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/RULES.md b/RULES.md index 0cbcdc7c..ac6cb7ef 100644 --- a/RULES.md +++ b/RULES.md @@ -66,7 +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 | | ❓ | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| GCI91 | Use `Cast` instead of `Select` to cast | `Cast` is optimized for this scenario and should be used | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ | +| 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` | | ❓ | ❓ | ❓ | ❓ | ❓ | ✅ | ❓ | diff --git a/src/main/rules/GCI90/csharp/GCI90.asciidoc b/src/main/rules/GCI90/csharp/GCI90.asciidoc index 474ec10b..ed965763 100644 --- a/src/main/rules/GCI90/csharp/GCI90.asciidoc +++ b/src/main/rules/GCI90/csharp/GCI90.asciidoc @@ -4,7 +4,7 @@ Use Cast instead of Select to cast. == Why is this an issue ? -Cast is more efficient than Select for casting operations, it's usage leads to better performance. +Cast is more efficient than Select for casting operations, its usage leads to better performance. === When can it be ignored ?