From 8906e8eba7ef5e25326212bd56acebb51dddc526 Mon Sep 17 00:00:00 2001
From: Anton Paulovich
Date: Mon, 19 Jul 2021 22:53:23 +0300
Subject: [PATCH] Update SA1119 to support C# Ranges (fix #3370)
---
.../SA1119CSharp8UnitTests.cs | 73 +++++++++++++++++++
...atementMustNotUseUnnecessaryParenthesis.cs | 1 +
2 files changed, 74 insertions(+)
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs
index 00625e0059..f60b42486b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/MaintainabilityRules/SA1119CSharp8UnitTests.cs
@@ -170,5 +170,78 @@ public unsafe void TestMethod()
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp8, testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
+
+ [Fact]
+ [WorkItem(3370, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3370")]
+ public async Task TestRangeFollowedByMemberCallAsync()
+ {
+ const string testCode = @"using System;
+
+public class TestClass
+{
+ public void TestMethod()
+ {
+ var array = (1..10).ToString();
+ }
+}
+";
+
+ await new CSharpTest(LanguageVersion.CSharp8)
+ {
+ ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31,
+ TestCode = testCode,
+ }.RunAsync(CancellationToken.None).ConfigureAwait(false);
+ }
+
+ [Fact]
+ [WorkItem(3370, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3370")]
+ public async Task TestRangeAsync()
+ {
+ const string testCode = @"using System;
+
+public class TestClass
+{
+ public void TestMethod()
+ {
+ var a = new int[0];
+ var b = a[(..)];
+ var range = (1..10);
+ }
+}
+";
+
+ const string fixedCode = @"using System;
+
+public class TestClass
+{
+ public void TestMethod()
+ {
+ var a = new int[0];
+ var b = a[..];
+ var range = 1..10;
+ }
+}
+";
+
+ DiagnosticResult[] expected =
+ {
+ Diagnostic(DiagnosticId).WithSpan(8, 19, 8, 23),
+ Diagnostic(ParenthesesDiagnosticId).WithLocation(8, 19),
+ Diagnostic(ParenthesesDiagnosticId).WithLocation(8, 22),
+ Diagnostic(DiagnosticId).WithSpan(9, 21, 9, 28),
+ Diagnostic(ParenthesesDiagnosticId).WithLocation(9, 21),
+ Diagnostic(ParenthesesDiagnosticId).WithLocation(9, 27),
+ };
+
+ var test = new CSharpTest(LanguageVersion.CSharp8)
+ {
+ ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31,
+ TestCode = testCode,
+ FixedCode = fixedCode,
+ };
+ test.ExpectedDiagnostics.AddRange(expected);
+
+ await test.RunAsync(CancellationToken.None).ConfigureAwait(false);
+ }
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs
index 080444749b..9dfa7b2e01 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers/MaintainabilityRules/SA1119StatementMustNotUseUnnecessaryParenthesis.cs
@@ -117,6 +117,7 @@ private static void HandleParenthesizedExpression(SyntaxNodeAnalysisContext cont
&& !node.Expression.IsKind(SyntaxKind.CoalesceExpression)
&& !node.Expression.IsKind(SyntaxKind.QueryExpression)
&& !node.Expression.IsKind(SyntaxKind.AwaitExpression)
+ && !node.Expression.IsKind(SyntaxKindEx.RangeExpression)
&& !node.IsKind(SyntaxKind.ConstructorDeclaration))
{
if (node.Expression.IsKind(SyntaxKind.ConditionalAccessExpression)