Skip to content
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
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix analyzer [RCS1032](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1032) ([PR](https://github.com/dotnet/roslynator/pull/1289))
- Fix analyzer [RCS1176](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1176) ([PR](https://github.com/dotnet/roslynator/pull/1291))
- Fix analyzer [RCS1197](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1197) ([PR](https://github.com/dotnet/roslynator/pull/1166))
- Fix analyzer [RCS1093](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1093) ([PR](https://github.com/dotnet/roslynator/pull/1296))

## [4.6.4] - 2023-11-24

Expand Down
36 changes: 31 additions & 5 deletions src/Analyzers/CSharp/Analysis/FileContainsNoCodeAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -40,11 +41,36 @@ private static void AnalyzeCompilationUnit(SyntaxNodeAnalysisContext context)

if (compilationUnit.Span == token.Span
&& !token.HasTrailingTrivia
&& !token.LeadingTrivia.Any(f => f.IsKind(
SyntaxKind.IfDirectiveTrivia,
SyntaxKind.ElseDirectiveTrivia,
SyntaxKind.ElifDirectiveTrivia,
SyntaxKind.EndIfDirectiveTrivia)))
&& !token.LeadingTrivia.Any(trivia =>
{
switch (trivia.Kind())
{
case SyntaxKind.IfDirectiveTrivia:
case SyntaxKind.ElseDirectiveTrivia:
case SyntaxKind.ElifDirectiveTrivia:
case SyntaxKind.EndIfDirectiveTrivia:
{
return true;
}
case SyntaxKind.PragmaWarningDirectiveTrivia:
{
var pragma = (PragmaWarningDirectiveTriviaSyntax)trivia.GetStructure();

foreach (ExpressionSyntax errorCode in pragma.ErrorCodes)
{
if (errorCode is IdentifierNameSyntax identifierName
&& string.Equals(identifierName.Identifier.ValueText, DiagnosticRules.FileContainsNoCode.Id, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

break;
}
}

return false;
}))
{
SyntaxTree syntaxTree = compilationUnit.SyntaxTree;

Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1093FileContainsNoCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ class C

");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.FileContainsNoCode)]
public async Task TestNoDiagnostic_PragmaWarningDirective()
{
await VerifyNoDiagnosticAsync(@"#pragma warning disable RCS1093 // Remove file with no code.
//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.IO;

//public class TestClass
//{
//}");
}
}