Skip to content

Commit 4d742cc

Browse files
authored
Merge pull request #3134 from scottmcginness/master
Fixes #3133. Ignore inferred tuple element name when explicit name is given
2 parents 03b3f10 + 5c1c88e commit 4d742cc

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/NamingRules/SA1316CSharp7UnitTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace StyleCop.Analyzers.Test.CSharp7.NamingRules
88
using Microsoft.CodeAnalysis.Testing;
99
using StyleCop.Analyzers.Lightup;
1010
using StyleCop.Analyzers.NamingRules;
11-
using StyleCop.Analyzers.Settings.ObjectModel;
1211
using Xunit;
1312
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
1413
StyleCop.Analyzers.NamingRules.SA1316TupleElementNamesShouldUseCorrectCasing,
@@ -170,6 +169,42 @@ public void TestMethod()
170169
await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
171170
}
172171

172+
/// <summary>
173+
/// Validates the properly explicitly named tuple elements, even when using inferred tuple element names, will not produce diagnostics.
174+
/// </summary>
175+
/// <param name="settings">The test settings to use.</param>
176+
/// <param name="tupleElementName1">The expected tuple element name for the first field.</param>
177+
/// <param name="tupleElementName2">The expected tuple element name for the second field.</param>
178+
/// <param name="tupleInferred1">The name of the first tuple element that would be inferred if not given explicitly.</param>
179+
/// <param name="tupleInferred2">The name of the second tuple element that would be inferred if not given explicitly.</param>
180+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
181+
[Theory]
182+
[InlineData(CamelCaseInferredTestSettings, "elementName1", "elementName2", "ElementValue1", "ElementValue2")]
183+
[InlineData(PascalCaseInferredTestSettings, "ElementName1", "ElementName2", "elementValue1", "elementValue2")]
184+
public async Task ValidateProperCasedExplicitNamesEvenWithInferredTupleElementNamesAsync(string settings, string tupleElementName1, string tupleElementName2, string tupleInferred1, string tupleInferred2)
185+
{
186+
var testCode = $@"
187+
public class TestClass
188+
{{
189+
public void TestMethod1()
190+
{{
191+
var {tupleInferred1} = 1;
192+
var {tupleInferred2} = ""test"";
193+
var tuple = ({tupleElementName1}: {tupleInferred1}, {tupleElementName2}: {tupleInferred2});
194+
}}
195+
196+
public void TestMethod2()
197+
{{
198+
var {tupleInferred1} = 1;
199+
var {tupleElementName2} = ""test"";
200+
var tuple = ({tupleElementName1}: {tupleInferred1}, {tupleElementName2});
201+
}}
202+
}}
203+
";
204+
205+
await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_1, testCode, settings, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
206+
}
207+
173208
/// <summary>
174209
/// Validates the improperly named tuple element names will produce the expected diagnostics.
175210
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/NamingRules/SA1316TupleElementNamesShouldUseCorrectCasing.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace StyleCop.Analyzers.NamingRules
66
using System;
77
using System.Collections.Immutable;
88
using Microsoft.CodeAnalysis;
9-
using Microsoft.CodeAnalysis.CSharp;
10-
using Microsoft.CodeAnalysis.CSharp.Syntax;
119
using Microsoft.CodeAnalysis.Diagnostics;
1210
using StyleCop.Analyzers.Helpers;
1311
using StyleCop.Analyzers.Lightup;
@@ -86,7 +84,7 @@ private static void HandleTupleExpressionAction(SyntaxNodeAnalysisContext contex
8684
var tupleExpression = (TupleExpressionSyntaxWrapper)context.Node;
8785
foreach (var argument in tupleExpression.Arguments)
8886
{
89-
var inferredMemberName = SyntaxFactsEx.TryGetInferredMemberName(argument.Expression);
87+
var inferredMemberName = SyntaxFactsEx.TryGetInferredMemberName(argument.NameColon?.Name ?? argument.Expression);
9088
if (inferredMemberName != null)
9189
{
9290
CheckName(context, settings, inferredMemberName, argument.Expression.GetLocation(), false);

0 commit comments

Comments
 (0)