Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-73911

Implement a clang-tidy check like `readability-redundant-string-cstr` but for StringData.

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Unresolved
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None
    • Server Security

    Description

      Consider adding a check for certain wasteful calls with std::string that results in unnecessary memory allocations or calls to strlen. For instance: std::string(std::string("abc").c_str()) would be waste and could be simplified as std::string("abc"). While this example is contrived, there are other likely possibilities with implicit constructor calls with StringData. Clang-tidy already has a check called readability-redundant-string-cstr that inspired me.

      Potentially useful checks:
      1. StringData(std::string("abc").c_str())

      • wasteful as it requires a call strlen instead of the StringData(std::string&) constructor
        2. StringData(StringData("abc").toString())
      • allocates a std::string instead of directly passing a StringData to a StringData.

      I am not sure how frequent these or other wasteful patterns with StringData are in the code though. It may or may not be worth considering to adding a clang-tidy check for these.

      Reference:
      https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-string-cstr.html
      Example of a similar check for LLVM's StringRef type.
      https://github.com/llvm/llvm-project/blob/3de0bc4c3d0284354b0c0ec8ca1536ee080193e2/clang-tools-extra/clang-tidy/readability/RedundantStringCStrCheck.cpp#L162-L180

      Attachments

        Activity

          People

            backlog-server-security Backlog - Security Team
            mark.benvenuto@mongodb.com Mark Benvenuto
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: