Menu

False positive : dead code block

2019-06-10
2021-01-29
  • AnotherUser

    AnotherUser - 2019-06-10

    The following code generates a dead-code block error when running with --enable=all.

    Checked against commit 87e7b57170a82a601e7c8289c75acf26b46a40d9

    [tmp.cpp:11] -> [tmp.cpp:13]: (warning) Opposite inner 'if' condition leads to a dead code block.
    
    #include <iostream>
    #include <cstdlib>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<int> v = {};//1,3,4};
    
        if(v.size() !=1)
        {
            if(v.empty())
                cerr << "V is empty" << endl;
            else
                cerr << "V is bigger than one" << endl;
        }
    
    }
    
     
  • CHR

    CHR - 2021-01-26

    This one still reproduces with v2.3.

     
  • CHR

    CHR - 2021-01-29

    Apparently, this has been fixed in the meantime. Head gives the correct warnings

    53684d14a2.cpp:11:17: style: Condition 'v.size()!=1' is always true [knownConditionTrueFalse]
        if(v.size() !=1)
                    ^
    53684d14a2.cpp:13:19: style: Condition 'v.empty()' is always true [knownConditionTrueFalse]
            if(v.empty())
                      ^
    

    It also correctly handles the case v = {1,3,4};.

     

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.