Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top