From 6f40f002bd5e85bdf6513996e47914193a893583 Mon Sep 17 00:00:00 2001 From: Christopher Schuchardt Date: Tue, 7 May 2024 04:25:11 -0400 Subject: [PATCH] Changed to `Use collection expression for array` (#3217) Co-authored-by: Shargon --- .editorconfig | 7 +++++-- src/Neo/Cryptography/BloomFilter.cs | 12 +++++++---- src/Neo/Network/P2P/Payloads/Signer.cs | 8 +++++--- .../UT_OrderedDictionary.cs | 10 ++++++---- .../Network/P2P/Payloads/UT_NotValidBefore.cs | 6 ++++-- .../SmartContract/UT_ContractParameter.cs | 6 ++++-- tests/Neo.UnitTests/VM/UT_Helper.cs | 20 +++++++++++-------- 7 files changed, 44 insertions(+), 25 deletions(-) diff --git a/.editorconfig b/.editorconfig index 9a574425e9..f24ccd45a8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -72,8 +72,8 @@ dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion dotnet_style_predefined_type_for_member_access = true:suggestion # Suggest more modern language features when available -dotnet_style_object_initializer = true:suggestion -dotnet_style_collection_initializer = true:suggestion +dotnet_style_object_initializer = true:warning +dotnet_style_collection_initializer = true:warning dotnet_style_coalesce_expression = true:suggestion dotnet_style_null_propagation = true:suggestion dotnet_style_explicit_tuple_names = true:suggestion @@ -256,6 +256,9 @@ dotnet_diagnostic.IDE0060.severity = none [src/{Analyzers,CodeStyle,Features,Workspaces,EditorFeatures,VisualStudio}/**/*.{cs,vb}] +# Use collection expression for array +dotnet_diagnostic.IDE0300.severity = warning + # Avoid "this." and "Me." if not necessary dotnet_diagnostic.IDE0003.severity = warning dotnet_diagnostic.IDE0009.severity = warning diff --git a/src/Neo/Cryptography/BloomFilter.cs b/src/Neo/Cryptography/BloomFilter.cs index ffa626a546..73141d9e11 100644 --- a/src/Neo/Cryptography/BloomFilter.cs +++ b/src/Neo/Cryptography/BloomFilter.cs @@ -48,8 +48,10 @@ public BloomFilter(int m, int k, uint nTweak) { if (k < 0 || m < 0) throw new ArgumentOutOfRangeException(); seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray(); - bits = new BitArray(m); - bits.Length = m; + bits = new BitArray(m) + { + Length = m + }; Tweak = nTweak; } @@ -64,8 +66,10 @@ public BloomFilter(int m, int k, uint nTweak, ReadOnlyMemory elements) { if (k < 0 || m < 0) throw new ArgumentOutOfRangeException(); seeds = Enumerable.Range(0, k).Select(p => (uint)p * 0xFBA4C795 + nTweak).ToArray(); - bits = new BitArray(elements.ToArray()); - bits.Length = m; + bits = new BitArray(elements.ToArray()) + { + Length = m + }; Tweak = nTweak; } diff --git a/src/Neo/Network/P2P/Payloads/Signer.cs b/src/Neo/Network/P2P/Payloads/Signer.cs index 2f96047323..9a68640880 100644 --- a/src/Neo/Network/P2P/Payloads/Signer.cs +++ b/src/Neo/Network/P2P/Payloads/Signer.cs @@ -153,9 +153,11 @@ public void Serialize(BinaryWriter writer) /// The converted signer. public static Signer FromJson(JObject json) { - Signer signer = new(); - signer.Account = UInt160.Parse(json["account"].GetString()); - signer.Scopes = Enum.Parse(json["scopes"].GetString()); + Signer signer = new() + { + Account = UInt160.Parse(json["account"].GetString()), + Scopes = Enum.Parse(json["scopes"].GetString()) + }; if (signer.Scopes.HasFlag(WitnessScope.CustomContracts)) signer.AllowedContracts = ((JArray)json["allowedcontracts"]).Select(p => UInt160.Parse(p.GetString())).ToArray(); if (signer.Scopes.HasFlag(WitnessScope.CustomGroups)) diff --git a/tests/Neo.Json.UnitTests/UT_OrderedDictionary.cs b/tests/Neo.Json.UnitTests/UT_OrderedDictionary.cs index 83a5c2c2f7..0f73afae95 100644 --- a/tests/Neo.Json.UnitTests/UT_OrderedDictionary.cs +++ b/tests/Neo.Json.UnitTests/UT_OrderedDictionary.cs @@ -21,10 +21,12 @@ public class UT_OrderedDictionary [TestInitialize] public void SetUp() { - od = new OrderedDictionary(); - od.Add("a", 1); - od.Add("b", 2); - od.Add("c", 3); + od = new OrderedDictionary + { + { "a", 1 }, + { "b", 2 }, + { "c", 3 } + }; } [TestMethod] diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs index 0b51550f83..8e7ad9087b 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs @@ -31,8 +31,10 @@ public void Size_Get() [TestMethod] public void ToJson() { - var test = new NotValidBefore(); - test.Height = 42; + var test = new NotValidBefore + { + Height = 42 + }; var json = test.ToJson().ToString(); Assert.AreEqual(@"{""type"":""NotValidBefore"",""height"":42}", json); } diff --git a/tests/Neo.UnitTests/SmartContract/UT_ContractParameter.cs b/tests/Neo.UnitTests/SmartContract/UT_ContractParameter.cs index 3c567ca5bc..6618a918e7 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_ContractParameter.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_ContractParameter.cs @@ -187,8 +187,10 @@ public void TestToString() ContractParameter contractParameter1 = new(); Assert.AreEqual("(null)", contractParameter1.ToString()); - ContractParameter contractParameter2 = new(ContractParameterType.ByteArray); - contractParameter2.Value = new byte[1]; + ContractParameter contractParameter2 = new(ContractParameterType.ByteArray) + { + Value = new byte[1] + }; Assert.AreEqual("00", contractParameter2.ToString()); ContractParameter contractParameter3 = new(ContractParameterType.Array); diff --git a/tests/Neo.UnitTests/VM/UT_Helper.cs b/tests/Neo.UnitTests/VM/UT_Helper.cs index fbe12fc787..c5bf61d152 100644 --- a/tests/Neo.UnitTests/VM/UT_Helper.cs +++ b/tests/Neo.UnitTests/VM/UT_Helper.cs @@ -40,11 +40,13 @@ public void TestEmit() [TestMethod] public void TestToJson() { - var item = new VM.Types.Array(); - item.Add(5); - item.Add("hello world"); - item.Add(new byte[] { 1, 2, 3 }); - item.Add(true); + var item = new VM.Types.Array + { + 5, + "hello world", + new byte[] { 1, 2, 3 }, + true + }; Assert.AreEqual("{\"type\":\"Integer\",\"value\":\"5\"}", item[0].ToJson().ToString()); Assert.AreEqual("{\"type\":\"ByteString\",\"value\":\"aGVsbG8gd29ybGQ=\"}", item[1].ToJson().ToString()); @@ -274,9 +276,11 @@ private void TestEmitPush2Array() { ScriptBuilder sb = new ScriptBuilder(); ContractParameter parameter = new ContractParameter(ContractParameterType.Array); - IList values = new List(); - values.Add(new ContractParameter(ContractParameterType.Integer)); - values.Add(new ContractParameter(ContractParameterType.Integer)); + IList values = new List + { + new ContractParameter(ContractParameterType.Integer), + new ContractParameter(ContractParameterType.Integer) + }; parameter.Value = values; sb.EmitPush(parameter); byte[] tempArray = new byte[4];