How are you using the pattern matching functionali

2020-05-19 02:59发布

Resharper 5's new pattern matching seems very powerful, though it takes a bit of tinkering to work out how to use it.

For those who aren't familiar with this feature, it allows you to search for specific patterns within your code. Instances of such patterns may optionally be replaced with an alternative. In IntelliJ this was called structural search and replace. It's much more powerful than simple RegEx search/replace.

I'd like to collect a series of patterns that people are using so that I can learn how to use this feature better.

I propose that each answer include:

  • a brief introduction of the rationale for the pattern
  • an example of what it would match
  • an optional example of a replacement
  • the XML generated by exporting the pattern so that others can try it out too

9条回答
迷人小祖宗
2楼-- · 2020-05-19 03:43

Remove if envelop around if body.

Example:

This code:

if (ok)
{
  DoNextStep();
}

will be replaced by:

DoNextStep();

So only body of if remains.

The XML:

  <Pattern Severity="HINT">
    <Comment>if</Comment>
    <ReplaceComment>Remove enclosing if</ReplaceComment>
    <ReplacePattern>$body$</ReplacePattern>
    <SearchPattern>if($condition$){$body$}</SearchPattern>
    <Params />
    <Placeholders>
      <StatementPlaceholder Name="body" Minimal="-1" Maximal="-1" />
      <ExpressionPlaceholder Name="condition" ExpressionType="System.Boolean" ExactType="True" />
    </Placeholders>
  </Pattern>
查看更多
放荡不羁爱自由
3楼-- · 2020-05-19 03:52

To regular cast.

Example:

This code:

string s = o as string;

will be replaced by:

string s = (string) o;

So now you have the regular cast instead of 'as' cast.

The XML:

<Pattern Severity="HINT">
  <Comment>Cast (as)</Comment>
  <ReplaceComment>To regular cast</ReplaceComment>
  <ReplacePattern>($type$)$exp$</ReplacePattern>
  <SearchPattern>$exp$ as $type$</SearchPattern>
  <Params />
  <Placeholders>
    <ExpressionPlaceholder Name="exp" ExpressionType="" ExactType="True" />
    <TypePlaceholder Name="type" Type="" ExactType="True" />
  </Placeholders>
</Pattern>
查看更多
4楼-- · 2020-05-19 03:53

This one's different. I discovered later in my project that mbunit asserts which compare property values to enums don't render nice messages when using the AssertEx.That syntax.

So I created a pattern to find this:

AssertEx.That(() => myVariable.Status == MyEnum.Ok);  

...and replace it with this:

Assert.AreEqual(MyEnum.Ok, myVariable.Status);

Here's the pattern:

<Pattern Severity="WARNING">
<Comment>AssertEx.That asserts for enum values don't give nice error msgs</Comment>
<ReplaceComment>Replace AssertEx.That asserts for enum values with trad Assert.AreEqual for better error msgs</ReplaceComment>
<ReplacePattern>Assert.AreEqual($enum$,$variable$.$property$)</ReplacePattern>
<SearchPattern><![CDATA[AssertEx.That(() => $variable$.$property$ == $enum$]]></SearchPattern>
<Params />
<Placeholders>
  <ExpressionPlaceholder Name="enum" ExpressionType="System.Enum" ExactType="False" />
  <IdentifierPlaceholder Name="variable" Type="" ExactType="False" RegEx="" CaseSensitive="True" />
  <IdentifierPlaceholder Name="property" Type="" ExactType="False" RegEx="" CaseSensitive="True" />
</Placeholders>

查看更多
登录 后发表回答