Report MA0179 on the array-returning GetCustomAttributes overloads - #1393
Merged
Merged
Conversation
IsGetCustomAttributesInvocation cast the operation directly to IInvocationOperation. The overloads returning an array (such as MemberInfo.GetCustomAttributes(Type, bool) or the static Attribute.GetCustomAttributes methods) are wrapped in an implicit conversion when they are passed to Enumerable.Any or Enumerable.Count, so the cast returned null and the rule reported nothing. Those are the overloads available on netstandard2.0 and used by older code, so the rule was silent exactly where they are used. Unwrap the conversions first, as IsGetCustomAttributeInvocation already does. The code fixer needs the same unwrapping in its Any() and Count() paths, otherwise it would offer a fix that leaves the document unchanged on the newly reported patterns. The Length comparisons were already reported: the array is the direct instance of the Length property, without a conversion.
meziantou
deleted the
feature/ma0179-getcustomattributes-arrays-6e5cea
branch
September 6, 2026 04:16
This was referenced Sep 6, 2026
Closed
Closed
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
MA0179(Use Attribute.IsDefined instead of GetCustomAttribute(s)) did not report theGetCustomAttributesoverloads that return an array when they are followed by.Any()or.Count():Why
IsGetCustomAttributesInvocationdidoperation as IInvocationOperation. The array-returning overloads (MemberInfo.GetCustomAttributes(Type, bool), the staticAttribute.GetCustomAttributesmethods, …) produce an implicitIConversionOperation(Attribute[]/object[]toIEnumerable<T>) around the invocation when they are passed toEnumerable.AnyorEnumerable.Count, so the cast yieldednull. Its singular twinIsGetCustomAttributeInvocationalready calledUnwrapConversions()first.These are the overloads available on
netstandard2.0and the ones used by older code, so the rule was silent exactly where the older API is in use.The
.Lengthcomparisons were already reported: there the array is the direct instance of theLengthproperty reference, with no conversion in between.Changes
UseAttributeIsDefinedAnalyzer: unwrap the conversions inIsGetCustomAttributesInvocation.UseAttributeIsDefinedFixer: the same unwrapping in theAny()andCount()paths. Without it, the fixer would be offered on the newly reported patterns and return the document unchanged.docs/Rules/MA0179.md: added the array-returning overload to the examples.MemberInfo.GetCustomAttributes(Type, bool).Any(),Attribute.GetCustomAttributes(member, Type).Any(),.Count() > 0with a namedinheritargument, and a negative case checking that a user-definedGetCustomAttributesextension method followed by.Any()is still not reported (the conversion unwrapping must not weaken the check added in Do not report MA0179 on user-defined GetCustomAttribute(s) methods #1386).Verification
UseAttributeIsDefinedAnalyzerTests).dotnet run --project src/DocumentationGeneratorexits 0 with no further markdown changes.