False positive warning with C++ structured bindings

Please provide

  • Operating system: Ubuntu 20.04, Linux/GCC-9
  • SonarLint plugin version: v4.8.0
  • Programming language you’re coding in: C++
  • Is connected mode used: No

And a thorough description of the problem / question:

SonarLint reported “1st function call argument is an uninitialized value [+4 locations] sonarlint(cpp:S836)” with this code snipset. But I think it is false positive warning.

// Function signature of ParseTuple: auto ParseTuple(const std::string& s) -> tl::expected<std::tuple<std::string, std::string, std::string>, Error>;

// tl::expected is open source implementation of std::expected.
// https://github.com/TartanLlama/expected

const auto default_value = std::make_tuple("lemon", "lime", "pomelo");
const auto& [x, y, z] = ParseTuple("(lemon,lime)").value_or(default_value);
const auto it = std::find_if(
    tups.cbegin(), tups.cend(),  // Type of tups is vector<tuple<string,string,string>>
    [&x](const auto& t) { return x == std::get<0>(t); }  // <-- sonarlint(cpp:S836)
);

OMG. Until C++20, structured bindings can not be captured by lambda expression.
https://en.cppreference.com/w/cpp/language/structured_binding

I compiled this code for C++17!

If you compile this with C++20, our analyzer indeed reports a false positive. CPP-5405 tracks this issue.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.