diff --git a/source/Analyzers/DiagnosticAnalyzers/CompilationUnitDiagnosticAnalyzer.cs b/source/Analyzers/DiagnosticAnalyzers/CompilationUnitDiagnosticAnalyzer.cs index 4cd9ab94cc..ac851c46d3 100644 --- a/source/Analyzers/DiagnosticAnalyzers/CompilationUnitDiagnosticAnalyzer.cs +++ b/source/Analyzers/DiagnosticAnalyzers/CompilationUnitDiagnosticAnalyzer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Immutable; +using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -41,7 +42,11 @@ private void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context) DeclareEachTypeInSeparateFileRefactoring.Analyze(context, compilationUnit); - if (compilationUnit.Span == compilationUnit.EndOfFileToken.Span) + SyntaxToken token = compilationUnit.EndOfFileToken; + + if (compilationUnit.Span == token.Span + && !token.HasTrailingTrivia + && token.LeadingTrivia.All(f => !f.IsDirective)) { context.ReportDiagnostic( DiagnosticDescriptors.RemoveFileWithNoCode, diff --git a/source/Tests/AnalyzersTests/AnalyzersTests.csproj b/source/Tests/AnalyzersTests/AnalyzersTests.csproj index 45ab4fcf75..0c15405037 100644 --- a/source/Tests/AnalyzersTests/AnalyzersTests.csproj +++ b/source/Tests/AnalyzersTests/AnalyzersTests.csproj @@ -66,6 +66,7 @@ + diff --git a/source/Tests/AnalyzersTests/RemoveFileWithNoCode2.cs b/source/Tests/AnalyzersTests/RemoveFileWithNoCode2.cs new file mode 100644 index 0000000000..2b1e3aa7e7 --- /dev/null +++ b/source/Tests/AnalyzersTests/RemoveFileWithNoCode2.cs @@ -0,0 +1,12 @@ +// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +#if ABC +// Copyright(c) Josef Pihrt.All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Roslynator.CSharp.Analyzers.Tests +{ + public static class RemoveFileWithNoCode + { + } +} +#endif