Regular Expressions for Visual Studio Code Analysis

By Michael Flanakin @ 1:30 PM :: 1221 Views :: 1 Comments :: .NET 2.0 :: Digg it!

CA1805 - Do Not Initialize Unnecessarily

Next to CA1802, this one's probably the simplest in the bunch. Basically, remove all initializers that set values to 0, null, or false. The one problem with this message is it will be triggered in try/catch blocks where the value may or may not have been set previously. Along those same lines, some variable need to be initialized prior to being used in a catch block. You won't get the code analysis message, but the find/replace doesn't know the difference, so you'll need to be judicious as to where you apply it. Then again, you'll get build errors, so at least it's not a run-time problem.

Find: :Wh*=:Wh*(0|0\.0|null|false);
Replace: ;

After some thought, and the fact that I typically only use the regex for fields, I ultimately decided to specialize the regex a bit more so that only fields were affected. Of course, this won't cover local variables, but that's probably a good thing, considering the aforementioned try/catch issue.

Find: {(public|private|protected|internal):Wh*(:i\.*)*:Wh*:i}:Wh*=:Wh*(0|0\.0|null|false);
Replace: \1;

Perhaps an idea for the future, assuming I do get all this packaged into a VS add-in, might be to do the reverse to refactor on initialization-related build errors -- add the default initialization.

Ratings

Comments

By Joben.Rara.blogs(".NET") @ Monday, June 25, 2007 11:22 AM

Click here to post a comment



Categories Categories