Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a difference between FxCop 10 (which ships with the Windows 7 and .NET 4.0 SDK) and Code Analysis 2010 (which ships with Visual Studio Premium and higher). Code Analysis 2010 has a <a href="http://blogs.msdn.com/b/codeanalysis/archive/2010/04/14/data-flow-analysis-rules-in-visual-studio-2010.aspx" rel="nofollow">set of additional rules</a>, which includes a highly improved version of the IDisposable rules.</p> <p>With Code Analysis 2010 under Visual Studio Premium, the Factory isn't being flagged (as the rule now sees the IDisposable variable is returned to the calling method). The Receiving method, however, isn't flagged either, due to one of the corner case exceptions to the rule. There is a list of method names that will cause the rule to trigger. If you rename your <code>GetStream</code> method to <code>CreateStream</code>, suddenly the rule will trigger:</p> <pre><code>Warning 4 CA2000 : Microsoft.Reliability : In method 'ServiceUser.Execute()', call System.IDisposable.Dispose on object 'stream' before all references to it are out of scope. BadProject\Class1.cs 14 BadProject </code></pre> <p>I was unable to locate the list of method pre-fixes that will work. I've tried a few and <code>Create~</code>, <code>Open~</code> trigger the rule, many others that you might expect to work, don't, including <code>Build~</code>, <code>Make~</code>, <code>Get~</code>.</p> <p>Additionally there is a <a href="http://connect.microsoft.com/VisualStudio/SearchResults.aspx?SearchQuery=ca2000" rel="nofollow">long list of bugs surrounding this rule</a>. The rule was altered in Visual Studio 2010 to trigger fewer false positives, but now it sometimes misses items it should have flagged (and would have flagged in the previous version). There wasn't enough time to fix the rules in the Visual Studio 2010 time frame (check the bug report comments).</p> <p>With the upcoming Roslyn compilers, Code Analysis will probably see a major upgrade, until then there are only minor updates to be expected. The current build of Visual Studio Dev11 does not trigger where you want it.</p> <p>So concluding, no your attribute wouldn't help much, as the rule already detects that you're passing the IDisposable as a return value. Thus Code Analysis knows it's not good to dispose it before returning. If you're using the undocumented naming rules, the rule will trigger. Maybe an attribute could extend the naming rules, but I'd rather have Microsoft would actually fix the actual rule.</p> <p>I created a <a href="https://connect.microsoft.com/VisualStudio/feedback/details/714032/ca2000-uses-an-undocumented-naming-rule-can-that-rule-please-be-documented" rel="nofollow">connect bug</a> requesting the naming guideline to be documented in <a href="http://msdn.microsoft.com/en-us/library/ms182289.aspx" rel="nofollow">the rules documentation</a>.</p> <p>Comment from Microsoft:</p> <blockquote> <p>Posted by Microsoft on 1/19/2012 at 10:41 AM Hello,</p> <p>Thank you for taking the time to investigate this and file the request for the documentation update. However after some discussion with our documentation team, we have decided to not document the naming convention as you requested. </p> <p>As you indicated on the stackoverflow thread, there have historically been a lot of reliability issues with this rule, and keying off of the names was an internal implementation detail added to try to reduce the number of false positives. However this is not considered prescriptive guidance for how developers should name their methods, it was added after a survey of common coding practices. We believe the long-term fix is to improve the reliability of the rule, not add naming guidance to our public documentation based on internal implementation details that will continue to change as the rule is improved.</p> <p>Best Regards, Visual Studio Code Analysis Team</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload