}
}
+static void
+pkgnotused(int lineno, Strlit *path, char *name)
+{
+ char *elem;
+
+ // If the package was imported with a name other than the final
+ // import path element, show it explicitly in the error message.
+ // Note that this handles both renamed imports and imports of
+ // packages containing unconventional package declarations.
+ // Note that this uses / always, even on Windows, because Go import
+ // paths always use forward slashes.
+ elem = strrchr(path->s, '/');
+ if(elem != nil)
+ elem++;
+ else
+ elem = path->s;
+ if(strcmp(elem, name) == 0)
+ yyerrorl(lineno, "imported and not used: \"%Z\"", path);
+ else
+ yyerrorl(lineno, "imported and not used: \"%Z\" as %s", path, name);
+}
+
void
mkpackage(char* pkgname)
{
// 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);
+ pkgnotused(s->def->lineno, s->def->pkg->path, s->name);
s->def = N;
continue;
}
// 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);
+ pkgnotused(s->def->pack->lineno, s->def->pack->pkg->path, s->name);
s->def->pack->used = 1;
}
s->def = N;
--- /dev/null
+package p
+
+import (
+ "./a" // ERROR "imported and not used: \x22a\x22 as surprise"
+ "./b" // ERROR "imported and not used: \x22b\x22 as surprise2"
+ b "./b" // ERROR "imported and not used: \x22b\x22$"
+ foo "math" // ERROR "imported and not used: \x22math\x22 as foo"
+ "fmt" // actually used
+ "strings" // ERROR "imported and not used: \x22strings\x22"
+)
+
+var _ = fmt.Printf