String format verification for .NET2.0
After some research, I've found out that FxCop already contain a rule for string format checking (Usage Rules\Provide correct arguments to formatting methods
). It's even identifies the example in @Sklivvz answer. Unfortunately:
- The rule exist only in the old 1.35 version but not in 1.36 (see: Discontinued FxCop rules in V1.36).
- The existing of lambda expressions in the code yield no rule errors or exception message. It just stops the check without informing. I'm guessing that's because V1.35 uses .NET runtime 2.0.
- The rule only cover calls for
Console.Write
, Console.WriteLine
, string.Format
& StringBuilder.AppendFormat
. I managed to tweak it (using Reflector) to also cover Common.Logging.ILog.DebugFormat
, but I'm pretty sure it's contradicts with MS EULA ("You may not reverse engineer, decompile, or disassemble...").
So, you can safely use FxCop for testing string formatting in your .NET2.0 apps.
But what about newer .NET versions?
For all of you working with more up-to-date versions, I've written a StyleCop rule that check string literals for common format error patterns. It's far from perfect and yield many false positives but still cover allot.
I would like to check the new FxCop SDK for a more comprehensive solution.
I will post my conclusions here.