diff --git a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs index 587da1caf8c886d228a7fdcb67eb7e27be52a659..0f17d60b6b7ad29e424e86faee2477402829effd 100644 --- a/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs +++ b/src/Compilers/CSharp/Portable/Lowering/DiagnosticsPass_ExpressionTrees.cs @@ -123,7 +123,8 @@ private static Symbol GetLocalOrParameterSymbol(BoundExpression expr) public override BoundNode VisitAssignmentOperator(BoundAssignmentOperator node) { - if (!CheckForAssignmentToSelf(node) && _inExpressionLambda && node.Left.Kind != BoundKind.ObjectInitializerMember && node.Left.Kind != BoundKind.DynamicObjectInitializerMember) + CheckForAssignmentToSelf(node); + if (_inExpressionLambda && node.Left.Kind != BoundKind.ObjectInitializerMember && node.Left.Kind != BoundKind.DynamicObjectInitializerMember) { Error(ErrorCode.ERR_ExpressionTreeContainsAssignment, node); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs index e68d5e4bbdc123750ff0a90bbe9285a9d5d57a15..b05477e96342794b0302ab4ab2be716e07f3732a 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/LambdaTests.cs @@ -1295,5 +1295,30 @@ static void stuff() Assert.Equal("Program a", symbolInfo.Symbol.ToTestDisplayString()); } + + [Fact] + [WorkItem(3826, "https://github.com/dotnet/roslyn/issues/3826")] + public void ExpressionTreeSelfAssignmentShouldError() + { + var source = @" +using System; +using System.Linq.Expressions; + +class Program +{ + static void Main() + { + Expression> x = y => y = y; + } +}"; + var compilation = CreateCompilationWithMscorlibAndSystemCore(source); + compilation.VerifyDiagnostics( + // (9,45): warning CS1717: Assignment made to same variable; did you mean to assign something else? + // Expression> x = y => y = y; + Diagnostic(ErrorCode.WRN_AssignmentToSelf, "y = y").WithLocation(9, 45), + // (9,45): error CS0832: An expression tree may not contain an assignment operator + // Expression> x = y => y = y; + Diagnostic(ErrorCode.ERR_ExpressionTreeContainsAssignment, "y = y").WithLocation(9, 45)); + } } }