Repository: dotnet/roslyn
File: src/Workspaces/Core/Portable/FindSymbols/ReferenceLocationExtensions.cs (C#)

Code before:

                foreach (var documentGroup in projectGroup)
                {
                    var document = documentGroup.Key;
                    var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
                    AddSymbols(semanticModel, documentGroup, result);
                }

                // Keep compilation alive so that GetSemanticModelAsync remains cheap
                GC.KeepAlive(compilation);
            }
        }

        return result;
    }

    private static void AddSymbols(
        SemanticModel semanticModel,
        IEnumerable<ReferenceLocation> references,
        Dictionary<ISymbol, List<Location>> result)
    {
        foreach (var reference in references)
        {
            // Filter out references in nameof expressions and typeof expressions
            // This fixes the most common Call Hierarchy false positives
            if (IsTokenInNameofOrTypeof(reference))
                continue;
            
            var containingSymbol = GetEnclosingMethodOrPropertyOrField(semanticModel, reference);
            if (containingSymbol != null)
            {
                if (!result.TryGetValue(containingSymbol, out var locations))
                {
                    locations = [];
                    result.Add(containingSymbol, locations);
                }

                locations.Add(reference.Location);
            }
        }
    }

    /// <summary>
    /// Checks if a reference location is in a nameof expression or typeof expression.
    /// These are common false positives in Call Hierarchy.
    /// </summary>
    private static bool IsTokenInNameofOrTypeof(ReferenceLocation reference)
    {
        if (!reference.Location.IsInSource)
            return false;


Review comment (refactor):
i think you can remove this entirely.  You can juse use reference.SymbolUsageInfo.  Then example ValueUsageInfo to see if it is IsNameOnly.

Code after:

                foreach (var documentGroup in projectGroup)
                {
                    var document = documentGroup.Key;
                    var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
                    AddSymbols(semanticModel, documentGroup, result);
                }

                // Keep compilation alive so that GetSemanticModelAsync remains cheap
                GC.KeepAlive(compilation);
            }
        }

        return result;
    }

    private static void AddSymbols(
        SemanticModel semanticModel,
        IEnumerable<ReferenceLocation> references,
        Dictionary<ISymbol, List<Location>> result)
    {
        foreach (var reference in references)
        {
            // Filter out name-only references (e.g. nameof expressions, typeof expressions)
            // This fixes the most common Call Hierarchy false positives
            if (reference.SymbolUsageInfo.ValueUsageInfoOpt?.IsNameOnly() == true)
                continue;

            var containingSymbol = GetEnclosingMethodOrPropertyOrField(semanticModel, reference);
            if (containingSymbol != null)
            {
                if (!result.TryGetValue(containingSymbol, out var locations))
                {
                    locations = [];
                    result.Add(containingSymbol, locations);
                }

                locations.Add(reference.Location);
            }
        }
    }

    private static ISymbol? GetEnclosingMethodOrPropertyOrField(
        SemanticModel semanticModel,
        ReferenceLocation reference)
    {
        var enclosingSymbol = semanticModel.GetEnclosingSymbol(reference.Location.SourceSpan.Start);

        for (var current = enclosingSymbol; current != null; current = current.ContainingSymbol)
        {
            if (current.Kind == SymbolKind.Field)