Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename project from EcoCode to Creedengo #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
A project that can be used to test the latest version of the [ecoCode-charp](https://github.com/green-code-initiative/ecoCode-charp) NuGet package.
A project that can be used to test the latest version of the [creedengo-charp](https://github.com/green-code-initiative/creedengo-charp) NuGet package.

### 1. Set up your local environment

- Install/update the .NET SDK on your machine, the minimum required version is specified in the ecoCode-charp's [global.json](https://github.com/green-code-initiative/ecoCode-csharp/blob/main/global.json) file.
- Clone this repository locally on your machine. Since this project is used to test the very latest ecoCode-csharp rules, including the beta versions, it is recommended to check it out often.
- Install/update the .NET SDK on your machine, the minimum required version is specified in the creedengo-charp's [global.json](https://github.com/green-code-initiative/creedengo-csharp/blob/main/global.json) file.
- Clone this repository locally on your machine. Since this project is used to test the very latest creedengo-csharp rules, including the beta versions, it is recommended to check it out often.
- Optional : install the IDE of your choice for C# (e.g. Visual Studio, Visual Studio Code, Rider, etc.) if you want to test with a GUI.

### 2. Test the rules through the CLI
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;

namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class DontCallFunctionsInLoopConditions
{
Expand All @@ -13,7 +13,7 @@ public static void Run(int p)
{
int i, j = 0, k = 10;

// EC69 on V2(), V3(k), V3(p) and V3(C)
// GCI69 on V2(), V3(k), V3(p) and V3(C)

for (i = 0; i < V1 && i < V2() && i < V3(i) && i < V3(j) && i < V3(k) && i < V3(p) && i < V3(C); i++)
j += i;
Expand All @@ -25,7 +25,7 @@ public static void Run(int p)
while (i < V1 && i < V2() && i < V3(i) && i < V3(j) && i < V3(k) && i < V3(p) && i < V3(C));

string? d = Path.GetDirectoryName("toto");
while (d != null && !d.Equals(@"S:\", StringComparison.OrdinalIgnoreCase)) // No EC69, d is reassigned in the loop
while (d != null && !d.Equals(@"S:\", StringComparison.OrdinalIgnoreCase)) // No GCI69, d is reassigned in the loop
d = Path.GetDirectoryName(d);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using System.Data;

namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class DontExecuteSqlCommandsInLoops
{
public static void Run()
{
var command = default(IDbCommand)!; // EC82, code fix: const IDbCommand command = default!;
var command = default(IDbCommand)!; // GCI82, code fix: const IDbCommand command = default!;
_ = command.ExecuteNonQuery();
_ = command.ExecuteScalar();
_ = command.ExecuteReader();
_ = command.ExecuteReader(CommandBehavior.Default);

for (int i = 0; i < 10; i++)
{
_ = command.ExecuteNonQuery(); // EC72
_ = command.ExecuteScalar(); // EC72
_ = command.ExecuteReader(); // EC72
_ = command.ExecuteReader(CommandBehavior.Default); // EC72
_ = command.ExecuteNonQuery(); // GCI72
_ = command.ExecuteScalar(); // GCI72
_ = command.ExecuteReader(); // GCI72
_ = command.ExecuteReader(CommandBehavior.Default); // GCI72
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Immutable;

namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class DontConcatenateStringsInLoops
{
Expand All @@ -11,38 +11,38 @@ public static void DontConcatenateStringsWithParameterAllLoops(string sParam)
for (int i = 0; i < 10; i++)
{
string si = $"{i}";
sParam += si; // EC75
sParam = sParam + si; // EC75
sParam = si + sParam; // EC75
sParam += si; // GCI75
sParam = sParam + si; // GCI75
sParam = si + sParam; // GCI75
sParam = si + si;
}

foreach (int i in Enumerable.Range(0, 10))
{
string si = $"{i}";
sParam += si; // EC75
sParam = sParam + si; // EC75
sParam = si + sParam; // EC75
sParam += si; // GCI75
sParam = sParam + si; // GCI75
sParam = si + sParam; // GCI75
sParam = si + si;
}

int i2 = 0;
while (i2++ < 10)
{
string si = $"{i2}";
sParam += si; // EC75
sParam = sParam + si; // EC75
sParam = si + sParam; // EC75
sParam += si; // GCI75
sParam = sParam + si; // GCI75
sParam = si + sParam; // GCI75
sParam = si + si;
}

i2 = 0;
do
{
string si = $"{i2}";
sParam += si; // EC75
sParam = sParam + si; // EC75
sParam = si + sParam; // EC75
sParam += si; // GCI75
sParam = sParam + si; // GCI75
sParam = si + sParam; // GCI75
sParam = si + si;
} while (++i2 < 10);
}
Expand All @@ -54,9 +54,9 @@ public static void DontConcatenateStringsWithField()
for (int i = 0; i < 10; i++)
{
string si = $"{i}";
sField += si; // EC75
sField = sField + si; // EC75
sField = si + sField; // EC75
sField += si; // GCI75
sField = sField + si; // GCI75
sField = si + sField; // GCI75
sField = si + si;
}
}
Expand All @@ -67,9 +67,9 @@ public static void DontConcatenateStringsWithProperty()
for (int i = 0; i < 10; i++)
{
string si = $"{i}";
sProp += si; // EC75
sProp = sProp + si; // EC75
sProp = si + sProp; // EC75
sProp += si; // GCI75
sProp = sProp + si; // GCI75
sProp = si + sProp; // GCI75
sProp = si + si;
}
}
Expand All @@ -80,9 +80,9 @@ public static void DontConcatenateStringsWithLocal()
for (int i = 0; i < 10; i++)
{
string si = $"{i}";
sLocal += si; // EC75
sLocal = sLocal + si; // EC75
sLocal = si + sLocal; // EC75
sLocal += si; // GCI75
sLocal = sLocal + si; // GCI75
sLocal = si + sLocal; // GCI75
sLocal = si + si;

string sLocalInLoop = string.Empty;
Expand All @@ -105,26 +105,26 @@ public static void DontConcatenateStringsWithForEach<T>()
Array.ForEach(arr, i =>
{
string si = $"{i}";
sLocal += si; // EC75
sLocal = sLocal + si; // EC75
sLocal = si + sLocal; // EC75
sLocal += si; // GCI75
sLocal = sLocal + si; // GCI75
sLocal = si + sLocal; // GCI75
sLocal = si + si;
});
Array.ForEach(arr, i => sLocal += $"{i}"); // EC75
Array.ForEach(arr, i => sLocal = sLocal + $"{i}"); // EC75
Array.ForEach(arr, i => sLocal = $"{i}" + sLocal); // EC75
Array.ForEach(arr, i => sLocal += $"{i}"); // GCI75
Array.ForEach(arr, i => sLocal = sLocal + $"{i}"); // GCI75
Array.ForEach(arr, i => sLocal = $"{i}" + sLocal); // GCI75
Array.ForEach(arr, i => sLocal = $"{i}" + $"{i}");

var list = new List<T>();
list.ForEach(i =>
{
string si = $"{i}";
sLocal += si; // EC75
sLocal = sLocal + si; // EC75
sLocal = si + sLocal; // EC75
sLocal += si; // GCI75
sLocal = sLocal + si; // GCI75
sLocal = si + sLocal; // GCI75
sLocal = si + si;
});
list.ForEach(i => sLocal += $"{i}"); // EC75
list.ForEach(i => sLocal += $"{i}"); // GCI75
list.ForEach(i => sLocal = sLocal + $"{i}"); // EC75
list.ForEach(i => sLocal = $"{i}" + sLocal); // EC75
list.ForEach(i => sLocal = $"{i}" + $"{i}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System.Runtime.InteropServices;

namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class SpecifyStructLayout
{
public readonly record struct TestStruct1;

public readonly record struct TestStruct2(int A);

public readonly record struct TestStruct3(int A, double B); // EC81, code fix: Add StructLayout attribute (Auto or Sequential)
public readonly record struct TestStruct3(int A, double B); // GCI81, code fix: Add StructLayout attribute (Auto or Sequential)

[StructLayout(LayoutKind.Auto)]
public readonly record struct TestStruct4(int A, double B);

public readonly record struct TestStruct5(int A, string B);

public readonly record struct TestStruct6(int A, double B, int C); // EC81, code fix: Add StructLayout attribute (Auto or Sequential)
public readonly record struct TestStruct6(int A, double B, int C); // GCI81, code fix: Add StructLayout attribute (Auto or Sequential)

public readonly record struct TestStruct7(bool A, int B, char C, ulong E, DateTime F); // EC81, code fix: Add StructLayout attribute (Auto or Sequential)
public readonly record struct TestStruct7(bool A, int B, char C, ulong E, DateTime F); // GCI81, code fix: Add StructLayout attribute (Auto or Sequential)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class VariableCanBeMadeConstant
{
public static void Run()
{
int i0 = 0; // EC82, code fix: const int i0 = 0;
int i0 = 0; // GCI82, code fix: const int i0 = 0;
Console.WriteLine(i0);

int i1 = 0;
Expand All @@ -24,20 +24,20 @@ public static void Run()
Console.WriteLine(i5);
Console.WriteLine(i6);

int i7 = 0, i8 = 0; // EC82, code fix: const int i7 = 0, i8 = 0;
int i7 = 0, i8 = 0; // GCI82, code fix: const int i7 = 0, i8 = 0;
Console.WriteLine(i7);
Console.WriteLine(i8);

object o = "abc";
Console.WriteLine(o);

string s = "abc"; // EC82, code fix: const string s = "abc";
string s = "abc"; // GCI82, code fix: const string s = "abc";
Console.WriteLine(s);

var v0 = 4; // EC82, code fix: const int item = 4;
var v0 = 4; // GCI82, code fix: const int item = 4;
Console.WriteLine(v0);

var v1 = "abc"; // EC82, code fix: const string item = "abc";
var v1 = "abc"; // GCI82, code fix: const string item = "abc";
Console.WriteLine(v1);
}
}
28 changes: 28 additions & 0 deletions RuleTests/Creedengo/GCI83.ReplaceEnumToStringWithNameOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace RuleTests.Creedengo;

internal static class ReplaceEnumToStringWithNameOf
{
private enum MyEnum { A, B, C, D }

public static void Run()
{
Console.WriteLine(nameof(MyEnum.A));
Console.WriteLine(nameof(MyEnum.B));
Console.WriteLine(nameof(MyEnum.C));
Console.WriteLine(nameof(MyEnum.D));

Console.WriteLine(MyEnum.A.ToString()); // GCI83, code fix: nameof(MyEnum.A)
Console.WriteLine(MyEnum.B.ToString("")); // GCI83, code fix: nameof(MyEnum.B)
Console.WriteLine(MyEnum.C.ToString(string.Empty)); // GCI83, code fix: nameof(MyEnum.C)
Console.WriteLine(MyEnum.D.ToString(format: null)); // GCI83, code fix: nameof(MyEnum.D)

Console.WriteLine(MyEnum.A.ToString("G")); // GCI83, code fix: nameof(MyEnum.A)
Console.WriteLine(MyEnum.B.ToString("F")); // GCI83, code fix: nameof(MyEnum.B)
Console.WriteLine(MyEnum.C.ToString("N"));

Console.WriteLine($"{MyEnum.A}"); // GCI83, code fix: nameof(MyEnum.A)
Console.WriteLine($"{MyEnum.B:G}"); // GCI83, code fix: nameof(MyEnum.B)
Console.WriteLine($"{MyEnum.C:F}"); // GCI83, code fix: nameof(MyEnum.C)
Console.WriteLine($"{MyEnum.D:N}");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Net.Http;

namespace RuleTests.EcoCode;
namespace RuleTests.Creedengo;

internal static class AvoidAsyncVoidMethods
{
public static async void Method1() => // EC84, code fix: public static async Task Method1()
public static async void Method1() => // GCI84, code fix: public static async Task Method1()
await Task.Delay(1000).ConfigureAwait(false);

public static async Task Method2() =>
Expand All @@ -16,7 +16,7 @@ public static async Task<int> Method3()
return 1;
}

public static async void Method4() // EC84, code fix: public static async Task Method4()
public static async void Method4() // GCI84, code fix: public static async Task Method4()
{
using var httpClient = new HttpClient();
_ = await httpClient.GetAsync(new Uri("URL")).ConfigureAwait(false);
Expand Down
Loading