Menu

Identical condition 'name1==name2', second condition is always false

Absol
2021-09-02
2021-09-03
  • Absol

    Absol - 2021-09-02

    https://github.com/danmar/cppcheck/pull/3431/commits/d4ad1b7aa37e831ee6c98f03d168983cc6095231

    lib/checkother.cpp:3600:18: debug: Executable scope 'IsSameName' with unknown function. [symbolDatabaseWarning]
    bool CheckOther::IsSameName(std::string name1, std::string name2, bool forcecheck = false)
                     ^
    lib/checkother.cpp:3609:15: warning: Identical condition 'name1==name2', second condition is always false [identicalConditionAfterEarlyExit]
        if (name1 == name2)
                  ^
    lib/checkother.cpp:3602:15: note: If condition 'name1==name2' is true, the function will return/exit
        if (name1 == name2)
                  ^
    lib/checkother.cpp:3609:15: note: Testing identical condition 'name1==name2'
        if (name1 == name2)
                  ^
    lib/checkother.cpp:3654:50: style: Call to 'Token::tokAt()' followed by 'Token::str()' can be simplified. [redundantNextPrevious]
                        std::string fieldname = tok->tokAt(2)->str();
                                                     ^
    lib/symboldatabase.cpp:2791:74: style: token and funcStart name mismatch. Did you mean: tok [mismatchingNamesWriteError]
    

    I got some false detects from cppcheck when tried to add new check (10444)

     
  • Absol

    Absol - 2021-09-02
    //next line : 3600:18
    bool CheckOther::IsSameName(std::string name1, std::string name2, bool forcecheck = false)
    {
    //next line : 3602:15
        if (name1 == name2)
            return true;
    
        std::transform(name1.begin(), name1.end(), name1.begin(),
            [](unsigned char c) { return std::tolower(c); });
        std::transform(name2.begin(), name2.end(), name2.begin(),
            [](unsigned char c) { return std::tolower(c); });
    //next line : 3609:15
      if (name1 == name2)
            return true;
    
        if (name1.length() > 2 && name2.length() > 2)
        {
            if (mSettings->certainty.isEnabled(Certainty::inconclusive) || forcecheck)
                return std::max(percent_of_varname_match_back(name1, name2, forcecheck), percent_of_varname_match_front(name1, name2, forcecheck)) > 50;
        }
    
        return false;
    }
    
     

    Last edit: Absol 2021-09-02
  • Absol

    Absol - 2021-09-02

    and

    lib/checkother.cpp:3654:50: style: Call to 'Token::tokAt()' followed by 'Token::str()' can be simplified. [redundantNextPrevious]
                        std::string fieldname = tok->tokAt(2)->str();
    

    how to do it ?

                    // 3654:50
                       std::string fieldname = tok->tokAt(2)->str();
    
     
    • Daniel Marjamäki

      tok->strAt(2)

       
      • Absol

        Absol - 2021-09-03

        thanks 👍, but why it not writed in output? (if cppcheck known it)

         
  • CHR

    CHR - 2021-09-02

    Regarding your first problem: The warning can be avoided if the default argument is removed from the definition. See here.

     
    • Absol

      Absol - 2021-09-03

      But I need this argument, it more easy to use if using default argument value

       
      • CHR

        CHR - 2021-09-03

        Then put it on the declaration instead?

         
        • Daniel Marjamäki

          hmm.. I thought it was a syntax error to write the default value like that. there is some compiler that don't require it on the declaration?

           
        • Absol

          Absol - 2021-09-03

          and then got syntax error?

           
          • Daniel Marjamäki

            Cppcheck is not good at detecting syntax errors. We pass through lots of strange stuff and try to handle it as good as possible. I want that the compiler is used to check the code first.

            If we wrote strict syntax checks according to the C/C++ standard then Cppcheck would be much less useful.

             
            • Absol

              Absol - 2021-09-03

              but why in my case cppcheck print error ? this is false.positive?

              Run ./cppcheck -q -j$(nproc) --std=c++11 --template=selfcheck -D__CPPCHECK__ --error-exitcode=1 --inline-suppr --suppressions-list=.travis_suppressions --library=cppcheck-lib -Ilib -Iexternals/simplecpp/ -Iexternals/tinyxml2/ -Icli --inconclusive --enable=style,performance,portability,warning,internal --exception-handling --debug-warnings cli lib
              lib/checkother.cpp:3600:18: debug: Executable scope 'IsSameName' with unknown function. [symbolDatabaseWarning]
              
              bool CheckOther::IsSameName(std::string name1, std::string name2, bool forcecheck = false)
                               ^
              
               

              Last edit: Absol 2021-09-03
              • CHR

                CHR - 2021-09-03

                Why is the the default argument still on the definition?

                 
                • Absol

                  Absol - 2021-09-03

                  definition or declaration? in first case it legal, and always been legally.

                  sorry if bad english)

                   
              • Daniel Marjamäki

                You must have a class with the name CheckOther otherwise you can't have a function CheckOther::IsSameName.

                 
                • Absol

                  Absol - 2021-09-03

                  but it compiled and executed without any problem, you can write how i do to fix this error found by cppcheck?? this function defined in this class.

                   
        • CHR

          CHR - 2021-09-03

          I was surprised also, but it is apparently legal.

          For a member function of a non-template class, the default arguments are allowed on the out-of-class definition, and are combined with the default arguments provided by the declaration inside the class body.

          https://en.cppreference.com/w/cpp/language/default_arguments
          Clang and MSVC both accept the code in the ticket.

           
  • Absol

    Absol - 2021-09-03

    Also

    //original cppcheck
    C:\Projects\ToolsProjects\cppcheck\lib\checkother.cpp:3177:0: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]
            if (f.type == Function::Type::eFunction && f.name() == varname)
    ^
    C:\Projects\ToolsProjects\cppcheck\lib\checkother.cpp:3203:70: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]
                        if (arg.nameToken() && var.name() == arg.name()) {
                                                                         ^
    //from my pull request          
    C:\Projects\ToolsProjects\cppcheck\lib\checkother.cpp:3668:45: style: Consider using std::any_of algorithm instead of a raw loop. [useStlAlgorithm]
                                                {
                                                ^
    C:\Projects\ToolsProjects\cppcheck\lib\checkother.cpp:3719:49: style: Consider using std::any_of algorithm instead of a raw loop. [useStlAlgorithm]
                                                    {
                                                    ^
    Checking C:\Projects\ToolsProjects\cppcheck\lib\checkother.cpp: CPPCHECKLIB_EXPORT;_WIN32...
    

    also strange proposes

     

    Last edit: Absol 2021-09-03

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.