Skip to content

Instantly share code, notes, and snippets.

@AlekSi
Created January 19, 2013 14:28
Show Gist options
  • Save AlekSi/4572944 to your computer and use it in GitHub Desktop.
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.
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);
}
}
@sethwklein
Copy link

@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