Skip to content

Commit

Permalink
Add test for SA1000's handling of c# 7.1 default literal expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornhellander committed May 15, 2024
1 parent 17b613d commit 6c9be60
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,27 @@ public async Task TestStackAllocImplicitArrayStatementAsync()
// this case is handled by SA1026, so it shouldn't be reported here
await this.TestKeywordStatementAsync(statementWithSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithSpace, languageVersion: LanguageVersion.CSharp7_3.OrLaterDefault()).ConfigureAwait(false);
}

[Theory]
[InlineData("var x = b ? default : 3;")]
[InlineData("var x = b ?default: 3;")]
[InlineData("int x = default;")]
[InlineData("int x =default ;")]
[WorkItem(2420, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2420")]
public async Task TestDefaultLiteralExpressionAsync(string statement)
{
var testCode = $@"namespace TestNamespace
{{
public class TestClass
{{
public void TestMethod(bool b)
{{
{statement}
}}
}}
}}";

await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#nullable disable

namespace StyleCop.Analyzers.SpacingRules
{
using System;
Expand Down Expand Up @@ -143,7 +141,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
case SyntaxKind.DefaultKeyword:
if (token.Parent.IsKind(SyntaxKindEx.DefaultLiteralExpression))
{
// Ignore spacing around a default literal expression for now
// Ignore spacing around a default literal expression
break;
}

Expand Down
4 changes: 3 additions & 1 deletion documentation/SA1000.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ The following C# keywords should always be followed by a single space: `and`, `a
`foreach`, `from`, `group`, `if`, `in`, `is`, `into`, `join`, `let`, `lock`, `not`, `orderby`, `or`, `out`, `ref`, `return`, `select`,
`switch`, `using`, `var`, `where`, `while`, `yield`.

The following keywords should not be followed by any space: `checked`, `default`, `nameof`, `sizeof`, `typeof`, `unchecked`.
The `checked`, `default`, `nameof`, `sizeof`, `typeof`, and `unchecked` keywords should not be followed by any space, except in the following cases:

* The `default` keyword is used as a c# 7.1 default literal expression. In this case it can both have and not have trailing spaces.

The `new` and `stackalloc` keywords should always be followed by a space, except in the following cases:

Expand Down

0 comments on commit 6c9be60

Please sign in to comment.