Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SA1141 #3610

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch with these missing test classes. I assume it was a mistake to not add them earlier. Would be good to have automated checks for things like this.

// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp9.ReadabilityRules;

public class SA1141CSharp10UnitTests : SA1141CSharp9UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp9.ReadabilityRules;

public class SA1142CSharp10UnitTests : SA1142CSharp9UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp11.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp10.ReadabilityRules;

public class SA1141CSharp11UnitTests : SA1141CSharp10UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp11.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp10.ReadabilityRules;

public class SA1142CSharp11UnitTests : SA1142CSharp10UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using StyleCop.Analyzers.Test.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1141UseTupleSyntax,
Expand All @@ -19,7 +18,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
/// </summary>
/// <seealso cref="SA1141UseTupleSyntax"/>
/// <seealso cref="SA1141CodeFixProvider"/>
public class SA1141CSharp7UnitTests
public class SA1141CSharp7UnitTests : SA1141UnitTests
{
/// <summary>
/// Verifies that member declarations containing ValueTuple will result in the proper diagnostics and fixes.
Expand All @@ -32,28 +31,28 @@ public async Task ValidateMemberDeclarationsWithValueTuplesAsync()

public class TestClass
{
public ValueTuple<int, int> TestMethod(ValueTuple<double, double> value)
public [|ValueTuple<int, int>|] TestMethod([|ValueTuple<double, double>|] value)
{
throw new NotImplementedException();
}

public System.ValueTuple<(int, int), int> TestMethod2(int p1, ValueTuple<System.ValueTuple<long, long>, long> p2, (ValueTuple<string, string>, string) p3)
public [|System.ValueTuple<(int, int), int>|] TestMethod2(int p1, [|ValueTuple<System.ValueTuple<long, long>, long>|] p2, ([|ValueTuple<string, string>|], string) p3)
{
throw new NotImplementedException();
}

public System.ValueTuple<int, int> TestProperty1 { get; set; }
public [|System.ValueTuple<int, int>|] TestProperty1 { get; set; }

public System.Collections.Generic.List<ValueTuple<int, int>> TestProperty2 { get; set; }
public System.Collections.Generic.List<[|ValueTuple<int, int>|]> TestProperty2 { get; set; }

public System.ValueTuple<int, long> this[int i] { get { return (1, 1l); } set { } }
public [|System.ValueTuple<int, long>|] this[int i] { get { return (1, 1l); } set { } }

public static explicit operator TestClass(System.ValueTuple<int, int> p1)
public static explicit operator TestClass([|System.ValueTuple<int, int>|] p1)
{
throw new NotImplementedException();
}

public static implicit operator System.ValueTuple<int, int>(TestClass p1)
public static implicit operator [|System.ValueTuple<int, int>|](TestClass p1)
{
throw new NotImplementedException();
}
Expand Down Expand Up @@ -92,21 +91,7 @@ public static implicit operator (int, int)(TestClass p1)
}
";

DiagnosticResult[] expectedDiagnostics =
{
Diagnostic().WithLocation(5, 12),
Diagnostic().WithLocation(5, 44),
Diagnostic().WithLocation(10, 12),
Diagnostic().WithLocation(10, 67),
Diagnostic().WithLocation(10, 120),
Diagnostic().WithLocation(15, 12),
Diagnostic().WithLocation(17, 44),
Diagnostic().WithLocation(19, 12),
Diagnostic().WithLocation(21, 47),
Diagnostic().WithLocation(26, 37),
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -122,18 +107,18 @@ public class TestClass
{
public void TestMethod()
{
var test1 = new ValueTuple<int, int>(1, 2);
var test2 = new System.ValueTuple<int, int>(1, 2);
var test3 = new ValueTuple<ValueTuple<int, int>, int>(new ValueTuple<int, int>(3, 4), 2);
var test4 = new System.ValueTuple<int, System.ValueTuple<int, int>>(1, new System.ValueTuple<int, int>(2, 3));
var test5 = (new ValueTuple<int, int>(3, 4), 2);
var test6 = new System.ValueTuple<int, System.ValueTuple<int, int>>(1, (2, 3));
var test7 = ValueTuple.Create(1, 2);
var test8 = ValueTuple.Create<int, double>(1, 2);
var test9 = System.ValueTuple.Create(1, new ValueTuple<int, double>(2, 3));
var test10 = ValueTuple.Create(ValueTuple.Create(1, 2, 3), 4);
var test11 = new ValueTuple<int, ValueTuple<int, int>>(1, ValueTuple.Create(2, 3));
var test12 = new System.ValueTuple<byte, int>(1, 2);
var test1 = [|new ValueTuple<int, int>(1, 2)|];
var test2 = [|new System.ValueTuple<int, int>(1, 2)|];
var test3 = [|new ValueTuple<ValueTuple<int, int>, int>([|new ValueTuple<int, int>(3, 4)|], 2)|];
var test4 = [|new System.ValueTuple<int, System.ValueTuple<int, int>>(1, [|new System.ValueTuple<int, int>(2, 3)|])|];
var test5 = ([|new ValueTuple<int, int>(3, 4)|], 2);
var test6 = [|new System.ValueTuple<int, System.ValueTuple<int, int>>(1, (2, 3))|];
var test7 = [|ValueTuple.Create|](1, 2);
var test8 = [|ValueTuple.Create<int, double>|](1, 2);
var test9 = [|System.ValueTuple.Create|](1, [|new ValueTuple<int, double>(2, 3)|]);
var test10 = [|ValueTuple.Create|]([|ValueTuple.Create|](1, 2, 3), 4);
var test11 = [|new ValueTuple<int, ValueTuple<int, int>>(1, [|ValueTuple.Create|](2, 3))|];
var test12 = [|new System.ValueTuple<byte, int>(1, 2)|];
}
}
";
Expand All @@ -160,28 +145,7 @@ public void TestMethod()
}
";

DiagnosticResult[] expectedDiagnostics =
{
Diagnostic().WithLocation(7, 21),
Diagnostic().WithLocation(8, 21),
Diagnostic().WithLocation(9, 21),
Diagnostic().WithLocation(9, 63),
Diagnostic().WithLocation(10, 21),
Diagnostic().WithLocation(10, 80),
Diagnostic().WithLocation(11, 22),
Diagnostic().WithLocation(12, 21),
Diagnostic().WithLocation(13, 21),
Diagnostic().WithLocation(14, 21),
Diagnostic().WithLocation(15, 21),
Diagnostic().WithLocation(15, 49),
Diagnostic().WithLocation(16, 22),
Diagnostic().WithLocation(16, 40),
Diagnostic().WithLocation(17, 22),
Diagnostic().WithLocation(17, 67),
Diagnostic().WithLocation(18, 22),
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -329,8 +293,8 @@ public class TestClass
{
public void TestMethod(object input)
{
var test1 = (ValueTuple<int, int>)input;
var test2 = (System.ValueTuple<System.ValueTuple<int, long>, byte>)input;
var test1 = ([|ValueTuple<int, int>|])input;
var test2 = ([|System.ValueTuple<System.ValueTuple<int, long>, byte>|])input;
}
}
";
Expand All @@ -347,13 +311,7 @@ public void TestMethod(object input)
}
";

DiagnosticResult[] expectedDiagnostics =
{
Diagnostic().WithLocation(7, 22),
Diagnostic().WithLocation(8, 22),
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -369,8 +327,8 @@ public class TestClass
{
public void TestMethod()
{
var test1 = default(ValueTuple<int, int>);
var test2 = default(System.ValueTuple<System.ValueTuple<int, long>, byte>);
var test1 = default([|ValueTuple<int, int>|]);
var test2 = default([|System.ValueTuple<System.ValueTuple<int, long>, byte>|]);
}
}
";
Expand All @@ -387,13 +345,7 @@ public void TestMethod()
}
";

DiagnosticResult[] expectedDiagnostics =
{
Diagnostic().WithLocation(7, 29),
Diagnostic().WithLocation(8, 29),
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -407,7 +359,7 @@ public async Task ValidateValueTupleUsageInDelegateAsync()

public class TestClass
{
public delegate System.ValueTuple<int, bool> TestDelegate(ValueTuple<int, ValueTuple<int, long>> arg1, (long, double) arg2, (long, System.ValueTuple<bool, bool>) arg3);
public delegate [|System.ValueTuple<int, bool>|] TestDelegate([|ValueTuple<int, ValueTuple<int, long>>|] arg1, (long, double) arg2, (long, [|System.ValueTuple<bool, bool>|]) arg3);
}
";

Expand All @@ -419,14 +371,7 @@ public class TestClass
}
";

DiagnosticResult[] expectedDiagnostics =
{
Diagnostic().WithLocation(5, 21),
Diagnostic().WithLocation(5, 63),
Diagnostic().WithLocation(5, 136),
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.ReadabilityRules;
using StyleCop.Analyzers.Test.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.ReadabilityRules.SA1142ReferToTupleElementsByName,
Expand All @@ -19,7 +18,7 @@ namespace StyleCop.Analyzers.Test.CSharp7.ReadabilityRules
/// </summary>
/// <seealso cref="SA1142ReferToTupleElementsByName"/>
/// <seealso cref="SA1142CodeFixProvider"/>
public class SA1142CSharp7UnitTests
public class SA1142CSharp7UnitTests : SA1142UnitTests
{
/// <summary>
/// Validate that tuple fields that are referenced by their name will not produce any diagnostics.
Expand Down Expand Up @@ -78,12 +77,7 @@ public int TestMethod2((int nameA, (int subNameA, int subNameB) nameB) p1)
}
";

DiagnosticResult[] expectedDiagnostics =
{
// diagnostics are specified inline
};

await VerifyCSharpFixAsync(testCode, expectedDiagnostics, fixedCode, CancellationToken.None).ConfigureAwait(false);
await VerifyCSharpFixAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp7.ReadabilityRules;

public class SA1141CSharp8UnitTests : SA1141CSharp7UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp8.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp7.ReadabilityRules;

public class SA1142CSharp8UnitTests : SA1142CSharp7UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;

public class SA1141CSharp9UnitTests : SA1141CSharp8UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Test.CSharp9.ReadabilityRules
{
using StyleCop.Analyzers.Test.CSharp8.ReadabilityRules;

public class SA1142CSharp9UnitTests : SA1142CSharp8UnitTests
{
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.Test.ReadabilityRules
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.CSharp;
using StyleCop.Analyzers.ReadabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
Expand Down Expand Up @@ -42,9 +40,10 @@ public ValueTuple<int, int> TestMethod(ValueTuple<double, double> value)
}
";

await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
await new CSharpTest(LanguageVersion.CSharp6)
{
TestCode = testCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

//// TODO: Make sure that all paths are covered!
}
}
Loading