Match the annotation attributes by name in MA0060, MA0124 and MA0139 - #1407
Merged
Merged
Conversation
The annotation attributes can be defined in multiple assemblies, as the Meziantou.Analyzer.Annotations package documents that their source can be copied into a project, so AnnotationAttributes identifies them by their full name only. Two rules still resolved them with GetBestTypeByMetadataName: - MA0060 used symbol equality for the attribute on a return value or an out parameter, while the assembly level form already went through AnnotationAttributes, so the two documented usages could disagree. - MA0124/MA0139 had no name based matcher at all for StructuredLogFieldAttribute. GetBestTypeByMetadataName ignores a type that is internal in a referenced assembly, so MA0060 reported nothing on the members a referenced library annotated with its own copy of the attribute. Add AnnotationAttributes.IsStructuredLogFieldAttributeSymbol and route both rules through AnnotationAttributes.
meziantou
enabled auto-merge (squash)
September 6, 2026 04:21
This was referenced Sep 6, 2026
Closed
This was referenced Sep 24, 2026
Open
Open
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
AnnotationAttributesstates that the annotation attributes "can be defined in multiple assemblies, so it's identified by its full name only", and theMeziantou.Analyzer.AnnotationsREADME documents that users may copy the attribute source into their own project,publicorinternal. Two rules did not follow that policy and still resolved the attribute withGetBestTypeByMetadataName:outparameter by symbol equality, while the assembly-level form already went throughAnnotationAttributes— two matching strategies for one attribute within a single file.StructuredLogFieldAttributeby metadata name; it was the only one of the nine shipped attributes with no name-based matcher.Changes:
AnnotationAttributes.IsStructuredLogFieldAttributeSymbol.DoNotIgnoreReturnValueAnalyzerdrops theDoNotIgnoreAttributeSymbolfield and itsis nullguards; the out-parameter and return-value paths share aFindDoNotIgnoreAttributehelper backed byAnnotationAttributes.IsDoNotIgnoreAttributeSymbol, so all three forms of MA0060 now match identically.LoggerParameterTypeAnalyzerdropsStructuredLogFieldAttributeSymboland matches by name.AnalyzeArgumentbecamestatic— CA1822 fires once it no longer touches instance state.Why it was a real bug, not only a consistency issue
GetBestTypeByMetadataNamereturns a type declared in the compilation's own source regardless of accessibility, so copying the attribute into your own project always worked. The case that actually diverged is a referenced assembly that copied the attribute asinternal: it is invisible toGetBestTypeByMetadataName, so MA0060 silently reported nothing on that library's annotated members.Tests
Four new tests in
DoNotIgnoreReturnValueAnalyzerTestsand one inLoggerParameterTypeAnalyzerTests, all annotating with a hand-copiedinternalattribute instead of the package one.Note for the reviewer: three of the four in-project tests pass against the old analyzer too, for the reason above — they are kept as coverage of the README-endorsed usage. The test that proves the fix is
Attribute_ReturnValue_NotUsed_AttributeDefinedInAReferencedProject, which puts theinternalattribute in an additional referenced project; it fails against the pre-fix analyzer and passes after.Verification
dotnet build— clean, 0 warnings.dotnet test --max-parallel-test-modules 2— 19,283 passed, 0 failed, across all five Roslyn versions.dotnet run --project src/DocumentationGenerator— exit 0, no markdown changes.Nothing under
src/Meziantou.Analyzer.Annotationschanged, so no README update or version bump was needed.