renesteg
renesteg

Reputation: 571

Value stored to 'var name' is never read

I am struggling a bit with a compiler warning. Here the code:

    const char *sql;
switch (fromVersion) {
    case 0:
    {
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column00 TEXT NOT NULL DEFAULT 'migrated from version 0'";
    }
    case 1:
    {
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column01 TEXT NOT NULL DEFAULT 'migrated from version 1'";
    }
    case 2:
    {
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column02 TEXT NOT NULL DEFAULT 'migrated from version 2'";
    }
    case 3:
    {                       
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column03 TEXT NOT NULL DEFAULT 'migrated from version 3'";
    }
    case 4:
    {                       
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column04 TEXT NOT NULL DEFAULT 'migrated from version 4'";
    }
    case 5:
    {                       
        sql = "ALTER TABLE abiliator_options ADD COLUMN new_column05 TEXT NOT NULL DEFAULT 'migrated from version 5'";
    }
}


sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
{
    if (sqlite3_step(selectstmt) == SQLITE_DONE) 
    {           
        NSLog(@"Alter statement successful");
        [self setDatabaseSchemaVersion];
    }
    else {

        NSLog(@"Failed to alter the table with message '%s'.", sqlite3_errmsg(database));
    }
}
else {
    NSLog(@"Failed to prepare the statement with message '%s'.", sqlite3_errmsg(database));

}
sqlite3_finalize(selectstmt);

First warning is Value stored to 'sql' is never read for all of the sql var assignments, except the last one in the switch (case 5). Case 5 does not result in a warning.

Second warning is Function call argument is an uninitialized value. This is for the if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) statement.

Thanks in advance for your hints.

Upvotes: 2

Views: 1086

Answers (5)

Deepesh
Deepesh

Reputation: 8053

Switch statement Structure:- You are missing "break" and " default:"

switch (expression)

{ case match1:

      statements

      break;

 case match2:

      statements

      break;

 default:

      statements

      break;

}

Upvotes: 1

alex-i
alex-i

Reputation: 5454

The warning is most probably because you're not using break; in the switch (you're setting new values over the old ones, without using them).

switch (fromVersion) {
case 0:
{
    sql = "ALTER TABLE abiliator_options ADD COLUMN new_column00 TEXT NOT NULL DEFAULT 'migrated from version 0'";
    break;
}
case 1:
{
    sql = "ALTER TABLE abiliator_options ADD COLUMN new_column01 TEXT NOT NULL DEFAULT 'migrated from version 1'";
    break;
}
...

Upvotes: 0

muffe
muffe

Reputation: 2295

Add a break; statement after each case. Further try adding a default case.

Upvotes: 2

user379305
user379305

Reputation:

You're missing the breaks after each case. Only the last assignment is being used.

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

  1. This is because you are missing break; statements between your cases.
  2. This is because you are missing a default:

Compiler tells you that when your fromVersion is, for example, 4, whatever you assign to it in the case 4: will get immediately overwritten by case 5:, because there is no break. It also tells you that when fromVersion is negative or more than 5, your sql is uninitialized.

Upvotes: 2

Related Questions