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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public static Location FindTypeExpressionOrNullLocation(this AttributeArgumentSy
return null;
}
}
return targetSymbol.GetAttributes().First(attributeSyntaxLocationMatches);
// Sometimes an attribute is put on a symbol that is nested within the containing symbol.
// For example, the ContainingSymbol for an AttributeSyntax on a parameter have a ContainingSymbol of the method.
// Since this method is internal and the callers don't care about attributes on parameters, we just allow
// this method to return null in those cases.
return targetSymbol.GetAttributes().FirstOrDefault(attributeSyntaxLocationMatches);

bool attributeSyntaxLocationMatches(AttributeData attrData)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,22 @@ await VerifyCS.VerifyAnalyzerAsync(source,
}

[Fact]
public async Task UnrelatedAssemblyOrModuleTargetDiagnostic_DoesNotCauseException()
public async Task UnrelatedAttributes_DoesNotCauseException()
{
string source = """
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly:AssemblyMetadata("MyKey", "MyValue")]
[module:SkipLocalsInit]

public class X
{
void Foo([MarshalAs(UnmanagedType.I4)] int i)
{
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(source);
Expand Down