Skip to content

Fix MA0151 false positive on members of containing types and namespaces - #1592

Merged
meziantou merged 1 commit into
mainfrom
fix/ma0151-name-resolution
Sep 23, 2026
Merged

meziantou merged 1 commit into
mainfrom
fix/ma0151-name-resolution

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

MA0151 resolved the first name of a DebuggerDisplay expression only in the members of the type and in the global namespace. It reported valid expressions that use a type of the containing namespace or a static member of a containing type:

namespace N
{
    [DebuggerDisplay("{Helper.Format(Value)}")] // Member 'Helper' does not exist
    public class Dummy
    {
        public int Value { get; set; }
    }

    public static class Helper
    {
        public static string Format(int value) => value.ToString();
    }
}
public class Outer
{
    private static string Prefix => "";

    [DebuggerDisplay("{Prefix.Length}")] // Member 'Prefix' does not exist
    public class Dummy { }
}

Expected: no diagnostic. Actual: MA0151.

Fix

The first name is now looked up the way the C# compiler looks it up: in the type (including its base types), then in each containing type, then in each containing namespace, and finally in the global namespace and the keyword types, as before. The namespaces are converted to compilation namespaces so the types of referenced assemblies in the same namespace are found too. The rule documentation describes the lookup.

Tests

Added 3 tests to DebuggerDisplayAttributeShouldContainValidExpressionsAnalyzerTests:

  • CallStaticMethodOnTypeOfContainingNamespace (failed before the fix)
  • StaticMemberOfContainingType (failed before the fix)
  • UnknownMemberOfContainingNamespace, which checks that an unknown member of a type found in the containing namespace is still reported

The class passes on roslyn5.9 and roslyn4.8 (93 tests each).

@meziantou
meziantou marked this pull request as ready for review September 23, 2026 04:12
@meziantou
meziantou merged commit 3c3a091 into main Sep 23, 2026
14 checks passed
@meziantou
meziantou deleted the fix/ma0151-name-resolution branch September 23, 2026 04:12
This was referenced Sep 23, 2026
This was referenced Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant