Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Rules/MA0151.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Source: [DebuggerDisplayAttributeShouldContainValidExpressionsAnalyzer.cs](https
<!-- sources -->

Validates the expressions within `[DebuggerDisplay]` attributes to ensure they reference valid members.
The names are resolved in the type, then in its containing types and its containing namespaces.

````c#
[DebuggerDisplay("{Display}")] // compliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private static bool IsValid(Compilation compilation, ISymbol rootSymbol, List<st
}

var firstMember = syntax[0];
var current = FindSymbol(compilation, rootSymbol, firstMember) ?? FindGlobalSymbol(compilation, firstMember);
var current = FindFirstSymbol(compilation, rootSymbol, firstMember);
if (current is null)
{
invalidMember = firstMember;
Expand Down Expand Up @@ -259,6 +259,20 @@ private static bool IsValid(Compilation compilation, ISymbol rootSymbol, List<st
return null;
}

static ISymbol? FindFirstSymbol(Compilation compilation, ISymbol rootSymbol, string name)
{
// The name is looked up in the type, then in its containing types and its containing namespaces
for (var symbol = rootSymbol; symbol is not null and not INamespaceSymbol { IsGlobalNamespace: true }; symbol = symbol.ContainingSymbol)
{
// The namespace of a type declared in the compilation only contains the types of the compilation
var scope = symbol is INamespaceSymbol namespaceSymbol ? compilation.GetCompilationNamespace(namespaceSymbol) ?? symbol : symbol;
if (FindSymbol(compilation, scope, name) is { } result)
return result;
}

return FindGlobalSymbol(compilation, name);
}

static ISymbol? FindGlobalSymbol(Compilation compilation, string name)
{
if (CSharpKeywordToTypeName.TryGetValue(name, out var specialType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,70 @@ public interface IFoo

return test.RunAsync();
}

[Fact]
public Task CallStaticMethodOnTypeOfContainingNamespace()
{
var test = CreateTest();
test.TestCode = """
using System.Diagnostics;
namespace N
{
[DebuggerDisplay("{Helper.Format(Value)}")]
public class Dummy
{
public int Value { get; set; }
}

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

return test.RunAsync();
}

[Fact]
public Task StaticMemberOfContainingType()
{
var test = CreateTest();
test.TestCode = """
using System.Diagnostics;
public class Outer
{
private static string Prefix => "";

[DebuggerDisplay("{Prefix.Length}")]
public class Dummy
{
}
}
""";

return test.RunAsync();
}

[Fact]
public Task UnknownMemberOfContainingNamespace()
{
var test = CreateTest();
test.TestCode = """
using System.Diagnostics;
namespace N
{
[{|MA0151:DebuggerDisplay("{Helper.Unknown()}")|}]
public class Dummy
{
}

public static class Helper
{
}
}
""";

return test.RunAsync();
}
}
Loading