-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why RCS1098 is enabled by default? #183
Comments
While it often true that
Here's a class with all possible overrides of public class Test
{
public static bool operator ==(Test x, Test y) => true;
public static bool operator !=(Test x, Test y) => !(x == y);
public static bool operator ==(Test x, object y) => true;
public static bool operator !=(Test x, object y) => !(x == y);
public static bool operator ==(object x, Test y) => true;
public static bool operator !=(object x, Test y) => !(x == y);
} And here you can see in the comments, which var a = new Test();
if (null == a) { } // Test == Test
if (a == null) { } // Test == Test
if (null == (object)a) { } // object == object
if (a == (object)null) { } // Test == object
if ((object)null == a) { } // object == Test
if ((object)a == null) { } // object == object As your links point out having |
Yoda condition is not relevant because this analyzer is applicable only for Also as Jon Hanna points out "a non-symmetric == is always a bug". So now we can agree that changing Disabling this analyzer by default is worth consideration but before doing that can someone provide c# project (repository) that consistently use |
While I can't point to any such repo, I can suggest a possibly cleaner way to do object null check, using
At least the compiler I tested with is generating the exactly same code. |
Avoid 'null' on the left side of a binary expression
null==a in C# means ReferenceEquals(a,null)
https://stackoverflow.com/questions/34229318/should-null-be-checked-on-the-left-or-right-hand-side
https://stackoverflow.com/questions/970334/is-referenceequalsnull-obj-the-same-thing-as-null-obj
https://stackoverflow.com/questions/4850053/what-is-good-practice-for-null-reference-checks
The text was updated successfully, but these errors were encountered: