Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3903: Add support for record structs #5574

Merged
merged 4 commits into from
Apr 20, 2022
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 @@ -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