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

Update SA1119 to allow parenthesis around a ref ternary conditional expression when it is the left-hand side of an assigment #3737

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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