@@ -33,7 +33,17 @@ public static Location FindTypeExpressionOrNullLocation(this AttributeArgumentSy
3333 switch ( attributeTarget . Identifier . Kind ( ) )
3434 {
3535 case SyntaxKind . ReturnKeyword :
36- return ( ( IMethodSymbol ) targetSymbol ) . GetReturnTypeAttributes ( ) . First ( attributeSyntaxLocationMatches ) ;
36+ if ( targetSymbol is IMethodSymbol method )
37+ {
38+ // Sometimes an attribute is put on a symbol that is nested within the containing symbol.
39+ // For example, the ContainingSymbol for an AttributeSyntax on a local function has a ContainingSymbol of the method.
40+ // Since this method is internal and the callers don't care about attributes on local functions,
41+ // we just allow this method to return null in those cases.
42+ return method . GetReturnTypeAttributes ( ) . FirstOrDefault ( attributeSyntaxLocationMatches ) ;
43+ }
44+ // An attribute on the return value of a delegate type's Invoke method has a ContainingSymbol of the delegate type.
45+ // We don't care about the attributes in this case for the callers, so we'll just return null.
46+ return null ;
3747 case SyntaxKind . AssemblyKeyword :
3848 return targetSymbol . ContainingAssembly . GetAttributes ( ) . First ( attributeSyntaxLocationMatches ) ;
3949 case SyntaxKind . ModuleKeyword :
@@ -43,7 +53,8 @@ public static Location FindTypeExpressionOrNullLocation(this AttributeArgumentSy
4353 }
4454 }
4555 // Sometimes an attribute is put on a symbol that is nested within the containing symbol.
46- // For example, the ContainingSymbol for an AttributeSyntax on a parameter have a ContainingSymbol of the method.
56+ // For example, the ContainingSymbol for an AttributeSyntax on a parameter has a ContainingSymbol of the method
57+ // and an AttributeSyntax on a local function has a ContainingSymbol of the containing method.
4758 // Since this method is internal and the callers don't care about attributes on parameters, we just allow
4859 // this method to return null in those cases.
4960 return targetSymbol . GetAttributes ( ) . FirstOrDefault ( attributeSyntaxLocationMatches ) ;
0 commit comments