Skip to content

Commit 337d6e2

Browse files
Add test for SA1000's handling of c# 7.1 default literal expressions
#2420
1 parent 17b613d commit 337d6e2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,27 @@ public async Task TestStackAllocImplicitArrayStatementAsync()
252252
// this case is handled by SA1026, so it shouldn't be reported here
253253
await this.TestKeywordStatementAsync(statementWithSpace, DiagnosticResult.EmptyDiagnosticResults, statementWithSpace, languageVersion: LanguageVersion.CSharp7_3.OrLaterDefault()).ConfigureAwait(false);
254254
}
255+
256+
[Theory]
257+
[InlineData("var x = b ? default : 3;")]
258+
[InlineData("var x = b ?default: 3;")]
259+
[InlineData("int x = default;")]
260+
[InlineData("int x =default ;")]
261+
[WorkItem(2420, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/2420")]
262+
public async Task TestDefaultLiteralExpressionAsync(string statement)
263+
{
264+
var testCode = $@"namespace TestNamespace
265+
{{
266+
public class TestClass
267+
{{
268+
public void TestMethod(bool b)
269+
{{
270+
{statement}
271+
}}
272+
}}
273+
}}";
274+
275+
await VerifyCSharpDiagnosticAsync(LanguageVersion.CSharp7_1.OrLaterDefault(), testCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
276+
}
255277
}
256278
}

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.SpacingRules
75
{
86
using System;
@@ -143,7 +141,7 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
143141
case SyntaxKind.DefaultKeyword:
144142
if (token.Parent.IsKind(SyntaxKindEx.DefaultLiteralExpression))
145143
{
146-
// Ignore spacing around a default literal expression for now
144+
// Ignore spacing around a default literal expression
147145
break;
148146
}
149147

documentation/SA1000.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ The following C# keywords should always be followed by a single space: `and`, `a
2727
`foreach`, `from`, `group`, `if`, `in`, `is`, `into`, `join`, `let`, `lock`, `not`, `orderby`, `or`, `out`, `ref`, `return`, `select`,
2828
`switch`, `using`, `var`, `where`, `while`, `yield`.
2929

30-
The following keywords should not be followed by any space: `checked`, `default`, `nameof`, `sizeof`, `typeof`, `unchecked`.
30+
The `checked`, `default`, `nameof`, `sizeof`, `typeof`, and `unchecked` keywords should not be followed by any space, except in the following cases:
31+
32+
* 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.
3133

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

0 commit comments

Comments
 (0)