Menu

False positive: [knownConditionTrueFalse] increment in loop condition, valueflow

2020-07-14
2020-09-10
  • Andreas Grob

    Andreas Grob - 2020-07-14
    #include <iostream>
    
    int main() {
        int a = 0;
        while (++a < 5) {
        }
        if (a == 1) {}
        std::cout << a << '\n';
    }
    

    output:

    $ ./a.out 
    5
    

    cppcheck: commit 62702a6
    built with: make MATCHCOMPILER=yes FILESDIR=/usr/share/cppcheck HAVE_RULES=yes

    $ cppcheck --enable=all main.cpp 
    Checking main.cpp ...
    main.cpp:7:11: style: Condition 'a==1' is always true [knownConditionTrueFalse]
        if (a == 1) {}
              ^
    main.cpp:4:13: note: Assignment 'a=0', assigned value is 0
        int a = 0;
                ^
    main.cpp:5:14: note: a is incremented', new value is 1
        while (++a < 5) {
                 ^
    main.cpp:7:11: note: Condition 'a==1' is always true
        if (a == 1) {}
              ^
    
     
  • Daniel Marjamäki

    Thanks! I can reproduce. I created ticket https://trac.cppcheck.net/ticket/9808

     
  • Andreas Grob

    Andreas Grob - 2020-09-10

    Fixed for me in 48a6852.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.