Skip to content

Commit

Permalink
feat: rename rule keys from ECXXX to GCIXXX
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent314 committed Dec 10, 2024
1 parent b0855eb commit c30f95a
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 149 deletions.
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 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
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,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
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 c30f95a

Please sign in to comment.