In cases where the full text of the format string is static and known at compile time, shouldn’t a missing format string parameter be a compile-time error or, at the very least, warning?
ReSharper catches this, but it’s just an underlined squiggle. I was under the impression that this would trigger a general compile-time error:
string x = string.Format("soeuotnh {0}");
Is there any way to trigger a warning on this kind of error without having to run my code through FxCop or something? Even C/C++ compilers will trigger an warning/error for such a blatantly clear bug (though they generally won’t check type-safety).
I have filed a request for consideration of this feature on Microsoft Connect, as I feel that
At the end of the day, it’s a “yes it could, no it doesn’t, maybe it should” situation. I was trying to make sure that there wasn’t something I could set in the compiler (not third party software) to make it more agressive with its warnings or if there were some way I could change my code to make it throw an exception (for instance, declaring everything as const), but it turns out (thus far) that that is not possible. Regardless of the response of the Visual Studio team on the issue I opened on MS Connect, the fact remains that the compiler certainly can catch a great many string formatting exceptions at compile time, that it would be a benefit… but it remains for the VS team to determine whether or not it is something worth implementing.