Created
January 19, 2013 14:28
-
-
Save AlekSi/4572944 to your computer and use it in GitHub Desktop.
Tiny patch for Go 1.0.3 to turn "is declared and not used" and "imported and not used" errors into warnings. See comment below.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -r 2d8bc3c94ecb src/cmd/gc/lex.c | |
--- a/src/cmd/gc/lex.c Fri Sep 21 17:10:44 2012 -0500 | |
+++ b/src/cmd/gc/lex.c Sat Jan 19 18:25:39 2013 +0400 | |
@@ -2200,7 +2200,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, "imported and not used: \"%Z\"", s->def->pkg->path); | |
s->def = N; | |
continue; | |
} | |
@@ -2208,7 +2208,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, "imported and not used: \"%Z\"", s->def->pack->pkg->path); | |
s->def->pack->used = 1; | |
} | |
s->def = N; | |
diff -r 2d8bc3c94ecb src/cmd/gc/subr.c | |
--- a/src/cmd/gc/subr.c Fri Sep 21 17:10:44 2012 -0500 | |
+++ b/src/cmd/gc/subr.c Sat Jan 19 18:25:39 2013 +0400 | |
@@ -400,7 +400,7 @@ | |
} | |
if(n == 0) { | |
// can't possibly be used - there were no symbols | |
- yyerrorl(pack->lineno, "imported and not used: \"%Z\"", opkg->path); | |
+ warnl(pack->lineno, "imported and not used: \"%Z\"", opkg->path); | |
} | |
} | |
diff -r 2d8bc3c94ecb src/cmd/gc/walk.c | |
--- a/src/cmd/gc/walk.c Fri Sep 21 17:10:44 2012 -0500 | |
+++ b/src/cmd/gc/walk.c Sat Jan 19 18:25:39 2013 +0400 | |
@@ -94,11 +94,11 @@ | |
if(l->n->defn->left->used) | |
continue; | |
lineno = l->n->defn->left->lineno; | |
- yyerror("%S declared and not used", l->n->sym); | |
+ warn("%S declared and not used", l->n->sym); | |
l->n->defn->left->used = 1; // suppress repeats | |
} else { | |
lineno = l->n->lineno; | |
- yyerror("%S declared and not used", l->n->sym); | |
+ warn("%S declared and not used", l->n->sym); | |
} | |
} | |
Looks like a good patch. If you're new to Go, however, I'd like to point out the effect of the following changes to the code in cortesi's blog post:
import (
"io/ioutil"
"fmt"
)
// BUG: remove this after debugging
// no need to fuss with imports when debugging with fmt
// just put this here:
var _ = fmt.Print
...
...
m, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
...
...
// no need to hunt for the declaration of m, just put this by the comment:
_ = m
//DoSomething(m)
@sethwklein I'm not new to Go, I write Go code for 💰 every day now, and I also created gonuts.io for community. I know how blank identifier works. Generally, I like how Go threats those errors as fatal. But for local hacking it sometimes throws me out of my "zone". I made this patch to actually experience the other side, not just read/write about it.
@AlekSi Of course you're not new to go; most people patching the Go compiler aren't, I think :) Many people reading this may be, though, and might not have read http://golang.org/doc/faq#unused_variables_and_imports. Peace.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This tiny patch for Go 1.0.3 turns "is declared and not used" and "imported and not used" errors into warnings. Inspired by http://corte.si/posts/code/go/go-rant.html
How to apply it: