Example #1
0
            public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
            {
                base.VisitBinaryOperatorExpression(binaryOperatorExpression);

                var match = pattern.Match(binaryOperatorExpression);

                if (!match.Success)
                {
                    return;
                }
                var expr = match.Get <Expression> ("expr").First();
                // check if expr is of boolean type
                var exprType = ctx.Resolve(expr).Type.GetDefinition();

                if (exprType == null || exprType.KnownTypeCode != KnownTypeCode.Boolean)
                {
                    return;
                }

                AddIssue(binaryOperatorExpression, ctx.TranslateString("Simplify boolean comparison"), scrpit => {
                    var boolConstant = (bool)match.Get <PrimitiveExpression> ("const").First().Value;
                    if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
                        (binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant))
                    {
                        expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr.Clone());
                        expr.AcceptVisitor(insertParenthesesVisitor);
                    }
                    scrpit.Replace(binaryOperatorExpression, expr);
                });
            }
Example #2
0
        public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
        {
            // lifted operators can't be transformed
            if (binaryOperatorExpression.Annotation <LiftedOperator>() != null)
            {
                return(base.VisitBinaryOperatorExpression(binaryOperatorExpression, data));
            }

            BinaryOperatorType op = binaryOperatorExpression.Operator;
            bool?rightOperand     = null;

            if (binaryOperatorExpression.Right is PrimitiveExpression)
            {
                rightOperand = ((PrimitiveExpression)binaryOperatorExpression.Right).Value as bool?;
            }
            if (op == BinaryOperatorType.Equality && rightOperand == true || op == BinaryOperatorType.InEquality && rightOperand == false)
            {
                // 'b == true' or 'b != false' is useless
                var ilRanges = binaryOperatorExpression.GetAllRecursiveILRanges();
                binaryOperatorExpression.Left.AcceptVisitor(this, data);
                binaryOperatorExpression.ReplaceWith(binaryOperatorExpression.Left.WithAnnotation(ilRanges));
                return(null);
            }
            else if (op == BinaryOperatorType.Equality && rightOperand == false || op == BinaryOperatorType.InEquality && rightOperand == true)
            {
                // 'b == false' or 'b != true' is a negation:
                Expression left = binaryOperatorExpression.Left;
                left.Remove();
                var ilRanges = binaryOperatorExpression.GetAllRecursiveILRanges();
                UnaryOperatorExpression uoe = new UnaryOperatorExpression(UnaryOperatorType.Not, left);
                binaryOperatorExpression.ReplaceWith(uoe);
                uoe.AddAnnotation(ilRanges);
                return(uoe.AcceptVisitor(this, data));
            }
            else
            {
                bool  negate = false;
                Match m      = asCastIsNotNullPattern.Match(binaryOperatorExpression);
                if (!m.Success)
                {
                    m      = asCastIsNullPattern.Match(binaryOperatorExpression);
                    negate = true;
                }
                if (m.Success)
                {
                    Expression expr = m.Get <Expression>("expr").Single().Detach().IsType(m.Get <AstType>("type").Single().Detach());
                    if (negate)
                    {
                        expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr);
                    }
                    binaryOperatorExpression.ReplaceWith(expr);
                    expr.AddAnnotation(binaryOperatorExpression.GetAllRecursiveILRanges());
                    return(expr.AcceptVisitor(this, data));
                }
                else
                {
                    return(base.VisitBinaryOperatorExpression(binaryOperatorExpression, data));
                }
            }
        }
Example #3
0
            public override void VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression)
            {
                base.VisitBinaryOperatorExpression(binaryOperatorExpression);

                var match = pattern.Match(binaryOperatorExpression);

                if (!match.Success)
                {
                    return;
                }
                var expr = match.Get <Expression> ("expr").First();
                // check if expr is of boolean type
                var exprType = ctx.Resolve(expr).Type.GetDefinition();

                if (exprType == null || exprType.KnownTypeCode != KnownTypeCode.Boolean)
                {
                    return;
                }

                var boolExpr     = match.Get <PrimitiveExpression>("const").First();
                var boolConstant = (bool)boolExpr.Value;

                TextLocation start, end;

                if (boolExpr == binaryOperatorExpression.Left)
                {
                    start = binaryOperatorExpression.StartLocation;
                    end   = binaryOperatorExpression.OperatorToken.EndLocation;
                }
                else
                {
                    start = binaryOperatorExpression.OperatorToken.StartLocation;
                    end   = binaryOperatorExpression.EndLocation;
                }

                AddIssue(new CodeIssue(
                             start, end,
                             boolConstant ? ctx.TranslateString("Comparison with 'true' is redundant") : ctx.TranslateString("Comparison with 'false' is redundant"),
                             ctx.TranslateString("Remove redundant comparison"),
                             script => {
                    if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
                        (binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant))
                    {
                        expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr.Clone());
                        expr.AcceptVisitor(insertParenthesesVisitor);
                    }
                    script.Replace(binaryOperatorExpression, expr);
                }
                             )
                {
                    IssueMarker = IssueMarker.GrayOut
                });
            }
