解释 clang 静态分析器消息
为什么 Clang 静态分析器 (CSA) 输出以下消息:
虽然存储到“self”的值是 在封闭表达式中使用时, 值从未被实际读取 “自我”
对于以下方法:
- (id)init
{
return (self = [super initWithStyle:UITableViewStyleGrouped]);
}
代码按预期工作,所以我想知道从技术角度来看代码是否不正确,这是 CSA 中的错误,或者我只是错过了一些非常明显的东西。
仅供参考,我使用此模式是因为我不希望创建此类实例的类能够指定表格样式。
Why does the Clang Static Analyzer (CSA) output the following message:
Although the value stored to 'self' is
used in the enclosing expression, the
value is never actually read from
'self'
for the following method:
- (id)init
{
return (self = [super initWithStyle:UITableViewStyleGrouped]);
}
The code works as expected, so I'm wondering whether the code is incorrect from a technical point-of-view, this is a bug within CSA or I'm simply missing something very obvious.
FYI, I'm using this pattern because I don't want the class creating an instance of this class to be able to specify the table style.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更“正确”的方法如下:
它应该满足静态分析器
编辑:
我对为什么 Clang 不喜欢该行的最佳猜测:
当您编写
(self = [super initWithStyle:UITableViewStyleGrouped])
,init
调用的结果存储到临时变量中,然后复制到self
中,并且那么它就是该方法实际返回的临时变量。尽管这是完全合法且正常的行为(并且不会破坏您的应用程序),但静态分析器(正确地)注意到存储在 self 中的值从未被实际读取。
为了说明这一点,请使用以下代码:
引发相同的静态分析器错误。
A more "proper" way to do this would be as follows:
And it should satisfy the static analyzer
edit:
My best guess as to why Clang doesn't like that line:
When you write
(self = [super initWithStyle:UITableViewStyleGrouped])
, the result of theinit
call is stored into a temporary variable, which is then copied intoself
, and then it is that temporary variable that is actually returned from the method.Although this is perfectly legal and normal behaviour (and will not break your app), the static analyzer (correctly) notices that the value stored in
self
is never actually read.To illustrate, the following code:
Throws the same static analyzer error.
它告诉您
self =
部分是不必要的。从“损坏或危险”的意义上来说,这并不是不正确的,但它是毫无意义的。变量self
从未被使用过,所以给它赋值是没有意义的。它可以简单地写成return [super initWithStyle:UITableViewStyleGrouped];
没有任何问题。It's telling you that the
self =
part is unnecessary. It's not incorrect in sense of "broken or dangerous," but in that it's pointless. The variableself
is just never used, so there's no point in assigning to it. It could be simply written asreturn [super initWithStyle:UITableViewStyleGrouped];
without any problem.