Skip to content

Commit 498bb29

Browse files
authored
Fix refactoring 'Change accessibility' (RR0186) (#1599)
1 parent c2b1f46 commit 498bb29

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix refactoring 'Change accessibility' ([RR0186](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0186)) ([PR](https://github.com/dotnet/roslynator/pull/1599))
13+
1014
### Changed
1115

1216
- Move `DiagnosticRules` and `DiagnosticIdentifiers` to `Roslynator.Common` ([PR](https://github.com/dotnet/roslynator/pull/1597))

src/Refactorings/CSharp/Refactorings/AccessModifierRefactoring.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using System.Collections.Immutable;
4+
using System.Linq;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Microsoft.CodeAnalysis;
@@ -39,9 +40,17 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
3940

4041
if (syntaxReferences.Length > 1)
4142
{
42-
ImmutableArray<MemberDeclarationSyntax> memberDeclarations = ImmutableArray.CreateRange(
43-
syntaxReferences,
44-
f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken));
43+
ImmutableArray<MemberDeclarationSyntax>.Builder memberDeclarations = ImmutableArray.CreateBuilder<MemberDeclarationSyntax>();
44+
45+
foreach (SyntaxReference syntaxReference in syntaxReferences)
46+
{
47+
SyntaxNode declaration = await syntaxReference.GetSyntaxAsync(context.CancellationToken).ConfigureAwait(false);
48+
49+
if (node.RawKind != declaration.RawKind)
50+
return;
51+
52+
memberDeclarations.Add((MemberDeclarationSyntax)declaration);
53+
}
4554

4655
ImmutableArray<CodeAction>.Builder typeDeclarationActions = ImmutableArray.CreateBuilder<CodeAction>();
4756

@@ -52,7 +61,7 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, Sy
5261
{
5362
typeDeclarationActions.Add(CodeActionFactory.Create(
5463
SyntaxFacts.GetText(accessibility),
55-
ct => RefactorAsync(context.Solution, memberDeclarations, accessibility, ct),
64+
ct => RefactorAsync(context.Solution, memberDeclarations.ToImmutable(), accessibility, ct),
5665
RefactoringDescriptors.ChangeAccessibility,
5766
accessibility.ToString()));
5867
}

src/Tests/Refactorings.Tests/RR0186ChangeAccessibilityTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,17 @@ class C : B
163163
[||]public override string M() => null;
164164
}", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Private)));
165165
}
166+
167+
[Fact, Trait(Traits.Refactoring, RefactoringIdentifiers.ChangeAccessibility)]
168+
public async Task Test_InvalidCode()
169+
{
170+
await VerifyNoRefactoringAsync("""
171+
[||]internal class Program;
172+
{
173+
static void Main(string[] args)
174+
{
175+
}
176+
}
177+
""", equivalenceKey: EquivalenceKey.Create(RefactoringId, nameof(Accessibility.Internal)), options: Options.AddAllowedCompilerDiagnosticIds(["CS8803", "CS0260", "CS7022"]));
178+
}
166179
}

0 commit comments

Comments
 (0)