Skip to content

Commit b4773b0

Browse files
authored
Fix IDE0008 in DllImportGenerator and Microsoft.Interop.SourceGeneration (#59885)
1 parent c4a164b commit b4773b0

28 files changed

+150
-180
lines changed

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Analyzers/ConvertToGeneratedDllImportAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Collections.Immutable;
56
using System.Diagnostics;
67
using System.Runtime.InteropServices;
@@ -101,7 +102,7 @@ private static bool RequiresMarshalling(IMethodSymbol method, DllImportData dllI
101102
}
102103

103104
Debug.Assert(dllImportAttr != null);
104-
foreach (var namedArg in dllImportAttr!.NamedArguments)
105+
foreach (KeyValuePair<string, TypedConstant> namedArg in dllImportAttr!.NamedArguments)
105106
{
106107
if (namedArg.Key != nameof(DllImportAttribute.PreserveSig))
107108
continue;

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Analyzers/ConvertToGeneratedDllImportFixer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Collections.Generic;
65
using System.Collections.Immutable;
76
using System.Runtime.InteropServices;
@@ -103,7 +102,7 @@ private async Task<Document> ConvertToGeneratedDllImport(
103102
var dllImportSyntax = (AttributeSyntax)dllImportAttr!.ApplicationSyntaxReference!.GetSyntax(cancellationToken);
104103

105104
// Create GeneratedDllImport attribute based on the DllImport attribute
106-
var generatedDllImportSyntax = GetGeneratedDllImportAttribute(
105+
SyntaxNode generatedDllImportSyntax = GetGeneratedDllImportAttribute(
107106
editor,
108107
generator,
109108
dllImportSyntax,
@@ -154,7 +153,7 @@ private async Task<Document> ConvertToGeneratedDllImport(
154153
}));
155154

156155
// Remove existing leading trivia - it will be on the GeneratedDllImport method
157-
var updatedDeclaration = methodSyntax.WithLeadingTrivia();
156+
MethodDeclarationSyntax updatedDeclaration = methodSyntax.WithLeadingTrivia();
158157

159158
// #endif
160159
updatedDeclaration = updatedDeclaration.WithTrailingTrivia(
@@ -184,7 +183,7 @@ private SyntaxNode GetGeneratedDllImportAttribute(
184183
{
185184
unmanagedCallConvAttributeMaybe = null;
186185
// Create GeneratedDllImport based on the DllImport attribute
187-
var generatedDllImportSyntax = generator.ReplaceNode(dllImportSyntax,
186+
SyntaxNode generatedDllImportSyntax = generator.ReplaceNode(dllImportSyntax,
188187
dllImportSyntax.Name,
189188
generator.TypeExpression(generatedDllImportAttrType));
190189

@@ -283,7 +282,7 @@ private bool TryCreateUnmanagedCallConvAttributeToEmit(
283282
private static bool TryGetAttribute(IMethodSymbol method, INamedTypeSymbol attributeType, out AttributeData? attr)
284283
{
285284
attr = default;
286-
foreach (var attrLocal in method.GetAttributes())
285+
foreach (AttributeData attrLocal in method.GetAttributes())
287286
{
288287
if (SymbolEqualityComparer.Default.Equals(attrLocal.AttributeClass, attributeType))
289288
{

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Analyzers/GeneratedDllImportAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
7474
{
7575
// Make sure declarations are marked partial. Technically, we can just check one
7676
// declaration, since Roslyn would error on inconsistent partial declarations.
77-
foreach (var reference in methodSymbol.DeclaringSyntaxReferences)
77+
foreach (SyntaxReference reference in methodSymbol.DeclaringSyntaxReferences)
7878
{
79-
var syntax = reference.GetSyntax(context.CancellationToken);
79+
SyntaxNode syntax = reference.GetSyntax(context.CancellationToken);
8080
if (syntax is MethodDeclarationSyntax methodSyntax && !methodSyntax.Modifiers.Any(SyntaxKind.PartialKeyword))
8181
{
8282
// Must be marked partial
@@ -87,9 +87,9 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
8787

8888
for (INamedTypeSymbol? typeSymbol = methodSymbol.ContainingType; typeSymbol is not null; typeSymbol = typeSymbol.ContainingType)
8989
{
90-
foreach (var reference in typeSymbol.DeclaringSyntaxReferences)
90+
foreach (SyntaxReference reference in typeSymbol.DeclaringSyntaxReferences)
9191
{
92-
var syntax = reference.GetSyntax(context.CancellationToken);
92+
SyntaxNode syntax = reference.GetSyntax(context.CancellationToken);
9393
if (syntax is TypeDeclarationSyntax typeSyntax && !typeSyntax.Modifiers.Any(SyntaxKind.PartialKeyword))
9494
{
9595
// Must be marked partial

src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/Analyzers/ManualTypeMarshallingAnalyzer.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Collections.Immutable;
65
using System.Linq;
76

@@ -195,12 +194,12 @@ public override void Initialize(AnalysisContext context)
195194

196195
private void PrepareForAnalysis(CompilationStartAnalysisContext context)
197196
{
198-
var generatedMarshallingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.GeneratedMarshallingAttribute);
199-
var blittableTypeAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.BlittableTypeAttribute);
200-
var nativeMarshallingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.NativeMarshallingAttribute);
201-
var marshalUsingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.MarshalUsingAttribute);
202-
var genericContiguousCollectionMarshallerAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.GenericContiguousCollectionMarshallerAttribute);
203-
var spanOfByte = context.Compilation.GetTypeByMetadataName(TypeNames.System_Span_Metadata)!.Construct(context.Compilation.GetSpecialType(SpecialType.System_Byte));
197+
INamedTypeSymbol? generatedMarshallingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.GeneratedMarshallingAttribute);
198+
INamedTypeSymbol? blittableTypeAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.BlittableTypeAttribute);
199+
INamedTypeSymbol? nativeMarshallingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.NativeMarshallingAttribute);
200+
INamedTypeSymbol? marshalUsingAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.MarshalUsingAttribute);
201+
INamedTypeSymbol? genericContiguousCollectionMarshallerAttribute = context.Compilation.GetTypeByMetadataName(TypeNames.GenericContiguousCollectionMarshallerAttribute);
202+
INamedTypeSymbol? spanOfByte = context.Compilation.GetTypeByMetadataName(TypeNames.System_Span_Metadata)!.Construct(context.Compilation.GetSpecialType(SpecialType.System_Byte));
204203

205204
if (generatedMarshallingAttribute is not null
206205
&& blittableTypeAttribute is not null
@@ -256,7 +255,7 @@ public void AnalyzeTypeDefinition(SymbolAnalysisContext context)
256255

257256
AttributeData? blittableTypeAttributeData = null;
258257
AttributeData? nativeMarshallingAttributeData = null;
259-
foreach (var attr in type.GetAttributes())
258+
foreach (AttributeData attr in type.GetAttributes())
260259
{
261260
if (SymbolEqualityComparer.Default.Equals(attr.AttributeClass, _generatedMarshallingAttribute))
262261
{
@@ -416,7 +415,7 @@ private void AnalyzeNativeMarshalerType(SymbolAnalysisContext context, ITypeSymb
416415

417416
bool hasConstructor = false;
418417
bool hasStackallocConstructor = false;
419-
foreach (var ctor in marshalerType.Constructors)
418+
foreach (IMethodSymbol ctor in marshalerType.Constructors)
420419
{
421420
if (ctor.IsStatic)
422421
{

0 commit comments

Comments
 (0)