Add MA0179: Detect inefficient attribute existence checks#939
Add MA0179: Detect inefficient attribute existence checks#939
Conversation
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
…n negation logic Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
…cription Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
|
@copilot Can you also detect |
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Done! Added support for detecting
Commit: a491f10 |
|
@copilot Can you add a test for |
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Done!
|
| { | ||
| var operation = (IInvocationOperation)context.Operation; | ||
|
|
||
| if (operation.TargetMethod.Name != "Any") |
There was a problem hiding this comment.
@copilot Can you be more specific and use a IMethodSymbol to actually compare it or check if the method is defined in System.Linq.Enumerable
There was a problem hiding this comment.
Done! Changed to use specific IMethodSymbol comparison via DocumentationCommentId.GetFirstSymbolForDeclarationId for both Any and Count methods. This ensures we only detect the exact System.Linq.Enumerable.Any<T>(IEnumerable<T>) and System.Linq.Enumerable.Count<T>(IEnumerable<T>) methods, making the detection more precise and robust.
| { | ||
| void Test(MemberInfo member) | ||
| { | ||
| _ = [||]member.GetCustomAttribute<ObsoleteAttribute>() != null; |
There was a problem hiding this comment.
@copilot Use the correct scope here and in every tests
| _ = [||]member.GetCustomAttribute<ObsoleteAttribute>() != null; | |
| _ = [|member.GetCustomAttribute<ObsoleteAttribute>() != null|]; |
There was a problem hiding this comment.
Fixed! Changed all test diagnostic markers from [||]expression to [|expression|] to properly indicate the diagnostic span scope.
Commit: 6ea0409
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Summary
Successfully implemented MA0179: Use Attribute.IsDefined instead of GetCustomAttribute(s)
Completed Tasks
Latest Changes
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.