开发者

Interpreting clang static analyzer messages

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.


A more "proper" way to do this would be as follows:

- (id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    return self;
}

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 the init call is stored into a temporary variable, which is then copied into self, 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:

- (id)init
{
    id temp = [super initWithStyle:UITableViewStyleGrouped];
    self = temp;
    return temp;
}

Throws the same static analyzer error.


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 variable self is just never used, so there's no point in assigning to it. It could be simply written as return [super initWithStyle:UITableViewStyleGrouped]; without any problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