Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@

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.Test.Verifiers.StyleCopCodeFixVerifier<
StyleCop.Analyzers.SpacingRules.SA1000KeywordsMustBeSpacedCorrectly,
StyleCop.Analyzers.SpacingRules.TokenSpacingCodeFixProvider>;

public class SA1000CSharp11UnitTests : SA1000CSharp10UnitTests
{
[Fact]
public async Task TestCheckedOperatorDeclarationAsync()
{
// NOTE: A checked operator requires a non-checked operator as well
// NOTE: Implicit conversion operators can not be checked
var testCode = @"
public class MyClass
{
public static MyClass operator {|#0:checked|}-(MyClass x) => x;
public static MyClass operator -(MyClass x) => x;

public static explicit operator {|#1:checked|}@MyClass(int i) => new MyClass();
public static explicit operator MyClass(int i) => new MyClass();
}";

var fixedCode = @"
public class MyClass
{
public static MyClass operator checked -(MyClass x) => x;
public static MyClass operator -(MyClass x) => x;

public static explicit operator checked @MyClass(int i) => new MyClass();
public static explicit operator MyClass(int i) => new MyClass();
}";

var expected = new[]
{
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(0),
Diagnostic().WithArguments("checked", string.Empty, "followed").WithLocation(1),
};
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,23 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)

case SyntaxKind.CheckedKeyword:
case SyntaxKind.UncheckedKeyword:
if (token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken))
switch (token.Parent.Kind())
{
case SyntaxKind.CheckedStatement:
case SyntaxKind.UncheckedStatement:
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
HandleRequiredSpaceToken(ref context, token);
}
else
{
break;

case SyntaxKind.CheckedExpression:
case SyntaxKind.UncheckedExpression:
HandleDisallowedSpaceToken(ref context, token);
break;

default:
// So far an unknown case, so we have no opinion yet
break;
}

break;
Expand Down