Skip to content

Commit 93b1b3e

Browse files
authored
Fix renaming constructor via alias (#79175)
Fix #58463
2 parents 0ba91aa + 6935d38 commit 93b1b3e

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/EditorFeatures/Test2/Rename/CSharp/AliasTests.vb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,5 +730,46 @@ class Program
730730
result.AssertLabeledSpansAre("resolved", "var y = (x as int?) + 1;", RelatedLocationType.ResolvedNonReferenceConflict)
731731
End Using
732732
End Sub
733+
734+
<Theory, WorkItem("https://github.com/dotnet/roslyn/issues/58463")>
735+
<CombinatorialData>
736+
Public Sub RenameReferencingConstreuctorViaAlias(host As RenameTestHost)
737+
Using result = RenameEngineResult.Create(_outputHelper,
738+
<Workspace>
739+
<Project Language="C#" CommonReferences="true">
740+
<Document><![CDATA[
741+
using [|ToBeRenamed|] = N.M.[|ToBeRenamed|];
742+
743+
namespace N
744+
{
745+
class Program
746+
{
747+
static void Main(string[] args)
748+
{
749+
[|ToBeRenamed|] myClass = new [|ToBeRenamed|]("hello"); // references to the constructor via an identically named alias should also be renamed
750+
}
751+
}
752+
}
753+
754+
]]></Document>
755+
<Document><![CDATA[
756+
namespace N.M
757+
{
758+
internal class [|$$ToBeRenamed|]
759+
{
760+
private string myVar2;
761+
internal [|ToBeRenamed|](string var1)
762+
{
763+
myVar2 = var1;
764+
}
765+
}
766+
}
767+
768+
]]></Document>
769+
</Project>
770+
</Workspace>, host:=host, renameTo:="ThisIsTheNewName")
771+
772+
End Using
773+
End Sub
733774
End Class
734775
End Namespace

src/Workspaces/Core/Portable/Rename/SymbolicRenameLocations.ReferenceProcessing.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ internal static async Task<IEnumerable<RenameLocation>> GetRenamableReferenceLoc
282282
// rather than a whole namespace of stuff.
283283
if (location.Alias != null)
284284
{
285-
if (location.Alias.Name == referencedSymbol.Name)
285+
var referencedSymbolName = referencedSymbol is IMethodSymbol { MethodKind: MethodKind.Constructor } constructorSymbol
286+
? constructorSymbol.ContainingType.Name
287+
: referencedSymbol.Name;
288+
289+
if (location.Alias.Name == referencedSymbolName)
286290
{
287291
results.Add(new RenameLocation(location.Location, location.Document.Id,
288292
candidateReason: location.CandidateReason, isRenamableAliasUsage: true, isWrittenTo: location.IsWrittenTo));

0 commit comments

Comments
 (0)