Skip to content

Commit

Permalink
feat: Add rule for modifier order and fix violations (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Nov 8, 2023
1 parent 1b2f1aa commit 1e5eabc
Show file tree
Hide file tree
Showing 45 changed files with 60 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,5 @@ spelling_exclusion_path = .\exclusion.dic
spelling_checkable_types = strings,identifiers,comments
spelling_error_severity = error

# Custom Rules
dotnet_diagnostic.IDE0036.severity = warning
1 change: 1 addition & 0 deletions EssentialCSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F97BC459-B5B9-4326-9E0D-D085FD4DEF96}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
.runsettings = .runsettings
after.EssentialCSharp.sln.targets = after.EssentialCSharp.sln.targets
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter03/Table03.02.ArrayHighlights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_02;
public class ArrayHighlights
{
// 1.
static public void Declaration()
public static void Declaration()
{
string[] languages; // one-dimensional
int[,] cells; // two-dimensional
}

// 2.
static public void Assignment()
public static void Assignment()
{
string[] languages = {
"C#", "COBOL", "Java",
Expand All @@ -34,7 +34,7 @@ static public void Assignment()
}

// 3.
static public void ForwardAndReverseAccessingAnArray()
public static void ForwardAndReverseAccessingAnArray()
{
string[] languages = new []{
"C#", "COBOL", "Java",
Expand All @@ -51,7 +51,7 @@ static public void ForwardAndReverseAccessingAnArray()
}

// 4.
static public void Ranges()
public static void Ranges()
{
string[] languages = new []{
"C#", "COBOL", "Java",
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.a.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 1.
static public void SquareBracketsOnVariableRatherThanType()
public static void SquareBracketsOnVariableRatherThanType()
{
#if COMPILEERROR
int numbers[];
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.b.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 2.
static public void NewKeywordWithDataTypeRequired()
public static void NewKeywordWithDataTypeRequired()
{
#if COMPILEERROR
int[] numbers;
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.c.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 3.
static public void ArraySizeCannotBeSpecifiedInDataType()
public static void ArraySizeCannotBeSpecifiedInDataType()
{
#if COMPILEERROR
int[3] numbers =
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.d.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 4.
static public void ArraySizeOrInitializerIsRequired()
public static void ArraySizeOrInitializerIsRequired()
{
#if COMPILEERROR
int[] numbers =
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.e.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 5.
static public void ArraySizeWithEmptyInitializer()
public static void ArraySizeWithEmptyInitializer()
{
#if COMPILEERROR
int[] numbers =
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.f.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 6.
static public void IndexingOffTheEndOfArray()
public static void IndexingOffTheEndOfArray()
{
int[] numbers =
new int[3];
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.g.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 7.
static public void Hat0IsOnePastTheEndOfTheArray1()
public static void Hat0IsOnePastTheEndOfTheArray1()
{
int[] numbers =
new int[3];
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.h.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 8.
static public void LastItemIsLengthMinus1()
public static void LastItemIsLengthMinus1()
{
int[] numbers =
new int[3];
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.i.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 9.
static public void MultiDimensionalArrayWithInconsistentSize()
public static void MultiDimensionalArrayWithInconsistentSize()
{
#if COMPILEERROR
int[,] numbers =
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter03/Table03.03.j.CommonArrayCodingErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03;
public partial class CommonArrayCodingErrors
{
// 10.
static public void MembersInJaggedArraysMustBeInstantiated()
public static void MembersInJaggedArraysMustBeInstantiated()
{
#if COMPILEERROR
int[][] numbers =
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter04/TicTacToe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.TicTacToe;
// play tic-tac-toe.
public class TicTacToeGame // Declares the TicTacToeGame class.
{
static public void Main() // Declares the entry point to the program.
public static void Main() // Declares the entry point to the program.
{
// Stores locations each player has moved.
int[] playerPositions = { 0, 0 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static string GetUserInput(string prompt)
lastName = GetUserInput("Enter your last name: ");
return (firstName, lastName);
}
static public void Main()
public static void Main()
{
#region HIGHLIGHT
(string First, string Last) name = GetName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_41;
public class NullabilityAttributesExamined
{
#endregion EXCLUDE
static public bool TryGetDigitAsText(
public static bool TryGetDigitAsText(
char number, [NotNullWhen(true)]out string? text) =>
(text = number switch
{
Expand All @@ -25,7 +25,7 @@ static public bool TryGetDigitAsText(
#else // EXCLUDE
[return: NotNullIfNotNull("text")] // EXCLUDE
#endif // EXCLUDE
static public string? TryGetDigitsAsText(string? text)
public static string? TryGetDigitsAsText(string? text)
{
if (text == null) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_42;

public class NullabilityAttributesExamined
{
static public string? Method() =>
public static string? Method() =>
GetObject(Array.Empty<string>(), (item) => true);
#region INCLUDE
// ...
[return: MaybeNull]
static public T GetObject<T>(
public static T GetObject<T>(
IEnumerable<T> sequence, Func<T, bool> match)
=>
// ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Save()
// ...
stream.Dispose();
}
static public Contact Copy(Contact contact)
public static Contact Copy(Contact contact)
#region HIGHLIGHT
=> new(contact.ObjectKey);
#endregion HIGHLIGHT
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.12.OverrideVersusNewModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override void DisplayName()

public class SuperSubDerivedClass : SubDerivedClass
{
public new static void DisplayName()
public static new void DisplayName()
{
Console.WriteLine("SuperSubDerivedClass");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.13.SealingMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public virtual void Method()

class B : A
{
public override sealed void Method()
public sealed override void Method()
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Contact : PdaItem
// ...
public Contact(string name) => Name = name;

static public Contact Load(PdaItem pdaItem)
public static Contact Load(PdaItem pdaItem)
{
#pragma warning disable IDE0019 // Use pattern matching
Contact? contact = pdaItem as Contact;
Expand Down
6 changes: 3 additions & 3 deletions src/Chapter08.Tests/UndersandingAccessModifiers.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public string CallConcreteInterfaceMethod3() =>
// Same as casting to ISampleInterface
((ISampleInterface)this).PublicConcreteInterfaceMethod();

virtual public string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
public virtual string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
}
public class SubClass : Information
{
public new static string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
public static new string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
}
#pragma warning restore IDE0051 // Remove unused private members

static public class Thing
public static class Thing
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Contact(string firstName, string lastName,
public string LastName { get; }
public string Address { get; }
public string Phone { get; }
static public string GetName(string firstName, string lastName)
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";

#region HIGHLIGHT
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter08/Listing08.03.ImplementingAnInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected string FirstName
}
protected string? Phone { get; set; }
protected string? Address { get; set; }
static public string GetName(string firstName, string lastName)
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";
#endregion EXCLUDE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ protected string FirstName
}
protected string? Phone { get; set; }
protected string? Address { get; set; }
static public string GetName(string firstName, string lastName)
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected string FirstName
}
protected string? Phone { get; set; }
protected string? Address { get; set; }
static public string GetName(string firstName, string lastName)
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";
#endregion EXCLUDE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ConsoleColor[] CellColors
}
}
#region HIGHLIGHT
static public ConsoleColor DefaultColumnColor { get; set; }
public static ConsoleColor DefaultColumnColor { get; set; }
#endregion HIGHLIGHT
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public static string[] Headers
}
}

static public string GetName(string firstName, string lastName)
public static string GetName(string firstName, string lastName)
=> $"{ firstName } { lastName }";
#endregion EXCLUDE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void Run()

public class Program
{
static public void Main()
public static void Main()
{
ExecuteProcessActivity activity = new("dotnet");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Employee(string firstName, string lastName)
}
public class Program
{
static public void Main()
public static void Main()
{
Person inigo = new("Inigo", "Montoya");
Console.WriteLine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class DisposeTests

private const string ProjectName = "ProcessExitTestProgram.testing";

private readonly static object RunPowerShellScriptSyncLock = new();
private static readonly object RunPowerShellScriptSyncLock = new();
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Chapter10/Listing10.14.WeakReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public T Target
#region INCLUDE
public static class ByteArrayDataSource
{
static private byte[] LoadData()
private static byte[] LoadData()
{
// Imagine a much lager number
byte[] data = new byte[1000];
Expand All @@ -45,9 +45,9 @@ static private byte[] LoadData()
return data;
}

static private WeakReference<byte[]>? Data { get; set; }
private static WeakReference<byte[]>? Data { get; set; }

static public byte[] GetData()
public static byte[] GetData()
{
byte[]? target;
if (Data is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static Task WriteWebRequestSizeAsync(
return task;
}

static public string FormatBytes(long bytes)
public static string FormatBytes(long bytes)
{
string[] magnitudes =
new string[] { "GB", "MB", "KB", "Bytes" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ private static void FillCell(Cell cell, ConsoleColor color)
#endregion INCLUDE
public struct Cell
{
readonly public int X;
readonly public int Y;
public readonly int X;
public readonly int Y;

public Cell(int x, int y)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_31;
public class Publisher
{
#region INCLUDE
static public object? InvokeAll<TDelegate>(
public static object? InvokeAll<TDelegate>(
object?[]? args, params TDelegate[] delegates)
// Constraint of type Action/Func not allowed
where TDelegate : System.MulticastDelegate
Expand All @@ -21,7 +21,7 @@ public class Publisher
}
#endregion INCLUDE

static public void InvokeAll(params Action?[] actions)
public static void InvokeAll(params Action?[] actions)
{
Action? result = (Action?)Delegate.Combine(actions);
result?.Invoke();
Expand Down
2 changes: 1 addition & 1 deletion src/Chapter12/Listing12.49.StackIntDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_49;

public class Program
{
static private void Main()
private static void Main()
{
#region INCLUDE
Stack<int> stack;
Expand Down
Loading

0 comments on commit 1e5eabc

Please sign in to comment.