private static bool ContainsExpression(ExecutableStatementSyntax container, ExpressionSyntax contained)
 {
     return(contained != null &&
            container.DescendantNodes()
            .OfType <MemberAccessExpressionSyntax>()
            .Any(m => m.Expression != null && VisualBasicEquivalenceChecker.AreEquivalent(contained, m.Expression)));
 }
        protected override bool AreDuplicates(MethodBlockSyntax firstMethod, MethodBlockSyntax secondMethod)
        {
            return(firstMethod.Statements.Count >= 2 &&
                   firstMethod.GetIdentifierText() != secondMethod.GetIdentifierText() &&
                   HaveSameParameters(firstMethod.GetParameters(), secondMethod.GetParameters()) &&
                   VisualBasicEquivalenceChecker.AreEquivalent(firstMethod.Statements, secondMethod.Statements));

            bool HaveSameParameters(SeparatedSyntaxList <ParameterSyntax>?leftParameters, SeparatedSyntaxList <ParameterSyntax>?rightParameters)
            {
                if (leftParameters == null && rightParameters == null)
                {
                    return(true);
                }

                if ((leftParameters == null && rightParameters != null) ||
                    (leftParameters != null && rightParameters == null) ||
                    leftParameters.Value.Count != rightParameters.Value.Count)
                {
                    return(false);
                }

                return(leftParameters.Value.Zip(rightParameters.Value, (p1, p2) => new { p1, p2 })
                       .All(tuple => tuple.p1.IsEquivalentTo(tuple.p2, false)));
            }
        }
 private static void ReportIfExpressionsMatch(SyntaxNodeAnalysisContext context, ExpressionSyntax left, ExpressionSyntax right,
                                              SyntaxToken operatorToken)
 {
     if (VisualBasicEquivalenceChecker.AreEquivalent(left.RemoveParentheses(), right.RemoveParentheses()))
     {
         var message = string.Format(OperatorMessageFormat, operatorToken);
         context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, context.Node.GetLocation(), message));
     }
 }
Exemple #4
0
        public void AreEquivalent_List_VB()
        {
            var result = VisualBasicEquivalenceChecker.AreEquivalent(methods_VB.Method1, methods_VB.Method2);

            result.Should().BeTrue();

            result = VisualBasicEquivalenceChecker.AreEquivalent(methods_VB.Method1, methods_VB.Method3);
            result.Should().BeFalse();
        }
Exemple #5
0
        public void AreEquivalent_Node_VB()
        {
            var result = VisualBasicEquivalenceChecker.AreEquivalent(methods_VB.Method1.First(), methods_VB.Method2.First());

            result.Should().BeTrue();

            result = VisualBasicEquivalenceChecker.AreEquivalent(methods_VB.Method1.First(), methods_VB.Method3.First());
            result.Should().BeFalse();
        }
 private static void CheckConditionAt(int currentIndex, List <ExpressionSyntax> conditions, SyntaxNodeAnalysisContext context)
 {
     for (var j = 0; j < currentIndex; j++)
     {
         if (VisualBasicEquivalenceChecker.AreEquivalent(conditions[currentIndex], conditions[j]))
         {
             context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, conditions[currentIndex].GetLocation(),
                                                                  conditions[j].GetLineNumberToReport()));
             return;
         }
     }
 }
Exemple #7
0
 protected override void Initialize(SonarAnalysisContext context)
 {
     context.RegisterSyntaxNodeActionInNonGenerated(
         c =>
     {
         var expression = (AssignmentStatementSyntax)c.Node;
         if (VisualBasicEquivalenceChecker.AreEquivalent(expression.Left, expression.Right))
         {
             c.ReportDiagnosticWhenActive(Diagnostic.Create(rule, c.Node.GetLocation()));
         }
     },
         SyntaxKind.SimpleAssignmentStatement);
 }
        protected override void Initialize(SonarAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var ifStatement = (SingleLineIfStatementSyntax)c.Node;

                if (ifStatement.ElseClause != null &&
                    VisualBasicEquivalenceChecker.AreEquivalent(ifStatement.ElseClause.Statements, ifStatement.Statements))
                {
                    ReportIssue(ifStatement.ElseClause.Statements, ifStatement.Statements, c, "branch");
                }
            },
                SyntaxKind.SingleLineIfStatement);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var ifBlock = (MultiLineIfBlockSyntax)c.Node;

                var statements = new[] { ifBlock.Statements }
                .Concat(ifBlock.ElseIfBlocks.Select(elseIf => elseIf.Statements))
                .Concat(new[] { ifBlock.ElseBlock?.Statements ?? new SyntaxList <StatementSyntax>() })
                .Where(l => l.Any())
                .ToList();

                for (var i = 1; i < statements.Count; i++)
                {
                    CheckStatementsAt(i, statements, c, "branch");
                }
            },
                SyntaxKind.MultiLineIfBlock);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var select     = (SelectBlockSyntax)c.Node;
                var statements = select.CaseBlocks.Select(b => b.Statements).ToList();
                for (var i = 1; i < statements.Count; i++)
                {
                    CheckStatementsAt(i, statements, c, "case");
                }
            },
                SyntaxKind.SelectBlock);
        }
        private static void CheckStatementsAt(int currentIndex, List <SyntaxList <StatementSyntax> > statements,
                                              SyntaxNodeAnalysisContext context, string constructType)
        {
            var currentBlockStatements = statements[currentIndex];

            if (currentBlockStatements.Count(IsApprovedStatement) < 2)
            {
                return;
            }

            for (var j = 0; j < currentIndex; j++)
            {
                if (VisualBasicEquivalenceChecker.AreEquivalent(currentBlockStatements, statements[j]))
                {
                    ReportIssue(currentBlockStatements, statements[j], context, constructType);
                    return;
                }
            }
        }
 protected override bool ContainsOnlyThrow(CatchBlockSyntax currentCatch) =>
 VisualBasicEquivalenceChecker.AreEquivalent(currentCatch.Statements, ThrowBlock);
Exemple #11
0
 protected override bool AreDuplicates(MethodBlockSyntax firstMethod, MethodBlockSyntax secondMethod) =>
 firstMethod.Statements.Count > 1 &&
 firstMethod.GetIdentifierText() != secondMethod.GetIdentifierText() &&
 HaveSameParameters(firstMethod.GetParameters(), secondMethod.GetParameters()) &&
 VisualBasicEquivalenceChecker.AreEquivalent(firstMethod.Statements, secondMethod.Statements);