Skip to content

LGTM.com - false positive cpp/comparison-of-identical-expressions #7385

Closed
@bblanchon

Description

@bblanchon

Description of the false positive

I'm using a meta-programming trick that reduces the number of overloads by sorting the arguments to a function like so:

template <typename TString1, typename TString2>
typename std::enable_if<TString1::typeSortKey <= TString2::typeSortKey, int>::type
stringCompare(TString1 s1, TString2 s2) {
  // IMPLEMENTATION
}

template <typename TString1, typename TString2>
typename std::enable_if<(TString1::typeSortKey > TString2::typeSortKey), int>::type
stringCompare(TString1 s1, TString2 s2) {
  return -stringCompare(s2, s1);
}

As you can see, this code uses the same implementation even if the arguments are reversed.

The alert "Self Comparison" triggers on the expression TString1::typeSortKey <= TString2::typeSortKey.
Indeed, it can be a self-comparison if TString1 and TString2 are the same type, but in the general case, it's not.

URL to the alert on the project page on LGTM.com

https://lgtm.com/projects/g/bblanchon/ArduinoJson/snapshot/6d857fd73270f4ee1e03f5e9aa5c1e7d1ee0f179/files/src/ArduinoJson/Strings/StringAdapters.hpp?sort=name&dir=ASC&mode=heatmap#x262128796e29999c:1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions