Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "unused imports" a warning (via flag) #3778

Closed
cloneable opened this issue Jun 25, 2012 · 5 comments
Closed

Make "unused imports" a warning (via flag) #3778

cloneable opened this issue Jun 25, 2012 · 5 comments

Comments

@cloneable
Copy link

When one comments out code it often means that one has to comment out one or more import
lines to make the program compile again.

This is somewhat annoying because it reduces productivity for two reasons:
- imports are usually at the other end of the file and one has to jump all the way up or
keep each source file in a two editors (imports+source).
- it's not always clear which imports need to be "deactivated" and the
compiler stops after 10 errors, so one has to try once or more times to find the unused
imports.

Please consider making this error a warning instead (yyerrorl -> warnl), or
switchable via flag: -dev, -devmode or something.
@rsc
Copy link
Contributor

rsc commented Jun 25, 2012

Comment 1:

One workaround is to comment out code with if false { } instead of /* */.

@robpike
Copy link
Contributor

robpike commented Jun 25, 2012

Comment 2:

Or for import X, write at the top level
var _ X.SomethingInX // Delete before committing.
http://golang.org/doc/go_faq.html#unused_variables_and_imports

Status changed to WorkingAsIntended.

@cloneable
Copy link
Author

Comment 3:

In case someone reads this in the hope of finding a practical and reasonable way I
suggest replacing the two yyerrorl() in question with warnl().
--- a/src/cmd/gc/lex.c  Mon Jun 25 08:54:36 2012 -0700
+++ b/src/cmd/gc/lex.c  Tue Jun 26 09:03:15 2012 +0200
@@ -2187,7 +2187,7 @@
                    // errors if a conflicting top-level name is
                    // introduced by a different file.
                    if(!s->def->used && !nsyntaxerrors)
-                       yyerrorl(s->def->lineno, "imported and not used: \"%Z\"", s->def->pkg->path);
+                       warnl(s->def->lineno, "[W] imported and not used: \"%Z\"", s->def->pkg->path);
                    s->def = N;
                    continue;
                }
@@ -2195,7 +2195,7 @@
                    // throw away top-level name left over
                    // from previous import . "x"
                    if(s->def->pack != N && !s->def->pack->used && !nsyntaxerrors) {
-                       yyerrorl(s->def->pack->lineno, "imported and not used: \"%Z\"",
s->def->pack->pkg->path);
+                       warnl(s->def->pack->lineno, "[W] imported and not used: \"%Z\"",
s->def->pack->pkg->path);
                        s->def->pack->used = 1;
                    }
                    s->def = N;

@gopherbot
Copy link

Comment 4 by cornelius.howl:

I agree, it really decreases my productivity when doing development.
Can we also do warning for unused variable? I think warnings would be more appropriate
for these unused variables.
Please consider making this error a warning instead (yyerrorl -> warnl), or switchable
via flag: -dev, -devmode or something.

@rsc
Copy link
Contributor

rsc commented Feb 12, 2013

Comment 5:

http://tip.golang.org/doc/effective_go.html#blank

Labels changed: added restrict-addissuecomment-commit.

@golang golang locked and limited conversation to collaborators Dec 8, 2014
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants