Repository: dotnet/roslyn
File: src/Compilers/CSharp/Test/Symbol/Symbols/Source/MethodTests.cs (C#)

Code before:
        public void PartialMethodsLocationsAndSyntaxReferences()
        {
            var source1 = """
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM();
                    }
                }
                """;

            var source2 = """
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM() { }
                    }
                }
                """;

            var comp = CreateCompilation([(source1, "source1"), (source2, "source2")]);
            comp.VerifyDiagnostics();

            var methodSymbols = comp.GetSymbolsWithName("PartialM");

            var method = (IMethodSymbol)methodSymbols.Single();
            Assert.NotNull(method);

            // For partial methods, Locations and DeclaringSyntaxReferences contain only one location
            Assert.Equal(1, method.Locations.Length);
            Assert.Equal(1, method.DeclaringSyntaxReferences.Length);

            // The single location is the definition part
            Assert.True(method.IsPartialDefinition);
            Assert.Null(method.PartialDefinitionPart);
            Assert.NotNull(method.PartialImplementationPart);

            // To get all locations, you need to use PartialImplementationPart
            var implementationPart = method.PartialImplementationPart;
            Assert.Equal(1, implementationPart.Locations.Length);
            Assert.Equal(1, implementationPart.DeclaringSyntaxReferences.Length);

            // Verify the locations are different
            Assert.NotEqual(method.Locations[0], implementationPart.Locations[0]);

            Assert.Equal("source1", method.Locations[0].SourceTree.FilePath);
            Assert.Equal("source2", implementationPart.Locations[0].SourceTree.FilePath);
        }
    }

Review comment (suggestion):
Consider inlining `methodSymbols` since only used once #Closed

Code after:
        public void PartialMethodsLocationsAndSyntaxReferences()
        {
            var source1 = """
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM();
                    }
                }
                """;

            var source2 = """
                namespace N1
                {
                    partial class C1
                    {
                        partial void PartialM() { }
                    }
                }
                """;

            var comp = CreateCompilation([(source1, "source1"), (source2, "source2")]);
            comp.VerifyDiagnostics();

            var method = (IMethodSymbol)comp.GetSymbolsWithName("PartialM").Single();

            // For partial methods, Locations and DeclaringSyntaxReferences contain only one location
            Assert.Equal(1, method.Locations.Length);
            Assert.Equal(1, method.DeclaringSyntaxReferences.Length);

            // The single location is the definition part
            Assert.True(method.IsPartialDefinition);
            Assert.Null(method.PartialDefinitionPart);
            Assert.NotNull(method.PartialImplementationPart);

            // To get all locations, you need to use PartialImplementationPart
            var implementationPart = method.PartialImplementationPart;
            Assert.Equal(1, implementationPart.Locations.Length);
            Assert.Equal(1, implementationPart.DeclaringSyntaxReferences.Length);

            // Verify the locations are different
            Assert.NotEqual(method.Locations[0], implementationPart.Locations[0]);

            Assert.Equal("source1", method.Locations[0].SourceTree.FilePath);
            Assert.Equal("source2", implementationPart.Locations[0].SourceTree.FilePath);
        }
    }
}