[Proposal] Compiler warnings with unobvious assignment #2527
Labels
Approved-Rule
Indicates if the new proposed rule has been approved to move to implementation phase
Feature Request
help wanted
The issue is up-for-grabs, and can be claimed by commenting
Needs-Fixer
Milestone
C#
is a beautiful and flexible language that will not allow a beginner to "shoot off his leg"... or it will?Issue
Let's imagine that we have the following class:
Then we want to do something like this:
What we'll see in the console's output?
Most of you're now thinking something like this:
And I have some bad news for you, cause it's
false
:DIf you're surprised, then let me surprise you even more:
Explanation
When we have construction like this:
As a result, we'll get this
IL
-code:But if we want to simplify this, and we'll use the following construction (that should mean the same as the example above):
IL
will look like this:As we see, the assignment happens to the object we didn't expected (cause we know that assignment goes from right to left).
Proposal
I found this behaviour kinda unobvious. Especially for those who aren't familiar with the "subtleties" of
C#
So I want to suggest introducing compiler warnings for those cases where the left side of an assignment contains a variable, the object inside of which changes somewhere in the right side of the expression, since it wouldn't be very cool to replace
a = b; a.x = b;
with a similara.x = a = b;
and spend a few sleepless nights in search of a bug :DThe text was updated successfully, but these errors were encountered: