Since resharper still doesn't give out any warning regarding objects implementing IDisposable, I'd like to create some custom search patterns available in resharper 5.0.
So far I have this:
(And don't mind my replace comments in the patterns, I don't really care about it, I just want a clear warning in the code when dealing with disposable objects.)
- <CustomPatterns>
- <Pattern Severity="WARNING">
<Comment>This class implements IDisposable interface.</Comment>
<ReplaceComment>Please use Using statement, or dispose the object manually when done using.</ReplaceComment>
<SearchPattern>$type$</SearchPattern>
<Params />
- <Placeholders>
<IdentifierPlaceholder Name="var" Type="" ExactType="False" RegEx="" CaseSensitive="True" />
<TypePlaceholder Name="type" Type="System.IDisposable" ExactType="False" />
<ArgumentPlaceholder Name="args" Minimal="-1" Maximal="-1" />
</Placeholders>
</Pattern>
- <Pattern Severity="WARNING">
<Comment>This class implements IDisposable interface.</Comment>
<ReplaceComment>Please use Using statement, or dispose the object manually when done using.</ReplaceComment>
<SearchPattern>new $type$($args$)</SearchPattern>
<Params />
- <Placeholders>
<IdentifierPlaceholder Name="var" Type="" ExactType="False" RegEx="" CaseSensitive="True" />
<TypePlaceholder Name="type" Type="System.IDisposable" ExactType="False" />
<ArgumentPlaceholder Name="args" Minimal="-1" Maximal="-1" />
</Placeholders>
</Pattern>
</CustomPatterns>
This handles cases of variable declaration, e.g.
Bitmap myBitmap = GetBitmap();
private Bitmap _bitmap;
and CTOR calls, e.g.
var myBitmap = new Bitmap(...);
What it doesn't support, is this:
var myBitmap = GetBitmap();
I can't find any example of how to define a search pattern that will either find 'var' usage, or a method return type, that is typeof IDisposable.
I'm sure there's a way, I can't find it though.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…