Fix MA0151 false positive on members of containing types and namespaces
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).