Custom code inspection pattern searching strings

Hi,

I'm new to the feature, but I'd like to define a pattern, that searches string comparisons with == operator. I want to suggest, that they get replaced by an EqualsIgnoreCase().

It would be great if something like this :

if (a.Text == b.Text)

would be found as also

var a = list.Where(l=>l.Text == "test")

Is this possible? I haven't found any solution.

Greets

2 comments

This structured pattern works for me.  Inside lambda expressions as well.

<CustomPatterns>
  <Pattern Severity="HINT" FormatAfterReplace="True" ShortenReferences="True" Language="CSHARP">
    <Comment>String comparison could use EqualsIgnoreCase()</Comment>
    <ReplaceComment>Make string comparison case insensitive</ReplaceComment>
    <ReplacePattern>EqualsIgnoreCase($str1$, $str2$)</ReplacePattern>
    <SearchPattern>$str1$ == $str2$</SearchPattern>
    <Params />
    <Placeholders>
      <ExpressionPlaceholder Name="str1" ExpressionType="System.String" ExactType="True" />
      <ExpressionPlaceholder Name="str2" ExpressionType="System.String" ExactType="True" />
    </Placeholders>
  </Pattern>
</CustomPatterns>



Attachment(s):
pattern.xml
0

Thanks! It works. I'm still not sure what I made wrong. Maybe I used something like %str% == %str% .. my fault.

Greets

0

Please sign in to leave a comment.