Added Add AutoCtor Code Fix#477
Open
distantcam wants to merge 4 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new analyzer + code fix rule (ACTR008) to suggest and apply [AutoConstruct] on eligible types that have no explicit constructors, and refactors shared helper logic to support the new rule and existing code fixes.
Changes:
- Added ACTR008 analyzer (
AddAutoConstructAnalyzer) and code fix provider (AddAutoConstructCodeFixer) plus associated tests/examples. - Refactored shared helper utilities (renamed
ModelUtilities→Utilities) and extracted a reusableAutoCtorRewriter. - Updated/expanded ACTR007 test/example layout and renamed ACTR007 test classes for clarity.
Reviewed changes
Copilot reviewed 23 out of 90 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Tests/Tests.csproj | Updates test project includes/excludes for new ACTR007/ACTR008 example folders. |
| src/Tests/ACTR008_CodeFixTests.cs | Adds code-fix verification test harness for ACTR008. |
| src/Tests/ACTR008_CodeFixExamples/Standard.cs | New ACTR008 code-fix input example. |
| src/Tests/ACTR008_CodeFixExamples/Standard.cs.verified.txt | Expected ACTR008 code-fix output snapshot. |
| src/Tests/ACTR008_CodeAnalyzerTests.cs | Adds diagnostic verification test harness for ACTR008. |
| src/Tests/ACTR008_CodeAnalyzerExamples/Standard.cs | New ACTR008 analyzer input example. |
| src/Tests/ACTR008_CodeAnalyzerExamples/Standard.cs.verified.txt | Expected ACTR008 analyzer diagnostics snapshot. |
| src/Tests/ACTR008_CodeAnalyzerExamples/AlreadyAutoCtor.cs | New ACTR008 “no diagnostics” example. |
| src/Tests/ACTR008_CodeAnalyzerExamples/AlreadyAutoCtor.cs.verified.txt | Expected empty diagnostics snapshot for AlreadyAutoCtor. |
| src/Tests/ACTR007_CodeFixTests.cs | Renames test class + updates example directory name. |
| src/Tests/ACTR007_CodeFixExamples/StructExample.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/StructExample.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/StandardExample.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/StandardExample.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/Region.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/Region.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/RecordExample.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/RecordExample.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/NestedNoDeclaredVisibility.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/NestedNoDeclaredVisibility.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/Nested.cs | New ACTR007 code-fix input example. |
| src/Tests/ACTR007_CodeFixExamples/Nested.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/MultipleTypes.cs | New ACTR007 code-fix input example with multiple top-level types. |
| src/Tests/ACTR007_CodeFixExamples/MultipleTypes.cs.verified.txt | Expected ACTR007 code-fix output snapshot for MultipleTypes. |
| src/Tests/ACTR007_CodeFixExamples/MultipleNested.cs | New ACTR007 code-fix input example (nested types). |
| src/Tests/ACTR007_CodeFixExamples/MultipleNested.cs.verified.txt | Expected ACTR007 code-fix output snapshot for MultipleNested. |
| src/Tests/ACTR007_CodeFixExamples/MultipleMatchingCtors.cs | New ACTR007 code-fix input example (multiple matching ctors). |
| src/Tests/ACTR007_CodeFixExamples/MultipleMatchingCtors.cs.verified.txt | Expected ACTR007 code-fix output snapshot for MultipleMatchingCtors. |
| src/Tests/ACTR007_CodeFixExamples/MultipleConstructors.cs | New ACTR007 code-fix input example (multiple ctors). |
| src/Tests/ACTR007_CodeFixExamples/MultipleConstructors.cs.verified.txt | Expected ACTR007 code-fix output snapshot for MultipleConstructors. |
| src/Tests/ACTR007_CodeFixExamples/MixedFieldsAndProperties.cs | New ACTR007 code-fix input example (fields + properties). |
| src/Tests/ACTR007_CodeFixExamples/MixedFieldsAndProperties.cs.verified.txt | Expected ACTR007 code-fix output snapshot for MixedFieldsAndProperties. |
| src/Tests/ACTR007_CodeFixExamples/ExistingUsingGlobalAutoCtor.cs | New ACTR007 code-fix input example (global using). |
| src/Tests/ACTR007_CodeFixExamples/ExistingUsingGlobalAutoCtor.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/ExistingUsingAutoCtor.cs | New ACTR007 code-fix input example (normal using). |
| src/Tests/ACTR007_CodeFixExamples/ExistingUsingAutoCtor.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/ExistingAutoConstructAttribute.cs | New ACTR007 code-fix input example (existing AutoConstruct attribute forms). |
| src/Tests/ACTR007_CodeFixExamples/ExistingAutoConstructAttribute.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/ExistingAttribute.cs | New ACTR007 code-fix input example (other attributes present). |
| src/Tests/ACTR007_CodeFixExamples/ExistingAttribute.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeFixExamples/AlreadyPartial.cs | New ACTR007 code-fix input example (already partial). |
| src/Tests/ACTR007_CodeFixExamples/AlreadyPartial.cs.verified.txt | Expected ACTR007 code-fix output snapshot. |
| src/Tests/ACTR007_CodeAnalyzerTests.cs | Renames ACTR007 analyzer test class + updates example directory name. |
| src/Tests/ACTR007_CodeAnalyzerExamples/OnlyPropertyAssignments.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/OnlyPropertyAssignments.cs.verified.txt | Expected ACTR007 analyzer diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.cs.verified.txt | Expected ACTR007 analyzer diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/OnlyFieldAssignments.CheckDiagnostics.verified.txt | Adds/updates verify snapshot artifact for ACTR007 tests. |
| src/Tests/ACTR007_CodeAnalyzerExamples/NotAllFieldsAssigned.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/NotAllFieldsAssigned.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/NoDeclaredVisibility.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/NoDeclaredVisibility.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/MultipleConstructors.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/MultipleConstructors.cs.verified.txt | Expected ACTR007 analyzer diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/InternalConstructor.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/InternalConstructor.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/IgnoredFieldIsNotAssigned.cs | New ACTR007 analyzer input example (ignored member). |
| src/Tests/ACTR007_CodeAnalyzerExamples/IgnoredFieldIsNotAssigned.cs.verified.txt | Expected ACTR007 analyzer diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/HasBaseInitializer.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/HasBaseInitializer.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/FieldHasInitializer.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/FieldHasInitializer.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithNonAssignment.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithNonAssignment.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExtraParameter.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExtraParameter.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExplicitDefault.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithExplicitDefault.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithAttribute.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorWithAttribute.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorAssignsIgnoredField.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/ConstructorAssignsIgnoredField.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/BlankConstructor.cs | New ACTR007 analyzer input example. |
| src/Tests/ACTR007_CodeAnalyzerExamples/BlankConstructor.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyPartial.cs | New ACTR007 analyzer input example (partial). |
| src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyPartial.cs.verified.txt | Expected ACTR007 analyzer diagnostics snapshot. |
| src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyHasAutoConstruct.cs | New ACTR007 analyzer input example (already attribute). |
| src/Tests/ACTR007_CodeAnalyzerExamples/AlreadyHasAutoConstruct.cs.verified.txt | Expected empty diagnostics snapshot. |
| src/Shared/Models/TypeModel.cs | Switches from ModelUtilities to Utilities for member filtering. |
| src/Shared/Models/ParameterModel.cs | Switches service-key lookup to Utilities. |
| src/Shared/Models/MemberModel.cs | Updates static import to new Utilities name. |
| src/Shared/Helpers/Utilities.cs | Renames helper class and adds helper APIs for eligibility/attribute detection. |
| src/Shared/Constants/Diagnostics.cs | Adds ACTR008 diagnostic descriptor. |
| src/Shared/AutoConstructSourceGenerator/UseAutoConstructAnalyzer.cs | Refactors eligible-member lookup to use shared utilities. |
| src/Shared/AutoConstructSourceGenerator/AddAutoConstructAnalyzer.cs | Introduces ACTR008 analyzer implementation. |
| src/Shared/AnalyzerReleases.Unshipped.md | Registers ACTR008 as a new analyzer rule. |
| src/AutoCtor.CodeFixes/UseAutoConstructCodeFixer.cs | Refactors to use extracted AutoCtorRewriter. |
| src/AutoCtor.CodeFixes/AutoCtorRewriter.cs | Adds reusable syntax rewriter used by multiple code fixes. |
| src/AutoCtor.CodeFixes/AutoCtor.CodeFixes.csproj | Broadens shared constant includes and adds shared Utilities to code-fix assembly. |
| src/AutoCtor.CodeFixes/AddAutoConstructCodeFixer.cs | Introduces ACTR008 code fix provider implementation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+34
to
+54
| var typeSyntax = (TypeDeclarationSyntax)context.Node; | ||
|
|
||
| if (Utilities.HasAutoConstructAttribute(typeSyntax)) | ||
| return; | ||
|
|
||
| if (context.SemanticModel.GetDeclaredSymbol(typeSyntax, context.CancellationToken) is not INamedTypeSymbol type) | ||
| return; | ||
|
|
||
| // no constructors | ||
| if (type.InstanceConstructors.Any(c => !c.IsImplicitlyDeclared)) | ||
| return; | ||
|
|
||
| if (!Utilities.HasEligibleMember(type)) | ||
| return; | ||
|
|
||
| foreach (var location in type.Locations) | ||
| { | ||
| context.ReportDiagnostic(Diagnostic.Create( | ||
| Diagnostics.ACTR008_AddAutoConstruct, | ||
| location, | ||
| type.Name)); |
Comment on lines
23
to
28
| context.RegisterCompilationStartAction(static compilationContext => | ||
| { | ||
| var autoConstructType = compilationContext.Compilation | ||
| .GetTypeByMetadataName(AttributeNames.AutoConstruct); | ||
| if (autoConstructType is null) | ||
| return; | ||
|
|
||
| compilationContext.RegisterSyntaxNodeAction( | ||
| ctx => AnalyzeConstructor(ctx, autoConstructType), | ||
| AnalyzeConstructor, | ||
| SyntaxKind.ConstructorDeclaration); | ||
| }); |
Comment on lines
+1
to
+6
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using System.Collections.Immutable; | ||
| using System.Composition; |
Comment on lines
+1
to
+4
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using System.Collections.Immutable; |
b29508d to
48a7896
Compare
Comment on lines
+78
to
+82
| var newText = await newDocument.GetTextAsync(TestHelper.CancellationToken) | ||
| .ConfigureAwait(false); | ||
|
|
||
| var verifyText = string.Join(Environment.NewLine, newText); | ||
|
|
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.
No description provided.