[英]Compiler option to catch unassigned r-values
相当尴尬的是,我写了类似下面的东西(消毒过):
vector_item next()
{
if (this->it == this->myVector.end()){
this->it == this->myVector.begin();
}
return *(this->it++);
}
明显的错误很明显。 然而,它确实需要一段时间来追踪。
没有为此生成编译器警告,但是应该有吗? 在这里创建了一个未使用的r值(并且不是说,来自为其副作用调用的函数的未使用的返回值),这在我看来是代码存在问题的指示器。
用g++ -Wall -Wextra
编译。 (GCC 4.8.3)
我知道-Wunused-result
但这不适用于此。
没有为此生成编译器警告,但是应该有吗?
我想不出我在标准中读到的任何需要警告的内容。
然而,clang开发团队似乎认为它保证一个:
18 : <source>:18:18: warning: equality comparison result unused [-Wunused-comparison]
this->it == this->myVector.begin();
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
18 : <source>:18:18: note: use '=' to turn this equality comparison into an assignment
this->it == this->myVector.begin();
^~
=
1 warning generated.
我的第一个想法是,它是gcc中的QoI问题。 可能值得提出它作为一个问题。
我很幸运,因为我们的软件是针对mac(clang),linux(gcc)和windows(msvc)编译的,因此可以及早发现标准侵权和边缘案例。
可能会想到在另一个编译器上重新编译你的代码然后再进行一次bug搜索 - 这对我很有帮助。
没有为此生成编译器警告,但是应该有吗?
标准中没有任何声明可以使GCC为该案例生成警告。
你可以通过将它标记为WARN_UNUSED
来扩展begin()
,首先你要定义:
#define WARN_UNUSED __attribute__((warn_unused_result))
正如这里所描述的那样,但这当然不是你想要的,但它是一些东西。 我找不到GCC的任何选项来为您的案例生成警告。
虽然这是一个已知的GCC错误 ,但尚未实现您正在寻找的功能,至少要到2017-07-21。
但是,clang 6.0.0会发出警告,(即使不使用Wall
和Wextra
标志):
prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
this->it == this->myVector.begin();
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
this->it == this->myVector.begin();
^~
=
1 warning generated.
此外,zapcc 1.0.1也发出警告(即使没有警告标志):
/home/jail/prog.cc:11:18: warning: equality comparison result unused [-Wunused-comparison]
this->it == this->myVector.begin();
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/home/jail/prog.cc:11:18: note: use '=' to turn this equality comparison into an assignment
this->it == this->myVector.begin();
^~
=
1 warning generated.
如果你愿意,可以在Wandbox中自己查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.