Skip to content

Commit b43c3aa

Browse files
authored
Merge pull request #6874 from Youssef1313/direct-cast
Use direct cast instead of type checking when type is known
2 parents 0139464 + a15b841 commit b43c3aa

File tree

7 files changed

+33
-32
lines changed

7 files changed

+33
-32
lines changed

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/EnumShouldNotHaveDuplicatedValues.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public sealed override void Initialize(AnalysisContext context)
5454

5555
void visitEnumSymbol(SymbolStartAnalysisContext context)
5656
{
57-
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Enum } enumSymbol)
57+
var enumSymbol = (INamedTypeSymbol)context.Symbol;
58+
if (enumSymbol.TypeKind != TypeKind.Enum)
5859
{
5960
return;
6061
}

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/EquatableAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ private static void OnCompilationStart(CompilationStartAnalysisContext context)
6262

6363
private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbol equatableType)
6464
{
65-
if (context.Symbol is not INamedTypeSymbol namedType
66-
|| (namedType.TypeKind != TypeKind.Struct && namedType.TypeKind != TypeKind.Class)
65+
var namedType = (INamedTypeSymbol)context.Symbol;
66+
if ((namedType.TypeKind != TypeKind.Struct && namedType.TypeKind != TypeKind.Class)
6767
|| (namedType.TypeKind == TypeKind.Struct && namedType.IsRefLikeType))
6868
{
6969
return;

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/ImplementIDisposableCorrectly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ public void Initialize(CompilationStartAnalysisContext context)
182182

183183
private void AnalyzeNamedTypeSymbol(SymbolAnalysisContext context)
184184
{
185+
var type = (INamedTypeSymbol)context.Symbol;
185186
// Note all the descriptors/rules for this analyzer have the same ID and category and hence
186187
// will always have identical configured visibility.
187-
if (context.Symbol is INamedTypeSymbol type &&
188-
type.TypeKind == TypeKind.Class &&
188+
if (type.TypeKind == TypeKind.Class &&
189189
context.Options.MatchesConfiguredVisibility(IDisposableReimplementationRule, type, context.Compilation))
190190
{
191191
bool implementsDisposableInBaseType = ImplementsDisposableInBaseType(type);

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/QualityGuidelines/MarkMembersAsStatic.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)
103103

104104
context.RegisterOperationAction(context =>
105105
{
106-
if (context.Operation is IInstanceReferenceOperation { ReferenceKind: InstanceReferenceKind.ContainingTypeInstance }
106+
var operation = (IInstanceReferenceOperation)context.Operation;
107+
if (operation.ReferenceKind == InstanceReferenceKind.ContainingTypeInstance
107108
&& (context.Operation.Parent is not IInvocationOperation invocation || !invocation.TargetMethod.Equals(methodSymbol, SymbolEqualityComparer.Default)))
108109
{
109110
isInstanceReferenced = true;
@@ -121,7 +122,8 @@ void OnOperationBlockStart(OperationBlockStartAnalysisContext context)
121122

122123
context.RegisterOperationAction(context =>
123124
{
124-
if (context.Operation is IParameterReferenceOperation { Parameter.ContainingSymbol: IMethodSymbol { MethodKind: MethodKind.Constructor } })
125+
var operation = (IParameterReferenceOperation)context.Operation;
126+
if (operation.Parameter.ContainingSymbol is IMethodSymbol { MethodKind: MethodKind.Constructor })
125127
{
126128
// we're referencing a parameter not from our actual method, but from a type constructor.
127129
// This must be a primary constructor scenario, and we're capturing the parameter here.

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,26 @@ public override void Initialize(AnalysisContext context)
5555

5656
context.RegisterSymbolAction(context =>
5757
{
58-
if (context.Symbol is IMethodSymbol method)
58+
var method = (IMethodSymbol)context.Symbol;
59+
60+
// Eliminate methods that would fail the CS8814, CS8815, and CS8816 checks
61+
// for what can have [ModuleInitializer] applied
62+
if (method.GetResultantVisibility() == SymbolVisibility.Private ||
63+
method.Parameters.Length > 0 ||
64+
method.IsGenericMethod ||
65+
method.ContainingType.IsGenericType ||
66+
!method.IsStatic ||
67+
!method.ReturnsVoid)
5968
{
60-
// Eliminate methods that would fail the CS8814, CS8815, and CS8816 checks
61-
// for what can have [ModuleInitializer] applied
62-
if (method.GetResultantVisibility() == SymbolVisibility.Private ||
63-
method.Parameters.Length > 0 ||
64-
method.IsGenericMethod ||
65-
method.ContainingType.IsGenericType ||
66-
!method.IsStatic ||
67-
!method.ReturnsVoid)
68-
{
69-
return;
70-
}
69+
return;
70+
}
7171

72-
AttributeData? initializerAttribute = context.Symbol.GetAttribute(moduleInitializerAttribute);
73-
SyntaxReference? attributeReference = initializerAttribute?.ApplicationSyntaxReference;
72+
AttributeData? initializerAttribute = context.Symbol.GetAttribute(moduleInitializerAttribute);
73+
SyntaxReference? attributeReference = initializerAttribute?.ApplicationSyntaxReference;
7474

75-
if (attributeReference is not null)
76-
{
77-
context.ReportDiagnostic(attributeReference.GetSyntax(context.CancellationToken).CreateDiagnostic(Rule));
78-
}
75+
if (attributeReference is not null)
76+
{
77+
context.ReportDiagnostic(attributeReference.GetSyntax(context.CancellationToken).CreateDiagnostic(Rule));
7978
}
8079
},
8180
SymbolKind.Method);

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Usage/ImplementGenericMathInterfacesCorrectly.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ public override void Initialize(AnalysisContext context)
5151

5252
context.RegisterSymbolAction(context =>
5353
{
54-
if (context.Symbol is INamedTypeSymbol ntSymbol)
55-
{
56-
AnalyzeSymbol(context, ntSymbol, iParsableInterface.ContainingNamespace, iNumberInterface.ContainingNamespace);
57-
}
54+
var symbol = (INamedTypeSymbol)context.Symbol;
55+
AnalyzeSymbol(context, symbol, iParsableInterface.ContainingNamespace, iNumberInterface.ContainingNamespace);
5856
}, SymbolKind.NamedType);
5957
});
6058
}

src/NetAnalyzers/Core/Microsoft.NetFramework.Analyzers/MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
1+
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

33
using System.Collections.Immutable;
44
using Analyzer.Utilities;
@@ -96,9 +96,10 @@ public override void Initialize(AnalysisContext context)
9696
compilationStartContext.RegisterSymbolAction(
9797
(SymbolAnalysisContext symbolContext) =>
9898
{
99+
var methodSymbol = (IMethodSymbol)symbolContext.Symbol;
100+
99101
// TODO enhancements: Consider looking at IAsyncResult-based action methods.
100-
if (symbolContext.Symbol is not IMethodSymbol methodSymbol
101-
|| methodSymbol.MethodKind != MethodKind.Ordinary
102+
if (methodSymbol.MethodKind != MethodKind.Ordinary
102103
|| methodSymbol.IsStatic
103104
|| !methodSymbol.IsPublic()
104105
|| !(methodSymbol.ReturnType.Inherits(actionResultSymbol) // FxCop implementation only looked at ActionResult-derived return types.

0 commit comments

Comments
 (0)