Skip to content

Commit

Permalink
Merge pull request #3737 from bjornhellander/feature/sa1119-ref-condi…
Browse files Browse the repository at this point in the history
…tional

Update SA1119 to allow parenthesis around a ref ternary conditional expression when it is the left-hand side of an assigment
  • Loading branch information
sharwell committed Nov 28, 2023
2 parents c0246ab + 380924c commit 081e2d0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules
{
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Test.Helpers;
using StyleCop.Analyzers.Test.MaintainabilityRules;
using Xunit;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
Expand All @@ -16,6 +19,31 @@ namespace StyleCop.Analyzers.Test.CSharp7.MaintainabilityRules

public partial class SA1119CSharp7UnitTests : SA1119UnitTests
{
public static IEnumerable<object[]> Assignments
{
get
{
yield return new object[] { "= 1" };
yield return new object[] { "+= 1" };
yield return new object[] { "-= 1" };
yield return new object[] { "*= 1" };
yield return new object[] { "/= 1" };
yield return new object[] { "%= 1" };
yield return new object[] { "&= 1" };
yield return new object[] { "|= 1" };
yield return new object[] { "^= 1" };
yield return new object[] { "<<= 1" };
yield return new object[] { ">>= 1" };
yield return new object[] { "++" };
yield return new object[] { "--" };

if (LightupHelpers.SupportsCSharp11)
{
yield return new object[] { ">>>= 1" };
}
}
}

/// <summary>
/// Verifies that extra parentheses in pattern matching is not reported.
/// </summary>
Expand Down Expand Up @@ -85,5 +113,21 @@ public void Bar()

await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Theory]
[MemberData(nameof(Assignments))]
[WorkItem(3712, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3712")]
public async Task TestConditionalRefAssignmentAsync(string assignment)
{
var testCode = $@"public class Foo
{{
public void Bar(bool b, ref int x, ref int y)
{{
(b ? ref x : ref y) {assignment};
}}
}}";

await VerifyCSharpDiagnosticAsync(LanguageVersionEx.CSharp7_2.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ private static void HandleParenthesizedExpression(SyntaxNodeAnalysisContext cont
return;
}

if (node.Parent is AssignmentExpressionSyntax assignmentExpression
&& node.Expression.IsKind(SyntaxKind.ConditionalExpression)
&& assignmentExpression.Left == node)
{
// NOTE: This is only valid syntax if the conditional expression is a ref expression
// Parenthesis can't be removed here
return;
}

if (!(node.Parent is ExpressionSyntax)
|| node.Parent is CheckedExpressionSyntax
|| node.Parent is MemberAccessExpressionSyntax)
Expand Down

0 comments on commit 081e2d0

Please sign in to comment.