diff --git a/ChangeLog.md b/ChangeLog.md index 32954c9d2a..3abf37bf19 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS1182](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1182) ([PR](https://github.com/dotnet/roslynator/pull/1502)) - Fix analyzer [RCS1198](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1198) ([PR](https://github.com/dotnet/roslynator/pull/1501)) - Fix analyzer [RCS1214](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1214) ([PR](https://github.com/dotnet/roslynator/pull/1500)) +- Fix analyzer [RCS1018](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1018) ([PR](https://github.com/dotnet/roslynator/pull/1510)) ## [4.12.4] - 2024-06-01 diff --git a/src/CSharp/CSharp/SyntaxAccessibility`1.cs b/src/CSharp/CSharp/SyntaxAccessibility`1.cs index 62824c11bc..c6246657ce 100644 --- a/src/CSharp/CSharp/SyntaxAccessibility`1.cs +++ b/src/CSharp/CSharp/SyntaxAccessibility`1.cs @@ -200,6 +200,11 @@ public static Accessibility GetDefaultExplicitAccessibility(BaseTypeDeclarationS if (declaration is null) throw new ArgumentNullException(nameof(declaration)); +#if ROSLYN_4_4 + if (declaration.Modifiers.Contains(SyntaxKind.FileKeyword)) + return Accessibility.NotApplicable; +#endif + #if ROSLYN_4_0 return (declaration.IsParentKind(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.RecordDeclaration, SyntaxKind.RecordStructDeclaration)) #else @@ -354,6 +359,11 @@ public override Accessibility GetDefaultExplicitAccessibility(DelegateDeclaratio if (declaration is null) throw new ArgumentNullException(nameof(declaration)); +#if ROSLYN_4_4 + if (declaration.Modifiers.Contains(SyntaxKind.FileKeyword)) + return Accessibility.NotApplicable; +#endif + if (declaration.IsParentKind( SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, diff --git a/src/Tests/Analyzers.Tests/RCS1018AddAccessibilityModifiersTests.cs b/src/Tests/Analyzers.Tests/RCS1018AddAccessibilityModifiersTests.cs index e3d97c3460..58e8b43d36 100644 --- a/src/Tests/Analyzers.Tests/RCS1018AddAccessibilityModifiersTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1018AddAccessibilityModifiersTests.cs @@ -472,6 +472,22 @@ object I.this[int index] } } } +", options: Options.AddConfigOption(ConfigOptionKeys.AccessibilityModifiers, ConfigOptionValues.AccessibilityModifiers_Explicit)); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.AddOrRemoveAccessibilityModifiers)] + public async Task TestNoDiagnostic_FileAccessModifier() + { + await VerifyNoDiagnosticAsync(@" +using System; + +file class C; +file struct S; +file interface I; +file record R; +file record struct RS; +file delegate void D(); +file enum E; ", options: Options.AddConfigOption(ConfigOptionKeys.AccessibilityModifiers, ConfigOptionValues.AccessibilityModifiers_Explicit)); } }