关闭golang 的 variable declared but not used 和 package imported but not used

254 views
Skip to first unread message

life

unread,
Nov 15, 2013, 10:35:53 AM11/15/13
to golang...@googlegroups.com
大家有没有觉得golang的 variable declared but not used 和 package imported but not used 在调试代码的时候很不方便?
有没有方法去掉这两个验证? 再上生产的时候开启验证再改代码? 
我通过修改源码, 重新编译源码解决了, 从此世界清静了, 不知道会不会有问题:

1 解决:declared but not used 
修改go/src/cmd/gc/walk.c
   
for(l=fn->dcl; l; l=l->next) {
         if(l->n->op != ONAME || (l->n->class&~PHEAP) != PAUTO || l->n->sym->name[0] == '&' || l->n->used)
              continue;
         if(l->n->defn && l->n->defn->op == OTYPESW) {
              if(l->n->defn->left->used)
                   continue;
              lineno = l->n->defn->left->lineno;
              yyerror("%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); // life
         }
    }  

2 解决imported but not used:
修改:go/src/cmd/gc/lex.c
if(s->def->op == OPACK) {
           // throw away top-level package name leftover
          // from previous file.
         // leave s->block set to cause redeclaration
           // 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 usedsss : \"%Z\"", s->def->pkg->path);
      }
      s->def = N;
            continue;
 }


Message has been deleted

life

unread,
Nov 15, 2013, 12:49:08 PM11/15/13
to golang...@googlegroups.com
或者改成warn()而不是yyerror(), 这样编译时有提醒(IDE也会有). 从而知道哪要修改, 但又不影响编译的结果.
在walk.c已注释处改成:
warn("%S declared and not used", l->n->sym);
在lex.c已注释处改成:
lineno = s->def->lineno;
warn("imported and not used: \"%Z\"" , s->def->pkg->path);


在 2013年11月15日星期五UTC+8下午11时35分53秒,life写道:

fighterlyt

unread,
Nov 15, 2013, 1:33:39 PM11/15/13
to golang...@googlegroups.com
强烈不建议这么做,导入未使用和声明未使用本来就是为了避免更多的bug出现,现在你的这种行为,是因噎废食。


life <lif...@gmail.com>写到:

--
抱歉暂时无法详细说明。这份邮件是使用安装有Gmail Plus的Android移动设备发送的。

life

unread,
Nov 15, 2013, 8:49:36 PM11/15/13
to golang...@googlegroups.com
因为这个问题, 所以推荐改成warn(), 而不是全部注释掉. 我觉得在开发的时候难免会声明一些变量不用, 或临时导入一些包来测试. 这时候却报错, 又要把那些删除掉, 可之后调度时又需要用到, 这样又要添加... 这样不麻烦吗?

在 2013年11月16日星期六UTC+8上午2时33分39秒,skywalker写道:

Qi

unread,
Nov 15, 2013, 10:47:30 PM11/15/13
to golang...@googlegroups.com
如果是变量的话
var _ = v
如果是包的话
var _ = fmt.Print

这样就不报错了,等确定变量不用了,把这行去掉就行了

Go本身就是开源的,用的不爽的地方自己改源码挺好的,相比某些人用的不爽就上来骂。。。
Reply all
Reply to author
Forward
0 new messages