From 8b07e0b28646733ab5afce7dadead83ccf398b48 Mon Sep 17 00:00:00 2001 From: IXLLEGACYIXL <107197024+IXLLEGACYIXL@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:21:33 +0200 Subject: [PATCH] fix: strdiag001 on file scoped classes (#2309) Co-authored-by: IXLLEGACYIXL --- .../AnalyzerTests/STRDIAG001_Test.cs | 3 +-- .../Analyzers/STRDIAG001InvalidDataContract.cs | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/sources/core/Stride.Core.CompilerServices.Tests/AnalyzerTests/STRDIAG001_Test.cs b/sources/core/Stride.Core.CompilerServices.Tests/AnalyzerTests/STRDIAG001_Test.cs index 3afe3f68ed..fe72ea7403 100644 --- a/sources/core/Stride.Core.CompilerServices.Tests/AnalyzerTests/STRDIAG001_Test.cs +++ b/sources/core/Stride.Core.CompilerServices.Tests/AnalyzerTests/STRDIAG001_Test.cs @@ -21,8 +21,7 @@ public void No_Error_On_Private_Inner_Class_without_DataContract() string sourceCode = string.Format(ClassTemplates.BasicClassTemplate, "private class InnerClass { }"); TestHelper.ExpectNoDiagnosticsErrors(sourceCode); } - // TODO: Enable with .NET8 merge as we need a higher C# version - [Fact(Skip = "file scoped classes won't compile")] + [Fact] public void Error_On_file_scope_Class_with_DataContract() { string sourceCode = "using Stride.Core; [DataContract] file class FileScopeClass { }"; diff --git a/sources/core/Stride.Core.CompilerServices/Analyzers/STRDIAG001InvalidDataContract.cs b/sources/core/Stride.Core.CompilerServices/Analyzers/STRDIAG001InvalidDataContract.cs index b267719aec..ca5601311e 100644 --- a/sources/core/Stride.Core.CompilerServices/Analyzers/STRDIAG001InvalidDataContract.cs +++ b/sources/core/Stride.Core.CompilerServices/Analyzers/STRDIAG001InvalidDataContract.cs @@ -49,7 +49,10 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo if (!symbol.HasAttribute(dataContractAttribute)) return; - if(!symbol.IsVisibleToSerializer(hasDataMemberAttribute: true)) + if (symbol.IsFileLocal) + Rule.ReportDiagnostics(context, symbol); + + if (!symbol.IsVisibleToSerializer(hasDataMemberAttribute: true)) Rule.ReportDiagnostics(context, symbol); } }