views:

466

answers:

5

Is there a setting in Resharper 4 (or even Visual Studio itself...) that forces a warning if I forget to wrap code in a using block, or ommit the proper Dispose call in a finally block?

+3  A: 

Not out of the box. That's not to say you can't extend it, though.

This discussion indicates that calling Dispose on unit tests was considered, though.

Kilhoffer
+2  A: 

Correct automatic Dispose analysis requires DFA (Data Flow Analysis) in a global way. It is unlikely that you create an IDisposable object and doesn't call any method on it and do not pass it around as an argument. If disposable object is passed to other methods (including calling its members, when "this" is implicitly passed), the tool should analyse if Dispose is not called within, or that object is not stored somewhere for later disposal.

That said, naive implementation of checking if disposable object is in fact disposed with "using" construct or in any other way would yield too much false positives, and render analysis useless.

Ilya Ryzhenkov
Not sure why you got voted down Ilya, makes sense for someone knowledgable in R# and the way it works to comment on a question about R#
Shaun Austin
I dunno, I've got voted down on another ReSharper related question, too. Looks like someone don't like me, personally. Or hate ReSharper :)
Ilya Ryzhenkov
Well I for one love it, and it's great to have people related to the product posting on StackOverflow. Cheers!
Shaun Austin
+2  A: 

You could design a small add-in to R# that you could have run inside the code editor that scans the code and updates the code analysis to reflect that you an object who's missing the structure you've just described.

I'd look into the R# plugin architecture if you decide to go that route.

+1  A: 

See this blog post for some tricks for testing for Dispose() in DEBUG. Basically, write a DEBUG-only destructor that asserts that you were disposed.

Jay Bazuzi
+1  A: 

You might want to look at FXCop for this: http://msdn.microsoft.com/en-us/library/ms182328%28VS.80%29.aspx

Its a pity R# doesn't handle it, even if just a warning for fields in your class and/or variables you create.