Another idea for an analyzer, warn when looking for an attribute on something where the AttributeUsage says it can't be.
eg.
given
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
sealed class FooAttribute : Attribute
{
}
MethodInfo methodInfo = ...
Type type = ...
var x = info.IsDefined(typeof(FooAttribute)); // this should warn because FooAttribute cannot be applied to a method.
var y = type.IsDefined(typeof(FooAttribute)); // this is fine because FooAttribute can be applied to one or more of the type targets (Class, Delegate, Enum, Interface, GenericParameter, Struct).
Should also apply to GetCustomAttributes(Type, Boolean) as well as the GetCustomAttribute<>() and GetCustomAttributes<>() extension methods.
Another idea for an analyzer, warn when looking for an attribute on something where the AttributeUsage says it can't be.
eg.
given
Should also apply to
GetCustomAttributes(Type, Boolean)as well as theGetCustomAttribute<>()andGetCustomAttributes<>()extension methods.