From 6c9be6030f4f4c189da98e87d98348f61ef9e5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Wed, 15 May 2024 19:23:08 +0200 Subject: [PATCH] Add test for SA1000's handling of c# 7.1 default literal expressions #2420 --- .../SpacingRules/SA1000CSharp7UnitTests.cs | 22 +++++++++++++++++++ .../SA1000KeywordsMustBeSpacedCorrectly.cs | 4 +--- documentation/SA1000.md | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs index 96f4b8455..ff11e58eb 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1000CSharp7UnitTests.cs @@ -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); + } } } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs index cd86a7c12..35cfea46c 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1000KeywordsMustBeSpacedCorrectly.cs @@ -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; @@ -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; } diff --git a/documentation/SA1000.md b/documentation/SA1000.md index e3b22b272..d206c3966 100644 --- a/documentation/SA1000.md +++ b/documentation/SA1000.md @@ -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: