Skip to content

Commit

Permalink
Fixer for MA0008 adds using directive automatically (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou authored Feb 20, 2023
1 parent 1e4741c commit f5418d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Meziantou.Analyzer/Rules/UseStructLayoutAttributeFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Simplification;

namespace Meziantou.Analyzer.Rules;

Expand Down Expand Up @@ -55,12 +56,12 @@ private static async Task<Document> Refactor(Document document, SyntaxNode nodeT
return document;

var attribute = editor.Generator.Attribute(
generator.TypeExpression(structLayoutAttribute, addImport: true),
generator.TypeExpression(structLayoutAttribute).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation),
new[]
{
generator.AttributeArgument(
generator.MemberAccessExpression(
generator.TypeExpression(layoutKindEnum, addImport: true),
generator.TypeExpression(layoutKindEnum).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation),
layoutKind.ToString())),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public async Task MissingAttribute_ShouldReportDiagnostic()
int a;
int b;
}";
const string CodeFix = @"[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Auto)]
const string CodeFix = @"using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Auto)]
struct TypeName
{
int a;
Expand Down Expand Up @@ -132,7 +134,9 @@ public async Task RecordStruct()
{
const string SourceCode = @"record struct [||]TypeName(int A, int B);";

const string CodeFix = @"[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Auto)]
const string CodeFix = @"using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Auto)]
record struct TypeName(int A, int B);";

await CreateProjectBuilder()
Expand Down

0 comments on commit f5418d3

Please sign in to comment.