Skip to content

Commit

Permalink
function-args-by-value: Also take into account manually defined ==/= …
Browse files Browse the repository at this point in the history
…operators
  • Loading branch information
alex1701c authored and gruenich committed Dec 27, 2024
1 parent 95392bd commit 09074e4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/checks/level2/function-args-by-value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ bool FunctionArgsByValue::shouldIgnoreClass(CXXRecordDecl *record)

bool FunctionArgsByValue::shouldIgnoreOperator(FunctionDecl *function)
{
// Too many warnings in operator<<
static const std::vector<StringRef> ignoreList = {"operator<<"};

// Too many warnings in operator<<, unrelated warnings for = or == operators
static const std::vector<StringRef> ignoreList = {"operator<<", "operator=", "operator=="};
if (auto *cxxFnc = dyn_cast<CXXMethodDecl>(function)) { // Get different clazy::name overload
return clazy::contains(ignoreList, clazy::name(cxxFnc));
}
return clazy::contains(ignoreList, clazy::name(function));
}

Expand Down
3 changes: 2 additions & 1 deletion tests/function-args-by-value/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"filename": "bug379342.cpp"
},
{
"filename": "default_methods.cpp"
"filename": "default_methods.cpp",
"flags": "-Wno-c++20-extensions"
},
{
"filename": "warn-for-overridden-methods.cpp",
Expand Down
53 changes: 53 additions & 0 deletions tests/function-args-by-value/default_methods.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <cstdio>

struct Color
{
unsigned int r;
unsigned int g;
unsigned int b;
unsigned int opacity;

Color &operator=(const Color &c) = default;
bool operator==(const Color &c) const = default;
Color(const Color &c) = default;
Color() = default;
};
struct Color2
{
unsigned int r;
unsigned int g;
unsigned int b;
unsigned int opacity;

Color2 &operator=(const Color2 &c) {
r=c.r;
g=c.g;
b=c.b;
opacity =c.opacity;
return *this;
}
bool operator==(const Color2 &c) const {
return c.b == b;
}
};

int main()
{
Color c;
c.r = 3;

Color c2;
c2 = c;

Color c3(c2);


Color2 otherType;
Color2 otherType2;

otherType2 = otherType;

printf("C2.r %d\n", c2.r);
printf("C3.r %d\n", c3.r);
}

Empty file.

0 comments on commit 09074e4

Please sign in to comment.