Skip to content

Commit d843809

Browse files
Check length of TypeArguments before accessing via index (#60742)
* Check length of TypeArguments before accessing via index * Removed unecessary using in associated test * Fixed a spelling mistake * Added comments around the additional check
1 parent 4fb5a14 commit d843809

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteHandlers/DisallowNonParsableComplexTypesOnParameters.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static void DisallowNonParsableComplexTypesOnParameters(
3030
continue;
3131
}
3232

33-
var parameterTypeSymbol = ResovleParameterTypeSymbol(handlerDelegateParameter);
33+
var parameterTypeSymbol = ResolveParameterTypeSymbol(handlerDelegateParameter);
3434

3535
// If this is null it means we aren't working with a named type symbol.
3636
if (parameterTypeSymbol == null)
@@ -113,7 +113,7 @@ static bool ReportFromAttributeDiagnostic(OperationAnalysisContext context, Well
113113
return false;
114114
}
115115

116-
static INamedTypeSymbol? ResovleParameterTypeSymbol(IParameterSymbol parameterSymbol)
116+
static INamedTypeSymbol? ResolveParameterTypeSymbol(IParameterSymbol parameterSymbol)
117117
{
118118
INamedTypeSymbol? parameterTypeSymbol = null;
119119

@@ -127,8 +127,10 @@ static bool ReportFromAttributeDiagnostic(OperationAnalysisContext context, Well
127127
parameterTypeSymbol = namedTypeSymbol;
128128
}
129129

130-
// If it is nullable, unwrap it.
131-
if (parameterTypeSymbol!.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T)
130+
// If it is nullable and we have type arguments, unwrap it.
131+
// The length check aims to alleviate AD0001 warnings when referencing methods in external class libraries that contain parameters
132+
if (parameterTypeSymbol!.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T &&
133+
parameterTypeSymbol.TypeArguments.Length > 0)
132134
{
133135
parameterTypeSymbol = parameterTypeSymbol.TypeArguments[0] as INamedTypeSymbol;
134136
}

src/Framework/AspNetCoreAnalyzers/test/RouteHandlers/DisallowNonParsableComplexTypesOnParametersTest.cs

Lines changed: 0 additions & 1 deletion
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.Security.Policy;
54
using Microsoft.CodeAnalysis.Testing;
65
using VerifyCS = Microsoft.AspNetCore.Analyzers.Verifiers.CSharpAnalyzerVerifier<Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer>;
76

0 commit comments

Comments
 (0)