Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge master to release/6.0.1xx-preview1 #4611

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ public class CSharpDiagnosticAnalyzerApiUsageAnalyzer : DiagnosticAnalyzerApiUsa
{
protected override bool IsNamedTypeDeclarationBlock(SyntaxNode syntax)
{
switch (syntax.Kind())
return syntax.Kind() switch
{
case SyntaxKind.ClassDeclaration:
SyntaxKind.ClassDeclaration
or SyntaxKind.StructDeclaration
or SyntaxKind.EnumDeclaration
#if CODEANALYSIS_V3_OR_BETTER
case SyntaxKind.RecordDeclaration:
or SyntaxKind.RecordDeclaration:
#endif
case SyntaxKind.StructDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.InterfaceDeclaration:
return true;

default:
return false;
}
or SyntaxKind.InterfaceDeclaration => true,
_ => false,
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -710,21 +710,13 @@ private static DiagnosticResult GetBasicExpectedDiagnostic(int line, int column,

private static string[] GetExpectedArguments(string parameterName, StartActionKind kind)
{
string arg2;
switch (kind)
var arg2 = kind switch
{
case StartActionKind.CompilationStartAction:
arg2 = "Initialize";
break;

case StartActionKind.CodeBlockStartAction:
case StartActionKind.OperationBlockStartAction:
arg2 = "Initialize, CompilationStartAction";
break;

default:
throw new ArgumentException("Unsupported action kind", nameof(kind));
}
StartActionKind.CompilationStartAction => "Initialize",
StartActionKind.CodeBlockStartAction
or StartActionKind.OperationBlockStartAction => "Initialize, CompilationStartAction",
_ => throw new ArgumentException("Unsupported action kind", nameof(kind)),
};

return new[] { parameterName, arg2 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,13 @@ private static bool IsEqualityOperator(IMethodSymbol method, Compilation compila
{
if (!method.IsStatic || !method.IsPublic())
return false;
switch (method.Name)

return method.Name switch
{
case WellKnownMemberNames.EqualityOperatorName:
case WellKnownMemberNames.InequalityOperatorName:
return true;
default:
return false;
}
WellKnownMemberNames.EqualityOperatorName
or WellKnownMemberNames.InequalityOperatorName => true,
_ => false,
};
}

private static bool IsImplicitCastOperator(IMethodSymbol method, Compilation compilation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,21 +671,14 @@ private bool ValidateOperations(ImmutableArray<IOperation> operations)

private bool ValidateOperation(IOperation operation)
{
switch (operation.Kind)
{
case OperationKind.Empty:
case OperationKind.Labeled:
return true;
case OperationKind.Block:
return ValidateOperations(((IBlockOperation)operation).Operations);
case OperationKind.ExpressionStatement:
return ValidateExpression((IExpressionStatementOperation)operation);
case OperationKind.Try:
return ValidateTryOperation((ITryOperation)operation);
default:
// Ignore operation roots with no IOperation API support (OperationKind.None)
return operation.IsOperationNoneRoot();
}
return operation.Kind switch
{
OperationKind.Empty or OperationKind.Labeled => true,
OperationKind.Block => ValidateOperations(((IBlockOperation)operation).Operations),
OperationKind.ExpressionStatement => ValidateExpression((IExpressionStatementOperation)operation),
OperationKind.Try => ValidateTryOperation((ITryOperation)operation),
_ => operation.IsOperationNoneRoot(),// Ignore operation roots with no IOperation API support (OperationKind.None)
};
}

private bool ValidateExpression(IExpressionStatementOperation expressionStatement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,11 @@ private static Diagnostic CreateDiagnostic(DiagnosticDescriptor descriptor, Loca

internal static bool IsPropertyExpected(string operatorName)
{
switch (operatorName)
return operatorName switch
{
case OpTrueText:
case OpFalseText:
return true;
default:
return false;
}
OpTrueText or OpFalseText => true,
_ => false,
};
}

internal static ExpectedAlternateMethodGroup? GetExpectedAlternateMethodGroup(string operatorName, ITypeSymbol returnType, ITypeSymbol? parameterType)
Expand All @@ -192,71 +189,47 @@ internal static bool IsPropertyExpected(string operatorName)

// the most common case; create a static method with the already specified types
static ExpectedAlternateMethodGroup createSingle(string methodName) => new(methodName);
switch (operatorName)
return operatorName switch
{
case "op_Addition":
case "op_AdditonAssignment":
return createSingle("Add");
case "op_BitwiseAnd":
case "op_BitwiseAndAssignment":
return createSingle("BitwiseAnd");
case "op_BitwiseOr":
case "op_BitwiseOrAssignment":
return createSingle("BitwiseOr");
case "op_Decrement":
return createSingle("Decrement");
case "op_Division":
case "op_DivisionAssignment":
return createSingle("Divide");
case "op_Equality":
case "op_Inequality":
return createSingle("Equals");
case "op_ExclusiveOr":
case "op_ExclusiveOrAssignment":
return createSingle("Xor");
case "op_GreaterThan":
case "op_GreaterThanOrEqual":
case "op_LessThan":
case "op_LessThanOrEqual":
return new ExpectedAlternateMethodGroup(alternateMethod1: "CompareTo", alternateMethod2: "Compare");
case "op_Increment":
return createSingle("Increment");
case "op_LeftShift":
case "op_LeftShiftAssignment":
return createSingle("LeftShift");
case "op_LogicalAnd":
return createSingle("LogicalAnd");
case "op_LogicalOr":
return createSingle("LogicalOr");
case "op_LogicalNot":
return createSingle("LogicalNot");
case "op_Modulus":
case "op_ModulusAssignment":
return new ExpectedAlternateMethodGroup(alternateMethod1: "Mod", alternateMethod2: "Remainder");
case "op_MultiplicationAssignment":
case "op_Multiply":
return createSingle("Multiply");
case "op_OnesComplement":
return createSingle("OnesComplement");
case "op_RightShift":
case "op_RightShiftAssignment":
case "op_SignedRightShift":
case "op_UnsignedRightShift":
case "op_UnsignedRightShiftAssignment":
return createSingle("RightShift");
case "op_Subtraction":
case "op_SubtractionAssignment":
return createSingle("Subtract");
case "op_UnaryNegation":
return createSingle("Negate");
case "op_UnaryPlus":
return createSingle("Plus");
case "op_Implicit":
case "op_Explicit":
return new ExpectedAlternateMethodGroup(alternateMethod1: $"To{GetTypeName(returnType)}", alternateMethod2: parameterType != null ? $"From{GetTypeName(parameterType)}" : null);
default:
return null;
}
"op_Addition"
or "op_AdditonAssignment" => createSingle("Add"),
"op_BitwiseAnd"
or "op_BitwiseAndAssignment" => createSingle("BitwiseAnd"),
"op_BitwiseOr"
or "op_BitwiseOrAssignment" => createSingle("BitwiseOr"),
"op_Decrement" => createSingle("Decrement"),
"op_Division"
or "op_DivisionAssignment" => createSingle("Divide"),
"op_Equality"
or "op_Inequality" => createSingle("Equals"),
"op_ExclusiveOr"
or "op_ExclusiveOrAssignment" => createSingle("Xor"),
"op_GreaterThan"
or "op_GreaterThanOrEqual" or "op_LessThan" or "op_LessThanOrEqual" => new ExpectedAlternateMethodGroup(alternateMethod1: "CompareTo", alternateMethod2: "Compare"),
"op_Increment" => createSingle("Increment"),
"op_LeftShift"
or "op_LeftShiftAssignment" => createSingle("LeftShift"),
"op_LogicalAnd" => createSingle("LogicalAnd"),
"op_LogicalOr" => createSingle("LogicalOr"),
"op_LogicalNot" => createSingle("LogicalNot"),
"op_Modulus"
or "op_ModulusAssignment" => new ExpectedAlternateMethodGroup(alternateMethod1: "Mod", alternateMethod2: "Remainder"),
"op_MultiplicationAssignment"
or "op_Multiply" => createSingle("Multiply"),
"op_OnesComplement" => createSingle("OnesComplement"),
"op_RightShift"
or "op_RightShiftAssignment"
or "op_SignedRightShift"
or "op_UnsignedRightShift"
or "op_UnsignedRightShiftAssignment" => createSingle("RightShift"),
"op_Subtraction"
or "op_SubtractionAssignment" => createSingle("Subtract"),
"op_UnaryNegation" => createSingle("Negate"),
"op_UnaryPlus" => createSingle("Plus"),
"op_Implicit"
or "op_Explicit" => new ExpectedAlternateMethodGroup(alternateMethod1: $"To{GetTypeName(returnType)}", alternateMethod2: parameterType != null ? $"From{GetTypeName(parameterType)}" : null),
_ => null,
};

static string GetTypeName(ITypeSymbol typeSymbol)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,45 +251,22 @@ void analyzeMetricsData(CodeAnalysisMetricData codeAnalysisMetricData)

static bool isApplicableByDefault(string ruleId, SymbolKind symbolKind)
{
switch (ruleId)
return ruleId switch
{
case CA1501RuleId:
return symbolKind == SymbolKind.NamedType;

case CA1502RuleId:
return symbolKind == SymbolKind.Method;

case CA1505RuleId:
switch (symbolKind)
{
case SymbolKind.NamedType:
case SymbolKind.Method:
case SymbolKind.Field:
case SymbolKind.Property:
case SymbolKind.Event:
return true;

default:
return false;
}

case CA1506RuleId:
switch (symbolKind)
{
case SymbolKind.NamedType:
case SymbolKind.Method:
case SymbolKind.Field:
case SymbolKind.Property:
case SymbolKind.Event:
return true;

default:
return false;
}

default:
throw new NotImplementedException();
}
CA1501RuleId => symbolKind == SymbolKind.NamedType,
CA1502RuleId => symbolKind == SymbolKind.Method,
CA1505RuleId => symbolKind switch
{
SymbolKind.NamedType or SymbolKind.Method or SymbolKind.Field or SymbolKind.Property or SymbolKind.Event => true,
_ => false,
},
CA1506RuleId => symbolKind switch
{
SymbolKind.NamedType or SymbolKind.Method or SymbolKind.Field or SymbolKind.Property or SymbolKind.Event => true,
_ => false,
},
_ => throw new NotImplementedException(),
};
}

static uint? getDefaultThreshold(string ruleId, SymbolKind symbolKind)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,24 @@ private static bool TypeHasWeakIdentity(ITypeSymbol type, Compilation compilatio

public static bool IsPrimitiveType(ITypeSymbol type)
{
switch (type.SpecialType)
return type.SpecialType switch
{
case SpecialType.System_Boolean:
case SpecialType.System_Byte:
case SpecialType.System_Char:
case SpecialType.System_Double:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_IntPtr:
case SpecialType.System_UIntPtr:
case SpecialType.System_SByte:
case SpecialType.System_Single:
return true;
default:
return false;
}
SpecialType.System_Boolean
or SpecialType.System_Byte
or SpecialType.System_Char
or SpecialType.System_Double
or SpecialType.System_Int16
or SpecialType.System_Int32
or SpecialType.System_Int64
or SpecialType.System_UInt16
or SpecialType.System_UInt32
or SpecialType.System_UInt64
or SpecialType.System_IntPtr
or SpecialType.System_UIntPtr
or SpecialType.System_SByte
or SpecialType.System_Single => true,
_ => false,
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,12 @@ private static bool IsSingleParameterLinqMethod(IMethodSymbol methodSymbol, ITyp

private static bool IsPossibleLinqInvocation(IInvocationOperation invocation, bool excludeOrDefaultMethods)
{
switch (invocation.TargetMethod.Name)
return invocation.TargetMethod.Name switch
{
case "Last":
case "First":
case "Count":
return true;
case "LastOrDefault":
case "FirstOrDefault":
return !excludeOrDefaultMethods;
default:
return false;
}
"Last" or "First" or "Count" => true,
"LastOrDefault" or "FirstOrDefault" => !excludeOrDefaultMethods,
_ => false,
};
}
}
}
Loading