WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
16 stars 3 forks source link

WTG3012 and or assignment expressions with false boolean literal #191

Closed andrewhongnsw closed 2 years ago

andrewhongnsw commented 2 years ago

The following or assignment expression is handled correctly:

Example 1

value1 |= true;

...becomes...

value1 = true;

However the following or assignment expression becomes an invalid statement, triggering CS0201.

Example 4

value1 |= false;

...becomes...

value1;
brian-reichle commented 2 years ago

I'm guessing it's only looking at it as an expression, and if the result of the expression is being used then it needs to maintain the existing behaviour, eg.

Foo(value1 |= false);

becomes

Foo(value1);

We should probably detect if the parent of the resulting expression is an ExpressionStatement, and if it is, delete it. Would need to be careful of the possibility that the statement to be deleted is not contained within a BlockStatement though.

if (Foo()) Value1 |= false;