Warn, when the initialisation or step expression of a for-statement has no effect.
authorChristoph Mallon <christoph.mallon@gmx.de>
Tue, 25 Dec 2007 13:41:01 +0000 (13:41 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Tue, 25 Dec 2007 13:41:01 +0000 (13:41 +0000)
[r18816]

parser.c

index fe527a4..4af9af6 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -5461,7 +5461,11 @@ static statement_t *parse_for(void)
                if(is_declaration_specifier(&token, false)) {
                        parse_declaration(record_declaration);
                } else {
-                       statement->fors.initialisation = parse_expression();
+                       expression_t *const init = parse_expression();
+                       statement->fors.initialisation = init;
+                       if (warning.unused_value  && !expression_has_effect(init)) {
+                               warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
+                       }
                        expect(';');
                }
        } else {
@@ -5473,7 +5477,11 @@ static statement_t *parse_for(void)
        }
        expect(';');
        if(token.type != ')') {
-               statement->fors.step = parse_expression();
+               expression_t *const step = parse_expression();
+               statement->fors.step = step;
+               if (warning.unused_value  && !expression_has_effect(step)) {
+                       warningf(step->base.source_position, "step of 'for'-statement has no effect");
+               }
        }
        expect(')');
        statement->fors.body = parse_loop_body(statement);