fix: Add unit tests and improve analyzers for handling generated code and diagnostics#399
fix: Add unit tests and improve analyzers for handling generated code and diagnostics#399BenjaminMichaelis wants to merge 3 commits intomainfrom
Conversation
IntelliTect.Analyzer/IntelliTect.Analyzer/Analyzers/FavorDirectoryEnumerationCalls.cs
Fixed
Show fixed
Hide fixed
There was a problem hiding this comment.
Pull request overview
This pull request improves analyzer robustness and test coverage by addressing issues with generated code detection, diagnostic URL generation, and analyzer behavior. The changes fix several bugs where analyzers would incorrectly report diagnostics on generated code or fail to handle edge cases properly.
Changes:
- Improved generated code detection in naming analyzers using proper type comparison instead of name-only checks
- Updated UnusedLocalVariable and AttributesOnSeparateLines analyzers to skip generated code
- Refactored FavorDirectoryEnumerationCalls to fix InvalidCastException with generic methods and handle fully-qualified Directory references
- Attempted performance optimization in DiagnosticUrlBuilder using GeneratedRegex (but incompatible with target framework)
- Added comprehensive unit tests documenting and verifying fixes for edge cases
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| DiagnosticUrlBuilder.cs | Attempts to optimize regex usage with GeneratedRegex, but incompatible with netstandard2.0 target |
| UnusedLocalVariable.cs | Changed to skip generated code and use DiagnosticUrlBuilder for HelpLinkUri |
| NamingPropertyPascal.cs | Improved GeneratedCodeAttribute detection using proper type comparison |
| NamingMethodPascal.cs | Improved GeneratedCodeAttribute detection using proper type comparison |
| NamingFieldPascalUnderscore.cs | Improved GeneratedCodeAttribute detection using proper type comparison |
| FavorDirectoryEnumerationCalls.cs | Refactored to fix InvalidCastException and handle fully-qualified names |
| AttributesOnSeparateLines.cs | Changed to skip generated code |
| UnusedLocalVariableTests.cs | Added tests for generated code skipping and HelpLinkUri correctness |
| NamingFieldPascalUnderscoreTests.cs | Added tests verifying proper GeneratedCodeAttribute detection |
| FavorEnumeratorDirectoryCallsTests.cs | Added regression tests for generic methods, unresolved symbols, and fully-qualified names |
| DiagnosticUriBuilderTests.cs | Added performance and whitespace handling tests |
| DateTimeConversionTests.cs | Added test documenting analyzer crash with unresolvable types (test will fail) |
| AttributesOnSeparateLinesTests.cs | Added test for generated code skipping |
| AsyncVoidTests.cs | Added test documenting null safety (existing behavior) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DiagnosticUriBuilderTests.cs
Outdated
Show resolved
Hide resolved
IntelliTect.Analyzer/IntelliTect.Analyzer/DiagnosticUrlBuilder.cs
Outdated
Show resolved
Hide resolved
IntelliTect.Analyzer/IntelliTect.Analyzer.Test/DateTimeConversionTests.cs
Outdated
Show resolved
Hide resolved
5dd93f5 to
1a54f21
Compare
| // memberAccess.ChildNodes().Cast<IdentifierNameSyntax>() will throw | ||
| // if any child node is not IdentifierNameSyntax (e.g. GenericNameSyntax). | ||
| // This test uses a generic method call on a class named Directory. | ||
| string source = @" |
There was a problem hiding this comment.
nit: Is there a reason we can't use multi-line string literals for these tests?
| INamedTypeSymbol generatedCodeAttribute = context.Compilation | ||
| .GetTypeByMetadataName("System.CodeDom.Compiler.GeneratedCodeAttribute"); | ||
| ImmutableArray<AttributeData> attributes = namedTypeSymbol.GetAttributes().AddRange(namedTypeSymbol.ContainingType.GetAttributes()); | ||
| if (attributes.Any(attribute => attribute.AttributeClass?.Name == nameof(System.CodeDom.Compiler.GeneratedCodeAttribute))) | ||
| if (generatedCodeAttribute is not null && | ||
| attributes.Any(attribute => SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, generatedCodeAttribute))) |
There was a problem hiding this comment.
This check seems like something that would make for a good extension method to saves repeating it.
| public static class DiagnosticUrlBuilder | ||
| { | ||
| private const string BaseUrl = "https://github.com/IntelliTect/CodingGuidelines"; | ||
| private static readonly Regex _HyphenateRegex = new(@"\s+", RegexOptions.Compiled); |
There was a problem hiding this comment.
Are we able to use the source generator for Regex here?
There was a problem hiding this comment.
No - because we are targeting netstandard 2.0 - unless there is a way to shim that back that I don't know of
No description provided.