Menu

False positive - knownConditionTrueFalse

pokopa2803
2020-09-30
2020-10-01
  • pokopa2803

    pokopa2803 - 2020-09-30

    Cppcheck-2.1

    #include <gmock/gmock.h>
    #include <gtest/gtest.h>
    #include <boost/optional.hpp>
    
    template <bool bool_param>
    inline int func(const int receivedRcc,
                                       const boost::optional<int&>& first = boost::none,
                                       const boost::optional<int&>& second = boost::none)
    {
    *first = 10;
    *second = 20;
       return 7;
    }
    
    TEST(cppcheck, cppcheck2)
    {
       int first = 0;
       int second = 0;
       EXPECT_EQ(0, func<false>(0, first, second));
       EXPECT_EQ(0, first);
       EXPECT_EQ(0, second);
       EXPECT_EQ(0, func<true>(0, first, second));
       EXPECT_EQ(0, first);
       EXPECT_EQ(0, second);
    }
    

    Result

    style/knownConditionTrueFalse: The comparison '0 == first' is always true.
    style/knownConditionTrueFalse: The comparison '0 == second' is always true.
    style/knownConditionTrueFalse: The comparison '0 == first' is always true.
    style/knownConditionTrueFalse: The comparison '0 == second' is always true.

     
  • Daniel Marjamäki

    well.. I can reproduce.. but I am not sure about the programming style to use const for a parameter and then write to its data.

    When I remove the const then the knownConditionTrueFalse is fixed. However I get a constParameter instead. Imho that is a false positive that should not be written.

    I created ticket https://trac.cppcheck.net/ticket/9927

     

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.