How are you using the pattern matching functionality in ReSharper 5?

一个人想着一个人 提交于 2019-12-02 18:35:51

Matching a [Flags] enum bit

.NET 4 introduces the System.Enum.HasFlag method which can tidy up your code.

Before:

(myValue & MyFlagsEnum.Foo) == MyFlagsEnum.Foo

After:

myValue.HasFlag(MyFlagsEnum.Foo)

XML:

<CustomPatterns>
  <Pattern Severity="SUGGESTION">
    <Comment>Can condense using 'Enum.HasFlag' method</Comment>
    <ReplaceComment>Replace bit matching with 'Enum.HasFlag'</ReplaceComment>
    <ReplacePattern>$myValue$.HasFlag($target$)</ReplacePattern>
    <SearchPattern><![CDATA[($myValue$ & $target$) == $target$]]></SearchPattern>
    <Params />
    <Placeholders>
      <ExpressionPlaceholder Name="myValue" ExpressionType="System.Enum" ExactType="False" />
      <ExpressionPlaceholder Name="target" ExpressionType="System.Enum" ExactType="False" />
    </Placeholders>
  </Pattern>
</CustomPatterns>
brgerner

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>
Drew Noakes

JetBrains offer a Sample Pattern Catalog for Structural Search and Replace for download containing 17 patterns:

  • 'try/finally' block can be converted to 'using' statement
  • Method StringBuilder.Append can be converted to StringBuilder.AppendFormat
  • Comparison with true is redundant
  • Conditional statement is redundant
  • Code is unreachable
  • 'if' block is never executed
  • Identical branches in a conditional statement
  • Redundant compound assignment with |= operator
  • Redundant compound assignment with &= operator
  • Redundant compound assignment with |= operator (alternative case)
  • Redundant compound assignment with &= operator (alternative case)
  • Redundant initialization to false and condition block
  • Redundant initialization to true and condition block
  • Method Array.CreateInstance can be replaced with an array creation expression
  • Method Array.CreateInstance can be replaced with a two-dimensional array creation expression
  • Redundant usage of GetType() == typeof() with a value type
  • Method OfType can be used for type-based filtering

Restarting a Stopwatch

.NET 4 introduces the System.Diagnostics.Stopwatch.Restart() method which can tidy up your code.

Before:

stopwatch.Reset();
stopwatch.Start();

After:

stopwatch.Restart();

XML:

<CustomPatterns>
  <Pattern Severity="SUGGESTION">
    <Comment>Use Restart method for System.Diagnostics.Stopwatch</Comment>
    <ReplaceComment>Use Restart method for System.Diagnostics.Stopwatch</ReplaceComment>
    <ReplacePattern>$stopwatch$.Restart();</ReplacePattern>
    <SearchPattern><![CDATA[$stopwatch$.Reset();
$stopwatch$.Start();]]></SearchPattern>
    <Params />
    <Placeholders>
      <ExpressionPlaceholder Name="stopwatch" ExpressionType="System.Diagnostics.Stopwatch" ExactType="True" />
    </Placeholders>
  </Pattern>
</CustomPatterns>
Justin

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>

To 'as' cast.

Example:

This code:

string s = (string) o;

will be replaced by:

string s = o as string;

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

The XML:

<Pattern Severity="HINT">
  <Comment>Cast</Comment>
  <ReplaceComment>To 'as' cast</ReplaceComment>
  <ReplacePattern>$exp$ as $type$</ReplacePattern>
  <SearchPattern>($type$)$exp$</SearchPattern>
  <Params />
  <Placeholders>
    <TypePlaceholder Name="type" Type="" ExactType="True" />
    <ExpressionPlaceholder Name="exp" ExpressionType="" ExactType="True" />
  </Placeholders>
</Pattern>
brgerner

Remove enclosing braces around a body.

Example:

This code:

    foreach (int i in arr)
    {
      DoNextStep();
    }

will be replaced (by Ctrl+L to remove foreach row and then run this pattern to remove braces) by:

DoNextStep();

So only body of braces remains after typing around two shortcuts.

The XML:

<Pattern Severity="HINT">
  <Comment>Curly braces with body</Comment>
  <ReplaceComment>Remove braces</ReplaceComment>
  <ReplacePattern>$body$</ReplacePattern>
  <SearchPattern>{$body$}</SearchPattern>
  <Params />
  <Placeholders>
    <StatementPlaceholder Name="body" Minimal="-1" Maximal="-1" />
  </Placeholders>

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>

For example, Microsoft recommends (and Code Analysis/FxCop generates appropriate warnings) if you are doing a comparison between a string value and an empty string, to use the String.IsNullOrEmpty() method.

http://david.gardiner.net.au/2010/02/resharper-5-structural-search-and.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!