Skip to content

Commit

Permalink
S3903: Add support for record structs (#5574)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-strecker-sonarsource authored Apr 20, 2022
1 parent 33e637d commit 0818415
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public sealed class DeclareTypesInNamespaces : DeclareTypesInNamespacesBase<Synt
SyntaxKind.EnumDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKindEx.RecordClassDeclaration,
SyntaxKindEx.RecordStructDeclaration,
};

protected override SyntaxToken GetTypeIdentifier(SyntaxNode declaration) =>
Expand All @@ -51,6 +52,7 @@ protected override bool IsInnerTypeOrWithinNamespace(SyntaxNode declaration, Sem
SyntaxKind.NamespaceDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKindEx.RecordClassDeclaration,
SyntaxKindEx.RecordStructDeclaration,
SyntaxKindEx.FileScopedNamespaceDeclaration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using SonarAnalyzer.Extensions;
using SonarAnalyzer.Helpers;

namespace SonarAnalyzer.Rules
Expand All @@ -30,7 +30,6 @@ public abstract class DeclareTypesInNamespacesBase<TSyntaxKind> : SonarDiagnosti
protected const string DiagnosticId = "S3903";

protected abstract TSyntaxKind[] SyntaxKinds { get; }

protected abstract bool IsInnerTypeOrWithinNamespace(SyntaxNode declaration, SemanticModel semanticModel);
protected abstract SyntaxToken GetTypeIdentifier(SyntaxNode declaration);

Expand All @@ -43,9 +42,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
c =>
{
var declaration = c.Node;

if (c.ContainingSymbol.Kind != SymbolKind.NamedType
|| IsInnerTypeOrWithinNamespace(declaration, c.SemanticModel))
if (c.IsRedundantPositionalRecordContext() || IsInnerTypeOrWithinNamespace(declaration, c.SemanticModel))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
record struct Bar // FN
record struct Bar // Noncompliant {{Move 'Bar' into a named namespace.}}
// ^^^
{
record struct InnerBar { } // Compliant - we want to report only on the outer record
record struct InnerBar { } // Compliant - we want to report only on the outer record
}

public record class Product { } // Noncompliant

0 comments on commit 0818415

Please sign in to comment.