Is there any way to generate a warning in VS2008 for unused using statements? I know there is the Edit->Intellisense->Organize Usings->Remove Unused Usings
, but it would be nice if this were a compile time warning.
views:
227answers:
4No because it is not a compiler issue, so you cannot generate compiler warnings. Compiler warnings only get generated when a dead code spot is found, such as an empty catch statement. Or an unused variable. Basically anything that could cause runtime issues. Since using statements have nothing to do with runtime and are more style issues of a text file, they will not generate warnings.
But you can use a tool like reSharper to alert you of unused using statements.
If you use ReSharper, by default, it'll show the unused usings in a different color (as a warning in the error analysis sidebar), and if you highlight them, you can change them to show as errors. Unfortunately, it won't prevent it from compiling, but it does alert you that they're not being used.
I guess it's not a compiler warning because it has absolutely no consequences whatsoever...
We try to create warnings for situations where the code in question is almost certainly broken, misleading or useless. Furthermore, since many people compile with "warnings as errors" turned on, we have a high bar; it's bad to introduce a spurious or superfluous warning.
Imagine you were compiling with "warnings as errors" turned on by default. Imagine we implemented the feature you want. You start up a new project, and we generate for you:
using System;
using System.Text;
using System.Linq;
class Program
{
static void Main(string[] arguments)
{
}
}
and we immediately tell you that you have an error in your program because none of the "usings" are necessary! That is a very bad user experience. Those "usings" are put there by the IDE for your convenience, so that you don't have to define them yourself. Your proposed feature would turn that from a convenience into a nag.