roslynator icon indicating copy to clipboard operation
roslynator copied to clipboard

Feature request: New analyzer and fix: Replace String.Compare(.......) == 0 with String.Equals

Open MSeifert04 opened this issue 7 years ago • 3 comments
trafficstars

In some case the String.Compare where the result is checked for equality to zero could be replaced with String.Equals. At least the variants that take string, string:

String.Compare(a, b) == 0;
String.Equals(a, b, StringComparison.CurrentCulture);

And string, string, bool:

String.Compare(a, b, true) == 0;
String.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);

I'm not really sure about the other overloads of String.Compare though.

It would be handy to have an analyzer that reports these cases and also a fix for these.

MSeifert04 avatar Jun 28 '18 09:06 MSeifert04

I guess we can extend this suggestion to have CompareOrdinal(a,b) == 0 replaced with Equals(a,b)

wiz0u avatar Jul 02 '18 12:07 wiz0u

and also the non-static a.CompareTo(b) == 0 replaced with a.Equals(b, StringComparison. CurrentCulture)

wiz0u avatar Mar 01 '19 17:03 wiz0u

Compare(string, string, StringComparison) == 0 seems also translatable to Equals(string, string, StringComparison)

wiz0u avatar Mar 01 '19 17:03 wiz0u