Skip to content

Commit bd49514

Browse files
committed
Switch to implicit object creation
Also enforce this via editorconfig
1 parent 4db8883 commit bd49514

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = false
5+
6+
[*.cs]
7+
8+
csharp_style_var_when_type_is_apparent = false:refactor
9+
# IDE0090: Use 'new(...)'
10+
csharp_style_implicit_object_creation_when_type_is_apparent = true:warning
11+

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/InvalidSerializationTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class InvalidSerializationTest
1616
[Fact]
1717
public void RequiresSqlUserDefinedTypeAttribute()
1818
{
19-
using MemoryStream stream = new MemoryStream();
19+
using MemoryStream stream = new();
2020

21-
InvalidUdtException exception = Assert.Throws<InvalidUdtException>(
21+
var exception = Assert.Throws<InvalidUdtException>(
2222
() => SerializationHelperSql9.Serialize(stream, new ClassMissingSqlUserDefinedTypeAttribute()));
2323

2424
Assert.Equal($"'{typeof(ClassMissingSqlUserDefinedTypeAttribute).FullName}' is an invalid user defined type, reason: no UDT attribute.", exception.Message);
@@ -27,9 +27,9 @@ public void RequiresSqlUserDefinedTypeAttribute()
2727
[Fact]
2828
public void CannotSerializeUnknownFormattedType()
2929
{
30-
using MemoryStream stream = new MemoryStream();
30+
using MemoryStream stream = new();
3131

32-
ArgumentOutOfRangeException exception = Assert.Throws<ArgumentOutOfRangeException>("Format",
32+
var exception = Assert.Throws<ArgumentOutOfRangeException>("Format",
3333
() => SerializationHelperSql9.Serialize(stream, new UnknownFormattedClass()));
3434

3535
#if NET

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/NativeSerializationTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ public void SerializeNullPrimitiveType(object primitive, byte[] expectedValue)
188188
[Fact]
189189
public void CanSerializeTopLevelClass()
190190
{
191-
NestedBoolWrapperClass validWrapper = new NestedBoolWrapperClass()
191+
NestedBoolWrapperClass validWrapper = new()
192192
{
193193
Field1 = true,
194194
Field2 = new BoolWrapperStruct() { Field1 = true }
195195
};
196-
using MemoryStream stream = new MemoryStream();
196+
using MemoryStream stream = new();
197197

198198
SerializationHelperSql9.Serialize(stream, validWrapper);
199199
}
@@ -207,14 +207,14 @@ public void CanSerializeTopLevelClass()
207207
[Fact]
208208
public void CannotSerializeNestedClass()
209209
{
210-
InvalidNestedBoolWrapperClass invalidWrapper = new InvalidNestedBoolWrapperClass()
210+
InvalidNestedBoolWrapperClass invalidWrapper = new()
211211
{
212212
Field1 = true,
213213
Field2 = new BoolWrapperClass() { Field1 = true }
214214
};
215-
using MemoryStream stream = new MemoryStream();
215+
using MemoryStream stream = new();
216216

217-
Exception ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
217+
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
218218
string expectedException = StringsHelper.GetString(Strings.SQL_CannotCreateNormalizer, invalidWrapper.Field2.GetType().FullName);
219219

220220
Assert.Equal(expectedException, ex.Message);
@@ -227,14 +227,14 @@ public void CannotSerializeNestedClass()
227227
[Fact]
228228
public void CannotSerializeNonPrimitiveType()
229229
{
230-
InvalidIntPtrAndByteWrapperStruct invalidWrapper = new InvalidIntPtrAndByteWrapperStruct()
230+
InvalidIntPtrAndByteWrapperStruct invalidWrapper = new()
231231
{
232232
Field1 = 1,
233233
Field2 = IntPtr.Zero
234234
};
235-
using MemoryStream stream = new MemoryStream();
235+
using MemoryStream stream = new();
236236

237-
Exception ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
237+
var ex = Assert.Throws<Exception>(() => SerializationHelperSql9.Serialize(stream, invalidWrapper));
238238
string expectedException = StringsHelper.GetString(Strings.SQL_CannotCreateNormalizer, invalidWrapper.Field2.GetType().FullName);
239239

240240
Assert.Equal(expectedException, ex.Message);
@@ -247,7 +247,7 @@ public void CannotSerializeNonPrimitiveType()
247247
/// <param name="expectedValue">Expected serialization output.</param>
248248
private static void RoundtripType(object inputValue, byte[] expectedValue)
249249
{
250-
using MemoryStream stream = new MemoryStream();
250+
using MemoryStream stream = new();
251251
object readPrimitive;
252252
int typeSize = SerializationHelperSql9.SizeInBytes(inputValue.GetType());
253253
int objectSize = SerializationHelperSql9.SizeInBytes(inputValue);

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/UdtSerialization/UserDefinedSerializationTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void CanSerializeClass()
2727
[Fact]
2828
public void RequiresPublicParameterlessConstructor()
2929
{
30-
using MemoryStream stream = new MemoryStream();
30+
using MemoryStream stream = new();
3131

3232
SerializationHelperSql9.Serialize(stream, new UserDefinedMissingPublicConstructor(true));
3333
stream.Seek(0, SeekOrigin.Begin);
@@ -39,7 +39,7 @@ public void RequiresPublicParameterlessConstructor()
3939
[Fact]
4040
public void RequiresIBinarySerializeImplementation()
4141
{
42-
using MemoryStream stream = new MemoryStream();
42+
using MemoryStream stream = new();
4343

4444
Assert.Throws<InvalidCastException>(
4545
() => SerializationHelperSql9.Serialize(stream, new UserDefinedDoesNotImplementIBinarySerialize()));
@@ -48,7 +48,7 @@ public void RequiresIBinarySerializeImplementation()
4848
private static void RoundtripType<T>(T userObject)
4949
where T : IFormattingProgress
5050
{
51-
using MemoryStream stream = new MemoryStream();
51+
using MemoryStream stream = new();
5252
byte[] serializedValue;
5353
T readInstance;
5454
int typeSize = SerializationHelperSql9.SizeInBytes(userObject.GetType());

0 commit comments

Comments
 (0)