From 6077bfe607e2fdf69acc9f0698a7c3bc2f632c3e Mon Sep 17 00:00:00 2001 From: Samu Lang Date: Fri, 20 Dec 2024 11:54:14 +0100 Subject: [PATCH] Various tidy --- Directory.Packages.props | 2 +- GraphEngine.Tests/BinaryTests.cs | 80 +++++++++++++++---------------- GraphEngine.Tests/SampleClass.cs | 16 ++++--- GraphEngine/BaseBind.cs | 7 +-- GraphEngine/Binder.cs | 7 +-- GraphEngine/Graph.cs | 2 +- GraphEngine/SerialisingVisitor.cs | 14 +++--- 7 files changed, 63 insertions(+), 65 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index fba2ee0..a75ec41 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -13,4 +13,4 @@ - \ No newline at end of file + diff --git a/GraphEngine.Tests/BinaryTests.cs b/GraphEngine.Tests/BinaryTests.cs index 9049525..f7a05a6 100644 --- a/GraphEngine.Tests/BinaryTests.cs +++ b/GraphEngine.Tests/BinaryTests.cs @@ -11,51 +11,51 @@ public static IEnumerable Data { get { - yield return new object[] { Linq.ExpressionType.Add }; - yield return new object[] { Linq.ExpressionType.AddAssign }; - yield return new object[] { Linq.ExpressionType.AddAssignChecked }; - yield return new object[] { Linq.ExpressionType.AddChecked }; - yield return new object[] { Linq.ExpressionType.And }; - yield return new object[] { Linq.ExpressionType.AndAlso, typeof(bool) }; - yield return new object[] { Linq.ExpressionType.AndAssign }; - yield return new object[] { Linq.ExpressionType.ArrayIndex, typeof(int[]), typeof(int) }; - yield return new object[] { Linq.ExpressionType.Assign }; - yield return new object[] { Linq.ExpressionType.Coalesce, typeof(object) }; - yield return new object[] { Linq.ExpressionType.Divide }; - yield return new object[] { Linq.ExpressionType.DivideAssign }; - yield return new object[] { Linq.ExpressionType.Equal }; - yield return new object[] { Linq.ExpressionType.ExclusiveOr }; - yield return new object[] { Linq.ExpressionType.ExclusiveOrAssign }; - yield return new object[] { Linq.ExpressionType.GreaterThan }; - yield return new object[] { Linq.ExpressionType.GreaterThanOrEqual }; - yield return new object[] { Linq.ExpressionType.LeftShift }; - yield return new object[] { Linq.ExpressionType.LeftShiftAssign }; - yield return new object[] { Linq.ExpressionType.LessThan }; - yield return new object[] { Linq.ExpressionType.LessThanOrEqual }; - yield return new object[] { Linq.ExpressionType.Modulo }; - yield return new object[] { Linq.ExpressionType.ModuloAssign }; - yield return new object[] { Linq.ExpressionType.Multiply }; - yield return new object[] { Linq.ExpressionType.MultiplyAssign }; - yield return new object[] { Linq.ExpressionType.MultiplyAssignChecked }; - yield return new object[] { Linq.ExpressionType.MultiplyChecked }; - yield return new object[] { Linq.ExpressionType.NotEqual }; - yield return new object[] { Linq.ExpressionType.Or }; - yield return new object[] { Linq.ExpressionType.OrAssign }; - yield return new object[] { Linq.ExpressionType.OrElse, typeof(bool) }; - yield return new object[] { Linq.ExpressionType.Power, typeof(double) }; - yield return new object[] { Linq.ExpressionType.PowerAssign, typeof(double) }; - yield return new object[] { Linq.ExpressionType.RightShift }; - yield return new object[] { Linq.ExpressionType.RightShiftAssign }; - yield return new object[] { Linq.ExpressionType.Subtract }; - yield return new object[] { Linq.ExpressionType.SubtractAssign }; - yield return new object[] { Linq.ExpressionType.SubtractAssignChecked }; - yield return new object[] { Linq.ExpressionType.SubtractChecked }; + yield return [Linq.ExpressionType.Add]; + yield return [Linq.ExpressionType.AddAssign]; + yield return [Linq.ExpressionType.AddAssignChecked]; + yield return [Linq.ExpressionType.AddChecked]; + yield return [Linq.ExpressionType.And]; + yield return [Linq.ExpressionType.AndAlso, typeof(bool)]; + yield return [Linq.ExpressionType.AndAssign]; + yield return [Linq.ExpressionType.ArrayIndex, typeof(int[]), typeof(int)]; + yield return [Linq.ExpressionType.Assign]; + yield return [Linq.ExpressionType.Coalesce, typeof(object)]; + yield return [Linq.ExpressionType.Divide]; + yield return [Linq.ExpressionType.DivideAssign]; + yield return [Linq.ExpressionType.Equal]; + yield return [Linq.ExpressionType.ExclusiveOr]; + yield return [Linq.ExpressionType.ExclusiveOrAssign]; + yield return [Linq.ExpressionType.GreaterThan]; + yield return [Linq.ExpressionType.GreaterThanOrEqual]; + yield return [Linq.ExpressionType.LeftShift]; + yield return [Linq.ExpressionType.LeftShiftAssign]; + yield return [Linq.ExpressionType.LessThan]; + yield return [Linq.ExpressionType.LessThanOrEqual]; + yield return [Linq.ExpressionType.Modulo]; + yield return [Linq.ExpressionType.ModuloAssign]; + yield return [Linq.ExpressionType.Multiply]; + yield return [Linq.ExpressionType.MultiplyAssign]; + yield return [Linq.ExpressionType.MultiplyAssignChecked]; + yield return [Linq.ExpressionType.MultiplyChecked]; + yield return [Linq.ExpressionType.NotEqual]; + yield return [Linq.ExpressionType.Or]; + yield return [Linq.ExpressionType.OrAssign]; + yield return [Linq.ExpressionType.OrElse, typeof(bool)]; + yield return [Linq.ExpressionType.Power, typeof(double)]; + yield return [Linq.ExpressionType.PowerAssign, typeof(double)]; + yield return [Linq.ExpressionType.RightShift]; + yield return [Linq.ExpressionType.RightShiftAssign]; + yield return [Linq.ExpressionType.Subtract]; + yield return [Linq.ExpressionType.SubtractAssign]; + yield return [Linq.ExpressionType.SubtractAssignChecked]; + yield return [Linq.ExpressionType.SubtractChecked]; } } [TestMethod] [DynamicData(nameof(Data))] - public void Regular(Linq.ExpressionType binaryType, System.Type leftType = null, System.Type rightType = null) + public void Regular(Linq.ExpressionType binaryType, System.Type? leftType = null, System.Type? rightType = null) { leftType ??= typeof(int); rightType ??= leftType; diff --git a/GraphEngine.Tests/SampleClass.cs b/GraphEngine.Tests/SampleClass.cs index 68799fb..29968e3 100644 --- a/GraphEngine.Tests/SampleClass.cs +++ b/GraphEngine.Tests/SampleClass.cs @@ -1,10 +1,11 @@ // MIT License, Copyright 2020 Samu Lang -#pragma warning disable CA1051 // Do not declare visible instance fields -#pragma warning disable CA1801 // Review unused parameters +#pragma warning disable IDE0079 // Remove unnecessary suppression +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. #pragma warning disable CA2211 // Non-constant fields should not be visible #pragma warning disable SA1401 // Fields should be private -#pragma warning disable SA1402 // File may only contain a single type +#pragma warning disable CA1822 // Mark members as static +#pragma warning disable IDE0060 // Remove unused parameter namespace GraphEngine.Tests; @@ -25,7 +26,7 @@ public class SampleClass [IndexerName("Indexer")] public string this[int index] { - get => default; + get => default!; set { } } @@ -70,8 +71,9 @@ public class SampleDerivedClass : SampleClass { } -#pragma warning restore CA1051 // Do not declare visible instance fields -#pragma warning restore CA1801 // Review unused parameters +#pragma warning restore IDE0060 // Remove unused parameter +#pragma warning restore CA1822 // Mark members as static #pragma warning restore CA2211 // Non-constant fields should not be visible #pragma warning restore SA1401 // Fields should be private -#pragma warning restore SA1402 // File may only contain a single type +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. +#pragma warning restore IDE0079 // Remove unnecessary suppression diff --git a/GraphEngine/BaseBind.cs b/GraphEngine/BaseBind.cs index a323288..5a14d6d 100644 --- a/GraphEngine/BaseBind.cs +++ b/GraphEngine/BaseBind.cs @@ -24,10 +24,7 @@ public Member Member internal static BaseBind Parse(NodeWithGraph node) { - if (node is null) - { - throw new ArgumentNullException(nameof(node)); - } + ArgumentNullException.ThrowIfNull(node); return Vocabulary.RdfType.ObjectOf(node) switch { @@ -36,7 +33,7 @@ internal static BaseBind Parse(NodeWithGraph node) INode t when t.Equals(Vocabulary.MemberBind) => new MemberBind(node), null => throw new Exception($"type not found on node {node}"), - INode t => throw new Exception($"unknown bind type {t} on node {node}"), + var t => throw new Exception($"unknown bind type {t} on node {node}"), }; } } diff --git a/GraphEngine/Binder.cs b/GraphEngine/Binder.cs index e0b9ea6..9f6ca85 100644 --- a/GraphEngine/Binder.cs +++ b/GraphEngine/Binder.cs @@ -26,10 +26,7 @@ public ExpressionType? ExpressionType internal static Binder Parse(NodeWithGraph node) { - if (node is null) - { - throw new ArgumentNullException(nameof(node)); - } + ArgumentNullException.ThrowIfNull(node); return Vocabulary.RdfType.ObjectOf(node) switch { @@ -37,7 +34,7 @@ internal static Binder Parse(NodeWithGraph node) INode t when t.Equals(Vocabulary.BinaryOperation) => new BinaryOperation(node), null => throw new Exception($"type not found on node {node}"), - INode t => throw new Exception($"unknown binder type {t} on node {node}"), + var t => throw new Exception($"unknown binder type {t} on node {node}"), }; } } diff --git a/GraphEngine/Graph.cs b/GraphEngine/Graph.cs index 3557bd1..ec4bce2 100644 --- a/GraphEngine/Graph.cs +++ b/GraphEngine/Graph.cs @@ -7,7 +7,7 @@ namespace GraphEngine; public class Graph : WrapperGraph { - private static readonly IInferenceEngine Reasoner = new StaticRdfsReasoner(); + private static readonly StaticRdfsReasoner Reasoner = new(); static Graph() { diff --git a/GraphEngine/SerialisingVisitor.cs b/GraphEngine/SerialisingVisitor.cs index 06f3d95..8b192b0 100644 --- a/GraphEngine/SerialisingVisitor.cs +++ b/GraphEngine/SerialisingVisitor.cs @@ -10,7 +10,7 @@ public class SerialisingVisitor(NodeWithGraph node) : Linq.ExpressionVisitor() { private readonly Dictionary mapping = []; private readonly NodeWithGraph node = node ?? throw new ArgumentNullException(nameof(node)); - private readonly Stack path = new (); + private readonly Stack path = new(); private bool initialised; private NodeWithGraph Current => this.path.Peek(); @@ -36,7 +36,7 @@ private NodeWithGraph this[object index] } } - public override Linq.Expression Visit(Linq.Expression node) + public override Linq.Expression Visit(Linq.Expression? node) { using (this.Wrap(node)) { @@ -298,7 +298,7 @@ protected override Linq.Expression VisitIndex(Linq.IndexExpression node) return node; } - protected override Linq.LabelTarget VisitLabelTarget(Linq.LabelTarget node) + protected override Linq.LabelTarget VisitLabelTarget(Linq.LabelTarget? node) { using (this.Wrap(node)) { @@ -372,8 +372,9 @@ protected override Linq.Expression VisitMember(Linq.MemberExpression node) { var memberAccess = node.Member.MemberType switch { - MemberTypes.Field => (MemberAccess)new Field(this.Current), - MemberTypes.Property => (MemberAccess)new Property(this.Current), + MemberTypes.Field => new Field(this.Current) as MemberAccess, + MemberTypes.Property => new Property(this.Current) as MemberAccess, + var mt => throw new Exception($"unknown member type {mt}") }; memberAccess.Name = node.Member.Name; @@ -411,6 +412,7 @@ protected override Linq.MemberBinding VisitMemberBinding(Linq.MemberBinding node Linq.MemberAssignment binding => this.VisitMemberAssignment(binding), Linq.MemberMemberBinding binding => this.VisitMemberMemberBinding(binding), Linq.MemberListBinding binding => this.VisitMemberListBinding(binding), + var n => throw new Exception($"unknown member binding {n}") }; } } @@ -716,7 +718,7 @@ private Type VisitType(System.Type type) } } - private IDisposable Wrap(object node) => new Wrapper(this, this[node]); + private Wrapper Wrap(object node) => new(this, this[node]); private readonly struct Wrapper : IDisposable {