Example #4
0
        public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
        {
            BinaryOperatorType op = binaryOperatorExpression.Operator;
            bool?rightOperand     = null;

            if (binaryOperatorExpression.Right is PrimitiveExpression)
            {
                rightOperand = ((PrimitiveExpression)binaryOperatorExpression.Right).Value as bool?;
            }
            if (op == BinaryOperatorType.Equality && rightOperand == true || op == BinaryOperatorType.InEquality && rightOperand == false)
            {
                // 'b == true' or 'b != false' is useless
                binaryOperatorExpression.Left.AcceptVisitor(this, data);
                binaryOperatorExpression.ReplaceWith(binaryOperatorExpression.Left);
                return(null);
            }
            else if (op == BinaryOperatorType.Equality && rightOperand == false || op == BinaryOperatorType.InEquality && rightOperand == true)
            {
                // 'b == false' or 'b != true' is a negation:
                Expression left = binaryOperatorExpression.Left;
                left.Remove();
                UnaryOperatorExpression uoe = new UnaryOperatorExpression(UnaryOperatorType.Not, left);
                binaryOperatorExpression.ReplaceWith(uoe);
                return(uoe.AcceptVisitor(this, data));
            }
            else
            {
                bool  negate = false;
                Match m      = asCastIsNotNullPattern.Match(binaryOperatorExpression);
                if (m == null)
                {
                    m      = asCastIsNullPattern.Match(binaryOperatorExpression);
                    negate = true;
                }
                if (m != null)
                {
                    Expression expr = m.Get <Expression>("expr").Single().Detach().IsType(m.Get <AstType>("type").Single().Detach());
                    if (negate)
                    {
                        expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr);
                    }
                    binaryOperatorExpression.ReplaceWith(expr);
                    return(expr.AcceptVisitor(this, data));
                }
                else
                {
                    return(base.VisitBinaryOperatorExpression(binaryOperatorExpression, data));
                }
            }
        }
			public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
			{
				base.VisitBinaryOperatorExpression (binaryOperatorExpression);

				var match = pattern.Match (binaryOperatorExpression);
				if (!match.Success)
					return;

				AddIssue (binaryOperatorExpression, ctx.TranslateString ("Simplify boolean comparison"), scrpit => {
					var expr = match.Get<Expression> ("expr").First ().Clone ();
					var boolConstant = (bool)match.Get<PrimitiveExpression> ("const").First ().Value;
					if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
						(binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant)) {
						expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr);
						expr.AcceptVisitor (insertParenthesesVisitor);
					}
					scrpit.Replace (binaryOperatorExpression, expr);
				});
			}
			public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
			{
				base.VisitBinaryOperatorExpression (binaryOperatorExpression);

				var match = pattern.Match (binaryOperatorExpression);
				if (!match.Success)
					return;
				var expr = match.First<Expression>("expr");
				// check if expr is of boolean type
				var exprType = ctx.Resolve (expr).Type.GetDefinition ();
				if (exprType == null || exprType.KnownTypeCode != KnownTypeCode.Boolean)
					return;

				AddIssue (binaryOperatorExpression, ctx.TranslateString ("Simplify boolean comparison"), scrpit => {
                    var boolConstant = (bool)match.First<PrimitiveExpression>("const").Value;
					if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
						(binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant)) {
						expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr.Clone());
						expr.AcceptVisitor (insertParenthesesVisitor);
					}
					scrpit.Replace (binaryOperatorExpression, expr);
				});
			}