diff --git a/ChangeLog.md b/ChangeLog.md index 71d8a88324..a6cbc447fd 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fix refactoring 'Change accessibility' ([RR0186](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0186)) ([PR](https://github.com/dotnet/roslynator/pull/1599)) + ### Changed - Move `DiagnosticRules` and `DiagnosticIdentifiers` to `Roslynator.Common` ([PR](https://github.com/dotnet/roslynator/pull/1597)) diff --git a/src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs b/src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs index 874b897492..71886d0569 100644 --- a/src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs +++ b/src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs @@ -1,6 +1,7 @@ // 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.Collections.Immutable; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -39,9 +40,17 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy if (syntaxReferences.Length > 1) { - ImmutableArray memberDeclarations = ImmutableArray.CreateRange( - syntaxReferences, - f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken)); + ImmutableArray.Builder memberDeclarations = ImmutableArray.CreateBuilder(); + + foreach (SyntaxReference syntaxReference in syntaxReferences) + { + SyntaxNode declaration = await syntaxReference.GetSyntaxAsync(context.CancellationToken).ConfigureAwait(false); + + if (node.RawKind != declaration.RawKind) + return; + + memberDeclarations.Add((MemberDeclarationSyntax)declaration); + } ImmutableArray.Builder typeDeclarationActions = ImmutableArray.CreateBuilder(); @@ -52,7 +61,7 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy { typeDeclarationActions.Add(CodeActionFactory.Create( SyntaxFacts.GetText(accessibility), - ct => RefactorAsync(context.Solution, memberDeclarations, accessibility, ct), + ct => RefactorAsync(context.Solution, memberDeclarations.ToImmutable(), accessibility, ct), RefactoringDescriptors.ChangeAccessibility, accessibility.ToString())); } diff --git a/src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs b/src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs index cb75a91ee2..53b8beb524 100644 --- a/src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs +++ b/src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs @@ -163,4 +163,17 @@ class C : B [||]public override string M() => null; }", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Private))); } + + [Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)] + public async Task Test_InvalidCode() + { + await VerifyNoRefactoringAsync(""" +[||]internal class Program; +{ + static void Main(string[] args) + { + } +} +""", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Internal)), options: Options.AddAllowedCompilerDiagnosticIds(["CS8803", "CS0260", "CS7022"])); + } }