Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to generate compiler warning for unused usings?

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.

like image 825
Taylor Leese Avatar asked Sep 16 '09 19:09

Taylor Leese


People also ask

How do I enable warnings in GCC?

The warning is made into an error by -pedantic-errors . Same as -Wimplicit-int and -Wimplicit-function-declaration . This warning is enabled by -Wall . -Wimplicit-fallthrough is the same as -Wimplicit-fallthrough=3 and -Wno-implicit-fallthrough is the same as -Wimplicit-fallthrough=0 .

What is a warning message generated by a compiler?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

How do I ignore GCC warnings?

To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. From the GCC Warning Options: You can request many specific warnings with options beginning -W , for example -Wimplicit to request warnings on implicit declarations.

Which keyword is used in configuration to suppress the warnings generated when compiling a configuration with sensitive data?

The compiler can suppress warnings that were introduced after a version you specify by using the /Wv compiler option.


2 Answers

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.

like image 89
Eric Lippert Avatar answered Sep 19 '22 08:09

Eric Lippert


No 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.

like image 30
Nick Berardi Avatar answered Sep 22 '22 08:09

Nick Berardi