Skip to content

Commit fe36546

Browse files
committed
Do not report SA1141 for single-element tuples
Fixes #3055
1 parent 1fe16b6 commit fe36546

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/ReadabilityRules/SA1141CSharp7UnitTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@ public void TestMethod(object x)
218218
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
219219
}
220220

221+
[Fact]
222+
[WorkItem(3055, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3055")]
223+
public async Task ValidateSingleElementValueTupleUsageAsync()
224+
{
225+
var testCode = @"using System;
226+
227+
public class TestClass
228+
{
229+
public void TestMethod()
230+
{
231+
var test1 = default(ValueTuple<int>);
232+
var test2 = default(ValueTuple<ValueTuple<int>>);
233+
ValueTuple<int> test3 = ValueTuple.Create(3);
234+
}
235+
}
236+
";
237+
238+
await VerifyCSharpDiagnosticAsync(testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
239+
}
240+
221241
/// <summary>
222242
/// Validates that the usage of <see cref="System.ValueTuple"/> within exception filtering will produce no diagnostics.
223243
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1141UseTupleSyntax.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ private static void HandleInvocationExpression(SyntaxNodeAnalysisContext context
121121
}
122122

123123
var invocationExpression = (InvocationExpressionSyntax)context.Node;
124+
if (invocationExpression.ArgumentList.Arguments.Count < 2)
125+
{
126+
// Tuple creation with less than two elements cannot use the language syntax
127+
return;
128+
}
129+
124130
if (!invocationExpression.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
125131
{
126132
return;
@@ -209,7 +215,7 @@ private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeS
209215

210216
private static void CheckGenericName(SyntaxNodeAnalysisContext context, GenericNameSyntax genericNameSyntax, Location reportLocation)
211217
{
212-
if (IsValueTuple(context, genericNameSyntax))
218+
if (IsValueTupleWithLanguageRepresentation(context, genericNameSyntax))
213219
{
214220
var location = reportLocation ?? genericNameSyntax.GetLocation();
215221
context.ReportDiagnostic(Diagnostic.Create(Descriptor, location));
@@ -224,10 +230,12 @@ private static void CheckGenericName(SyntaxNodeAnalysisContext context, GenericN
224230
}
225231
}
226232

227-
private static bool IsValueTuple(SyntaxNodeAnalysisContext context, ExpressionSyntax syntax)
233+
private static bool IsValueTupleWithLanguageRepresentation(SyntaxNodeAnalysisContext context, ExpressionSyntax syntax)
228234
{
229235
var symbolInfo = context.SemanticModel.GetSymbolInfo(syntax, context.CancellationToken);
230-
return (symbolInfo.Symbol is ITypeSymbol typeSymbol) && typeSymbol.IsTupleType();
236+
return symbolInfo.Symbol is INamedTypeSymbol typeSymbol
237+
&& typeSymbol.IsTupleType()
238+
&& typeSymbol.TupleElements().Length > 1;
231239
}
232240
}
233241
}

0 commit comments

Comments
 (0)