Skip to content

Commit

Permalink
Merge pull request #3823 from bjornhellander/feature/sa1003-null-coal…
Browse files Browse the repository at this point in the history
…escing-assignment-operator-3822

Update SA1003 to handle the null-coalescing assignment operator, the unsigned right-shift operator, the unsigned right-shift assignment operator and the null-forgiving operator
  • Loading branch information
sharwell authored Apr 8, 2024
2 parents 2081d85 + 9a680bd commit 045aba1
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,92 @@

namespace StyleCop.Analyzers.Test.CSharp11.SpacingRules
{
using System.Threading;
using System.Threading.Tasks;
using StyleCop.Analyzers.Test.CSharp10.SpacingRules;
using Xunit;

using static StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly;
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1003SymbolsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.SA1003CodeFixProvider>;

public partial class SA1003CSharp11UnitTests : SA1003CSharp10UnitTests
{
[Fact]
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
public async Task TestUnsignedRightShiftAssignmentOperatorAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod(int x)
{
x{|#0:>>>=|}0;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod(int x)
{
x >>>= 0;
}
}
}
";

var expected = new[]
{
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments(">>>="),
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments(">>>="),
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
public async Task TestUnsignedRightShiftOperatorAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public int TestMethod(int x)
{
return x{|#0:>>>|}0;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public int TestMethod(int x)
{
return x >>> 0;
}
}
}
";

var expected = new[]
{
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments(">>>"),
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments(">>>"),
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,81 @@ public void TestMethod()
FixedCode = fixedCode,
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
public async Task TestNullCoalescingAssignmentOperatorAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod(int? x)
{
x{|#0:??=|}0;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public void TestMethod(int? x)
{
x ??= 0;
}
}
}
";

var expected = new[]
{
Diagnostic(DescriptorPrecededByWhitespace).WithLocation(0).WithArguments("??="),
Diagnostic(DescriptorFollowedByWhitespace).WithLocation(0).WithArguments("??="),
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}

[Fact]
[WorkItem(3822, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3822")]
public async Task TestNullForgivingOperatorAsync()
{
var testCode = @"
namespace TestNamespace
{
public class TestClass
{
public string TestMethod(string? x)
{
return x {|#0:!|} ;
}
}
}
";

var fixedCode = @"
namespace TestNamespace
{
public class TestClass
{
public string TestMethod(string? x)
{
return x!;
}
}
}
";

var expected = new[]
{
Diagnostic(DescriptorNotPrecededByWhitespace).WithLocation(0).WithArguments("!"),
Diagnostic(DescriptorNotFollowedByWhitespace).WithLocation(0).WithArguments("!"),
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
2 changes: 2 additions & 0 deletions StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SyntaxKindEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ internal static class SyntaxKindEx
public const SyntaxKind IsPatternExpression = (SyntaxKind)8657;
public const SyntaxKind RangeExpression = (SyntaxKind)8658;
public const SyntaxKind ImplicitObjectCreationExpression = (SyntaxKind)8659;
public const SyntaxKind UnsignedRightShiftExpression = (SyntaxKind)8692;
public const SyntaxKind CoalesceAssignmentExpression = (SyntaxKind)8725;
public const SyntaxKind UnsignedRightShiftAssignmentExpression = (SyntaxKind)8726;
public const SyntaxKind IndexExpression = (SyntaxKind)8741;
public const SyntaxKind DefaultLiteralExpression = (SyntaxKind)8755;
public const SyntaxKind LocalFunctionStatement = (SyntaxKind)8830;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
SyntaxKind.GreaterThanOrEqualExpression,
SyntaxKind.LeftShiftExpression,
SyntaxKind.RightShiftExpression,
SyntaxKindEx.UnsignedRightShiftExpression,
SyntaxKind.AddExpression,
SyntaxKind.SubtractExpression,
SyntaxKind.MultiplyExpression,
Expand All @@ -105,7 +106,10 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
SyntaxKind.AddressOfExpression);

private static readonly ImmutableArray<SyntaxKind> PostfixUnaryExpressionKinds =
ImmutableArray.Create(SyntaxKind.PostIncrementExpression, SyntaxKind.PostDecrementExpression);
ImmutableArray.Create(
SyntaxKind.PostIncrementExpression,
SyntaxKind.PostDecrementExpression,
SyntaxKindEx.SuppressNullableWarningExpression);

private static readonly ImmutableArray<SyntaxKind> AssignmentExpressionKinds =
ImmutableArray.Create(
Expand All @@ -114,11 +118,13 @@ internal class SA1003SymbolsMustBeSpacedCorrectly : DiagnosticAnalyzer
SyntaxKind.ExclusiveOrAssignmentExpression,
SyntaxKind.LeftShiftAssignmentExpression,
SyntaxKind.RightShiftAssignmentExpression,
SyntaxKindEx.UnsignedRightShiftAssignmentExpression,
SyntaxKind.AddAssignmentExpression,
SyntaxKind.SubtractAssignmentExpression,
SyntaxKind.MultiplyAssignmentExpression,
SyntaxKind.DivideAssignmentExpression,
SyntaxKind.ModuloAssignmentExpression,
SyntaxKindEx.CoalesceAssignmentExpression,
SyntaxKind.SimpleAssignmentExpression);

private static readonly Action<SyntaxNodeAnalysisContext> ConstructorDeclarationAction = HandleConstructorDeclaration;
Expand Down

0 comments on commit 045aba1

Please sign in to comment.