Skip to content

Commit

Permalink
Update before rename (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Vianney de Bellabre <[email protected]>
  • Loading branch information
Djoums and Vianney de Bellabre authored Jan 4, 2025
1 parent b0855eb commit ec3c9e4
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 211 deletions.
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
28 changes: 0 additions & 28 deletions RuleTests/EcoCode/EC83.ReplaceEnumToStringWithNameOf.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ 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
Expand Up @@ -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,23 +54,24 @@ 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;
}
}

private static string sProp { get; set; } = string.Empty;
private static string PropStr { get; set; } = string.Empty;

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 + si;
PropStr += si; // GCI75
PropStr = PropStr + si; // GCI75
PropStr = si + PropStr; // GCI75
PropStr = si + si;
}
}

Expand All @@ -80,9 +81,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,55 +106,55 @@ 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 = sLocal + $"{i}"); // EC75
list.ForEach(i => sLocal = $"{i}" + sLocal); // EC75
list.ForEach(i => sLocal += $"{i}"); // GCI75
list.ForEach(i => sLocal = sLocal + $"{i}"); // GCI75
list.ForEach(i => sLocal = $"{i}" + sLocal); // GCI75
list.ForEach(i => sLocal = $"{i}" + $"{i}");

var immList = ImmutableList<T>.Empty;
immList.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;
});
immList.ForEach(i => sLocal += $"{i}"); // EC75
immList.ForEach(i => sLocal = sLocal + $"{i}"); // EC75
immList.ForEach(i => sLocal = $"{i}" + sLocal); // EC75
immList.ForEach(i => sLocal += $"{i}"); // GCI75
immList.ForEach(i => sLocal = sLocal + $"{i}"); // GCI75
immList.ForEach(i => sLocal = $"{i}" + sLocal); // GCI75
immList.ForEach(i => sLocal = $"{i}" + $"{i}");

_ = Parallel.ForEach(list, 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;
});
_ = Parallel.ForEach(list, i => sLocal += $"{i}"); // EC75
_ = Parallel.ForEach(list, i => sLocal = sLocal + $"{i}"); // EC75
_ = Parallel.ForEach(list, i => sLocal = $"{i}" + sLocal); // EC75
_ = Parallel.ForEach(list, i => sLocal += $"{i}"); // GCI75
_ = Parallel.ForEach(list, i => sLocal = sLocal + $"{i}"); // GCI75
_ = Parallel.ForEach(list, i => sLocal = $"{i}" + sLocal); // GCI75
_ = Parallel.ForEach(list, i => sLocal = $"{i}" + $"{i}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ internal static class SpecifyStructLayout

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
Expand Up @@ -4,7 +4,7 @@ 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/EcoCode/GCI83.ReplaceEnumToStringWithNameOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace RuleTests.EcoCode;

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
Expand Up @@ -4,7 +4,7 @@ namespace RuleTests.EcoCode;

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

0 comments on commit ec3c9e4

Please sign in to comment.