Skip to content

Commit e29abd4

Browse files
committed
CSHARP-56j28: Requested changes
1 parent 631be11 commit e29abd4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/PartialEvaluator.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public static Expression EvaluatePartially(Expression expression)
5050
// nested types
5151
private class SubtreeEvaluator : ExpressionVisitor
5252
{
53+
// private static fields
54+
private static readonly Expression __falseConstantExpression = Expression.Constant(false, typeof(bool));
55+
private static readonly Expression __trueConstantExpression = Expression.Constant(true, typeof(bool));
56+
5357
// private fields
5458
private readonly HashSet<Expression> _candidates;
5559

@@ -87,15 +91,15 @@ protected override Expression VisitBinary(BinaryExpression node)
8791
{
8892
// true && Q => Q
8993
// false && Q => false
90-
return leftValue ? Visit(rightExpression) : Expression.Constant(false);
94+
return leftValue ? Visit(rightExpression) : __falseConstantExpression;
9195
}
9296

9397
rightExpression = Visit(rightExpression);
9498
if (IsConstant<bool>(rightExpression, out var rightValue))
9599
{
96100
// P && true => P
97101
// P && false => false
98-
return rightValue ? leftExpression : Expression.Constant(false);
102+
return rightValue ? leftExpression : __falseConstantExpression;
99103
}
100104

101105
return node.Update(leftExpression, conversion: null, rightExpression);
@@ -108,15 +112,15 @@ protected override Expression VisitBinary(BinaryExpression node)
108112
{
109113
// true || Q => true
110114
// false || Q => Q
111-
return leftValue ? Expression.Constant(true) : Visit(rightExpression);
115+
return leftValue ? __trueConstantExpression : Visit(rightExpression);
112116
}
113117

114118
rightExpression = Visit(rightExpression);
115119
if (IsConstant<bool>(rightExpression, out var rightValue))
116120
{
117121
// P || true => true
118122
// P || false => P
119-
return rightValue ? Expression.Constant(true) : leftExpression;
123+
return rightValue ? __trueConstantExpression : leftExpression;
120124
}
121125

122126
return node.Update(leftExpression, conversion: null, rightExpression);
@@ -144,10 +148,10 @@ protected override Expression VisitConditional(ConditionalExpression node)
144148
{
145149
return (ifTrueValue, ifFalseValue) switch
146150
{
147-
(false, false) => Expression.Constant(false), // T ? false : false => false
151+
(false, false) => __falseConstantExpression, // T ? false : false => false
148152
(false, true) => Expression.Not(test), // T ? false : true => !T
149153
(true, false) => test, // T ? true : false => T
150-
(true, true) => Expression.Constant(true) // T ? true : true => true
154+
(true, true) => __trueConstantExpression // T ? true : true => true
151155
};
152156
}
153157
else if (IsConstant<bool>(ifTrue, out ifTrueValue))

0 commit comments

Comments
 (0)