private static void Analyze(SyntaxNodeAnalysisContext context, SwitchExpressionSyntaxWrapper switchExpression)
        {
            var arms = switchExpression.Arms;

            if (arms.Count < 2)
            {
                return;
            }
            var firstArm = arms[0];

            if (switchExpression.HasDiscardPattern() &&
                arms.Skip(1).All(arm => SyntaxFactory.AreEquivalent(arm.Expression, firstArm.Expression)))
            {
                context.ReportDiagnostic(Diagnostic.Create(rule, switchExpression.SwitchKeyword.GetLocation(), StatementsMessage));
            }
        }
            public override void Visit(SyntaxNode node)
            {
                if (node.IsKind(SyntaxKindEx.LocalFunctionStatement))
                {
                    State.VisitWithNesting(node, base.Visit);
                }
                else if (SwitchExpressionSyntaxWrapper.IsInstance(node))
                {
                    var switchExpression = (SwitchExpressionSyntaxWrapper)node;

                    State.IncreaseComplexityByNestingPlusOne(switchExpression.SwitchKeyword);
                    State.VisitWithNesting(node, base.Visit);
                }
                else
                {
                    base.Visit(node);
                }
            }
 private static SwitchExpressionType EvaluateType(SwitchExpressionSyntaxWrapper switchExpression)
 {
     var numberOfArms = switchExpression.Arms.Count;
     if (numberOfArms > 2)
     {
         return SwitchExpressionType.ManyReturnValues;
     }
     var hasDiscardValue = switchExpression.HasDiscardPattern();
     if (numberOfArms == 2)
     {
         return hasDiscardValue ? SwitchExpressionType.TwoReturnValues : SwitchExpressionType.ManyReturnValues;
     }
     if (numberOfArms == 1)
     {
         return hasDiscardValue ? SwitchExpressionType.SingleReturnValue : SwitchExpressionType.TwoReturnValues;
     }
     return SwitchExpressionType.SingleReturnValue;
 }
Exemplo n.º 4
0
 public void TestIsInstance()
 {
     Assert.False(SwitchExpressionSyntaxWrapper.IsInstance(null));
     Assert.False(SwitchExpressionSyntaxWrapper.IsInstance(SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
 }
 public static bool HasDiscardPattern(this SwitchExpressionSyntaxWrapper switchExpression) =>
 switchExpression.Arms.Any(arm => DiscardPatternSyntaxWrapper.IsInstance(arm.Pattern.SyntaxNode));
Exemplo n.º 6
0
        public void TestIsInstanceTrue()
        {
            var switchExpression = this.CreateSwitchExpression();

            Assert.True(SwitchExpressionSyntaxWrapper.IsInstance(switchExpression));
        }