Menu

False positive stlIfStrFind

scpeters
2016-07-06
2021-01-29
  • scpeters

    scpeters - 2016-07-06

    The following code gets flagged with a stlIfStrFind error "Inefficient usage of string::find() in condition; string::compare() would be faster."

    std::string line = getLine();
    if (line.find(" GL_EXTENSIONS =") < 12)
      continue;
    

    The full context of the error message is in checkstl.cpp, which notes that one should use compare if comparing to 0 or compare to std::string::npos if looking for no matches.

    In the above example, a string is checked for existence of a substring in an unknown but bounded location (starts within the first 12 characters), so I think it's a valid use of the API. I'm planning to suppress this warning but wanted to report it as a false positive.

     
  • CHR

    CHR - 2021-01-27

    v2.3 reports

    0c113d65.cpp:5:5: performance: Inefficient usage of string::find() in condition; string::starts_with() could be faster. [stlIfStrFind]
    if (line.find(" GL_EXTENSIONS =") < 12)
    

    on this completed snippet:

    int main() {
    int c{};
    while (c < 5) {
    std::string line = getLine();
    if (line.find(" GL_EXTENSIONS =") < 12)
      continue;
    ++c;
    }
    return c;
    }
    

    Since starts_with() can't be used in this situation, I think it's a FP.

     
  • CHR

    CHR - 2021-01-29
     

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.