Skip to content

Fix #12992 (Refactoring: Use l-value reference specifier for assignment operators to prevent misuse)#6665

Merged
danmar merged 2 commits intocppcheck-opensource:mainfrom
cppchecksolutions:misracpp-15.0.2
Aug 7, 2024
Merged

Fix #12992 (Refactoring: Use l-value reference specifier for assignment operators to prevent misuse)#6665
danmar merged 2 commits intocppcheck-opensource:mainfrom
cppchecksolutions:misracpp-15.0.2

Conversation

@danmar
Copy link
Copy Markdown
Collaborator

@danmar danmar commented Aug 6, 2024

Misra C++ 2023 15.0.2

@danmar
Copy link
Copy Markdown
Collaborator Author

danmar commented Aug 6, 2024

@firewave @chrchr-github do you have any opinion about this? It prevents some misuse, compilers will not allow the code below:

struct C {
    C& operator=(const C& c) &;
    int x;
};

C getC();

int main() {
    getC() = getC();
    return 0;
}

@danmar
Copy link
Copy Markdown
Collaborator Author

danmar commented Aug 6, 2024

For your information, I want that we use cppcheck premium for a self check. And activate Misra C++ 2023. However there are many Misra C++ rules that don't feel "natural" for Cppcheck source code to follow and it's 100% fine to suppress those rules.

@chrchr-github
Copy link
Copy Markdown
Collaborator

I think it's OK to do this.
Apparently, built-in types are not susceptible to this issue, but only user-defined ones.

int f();
struct S {};
S g();
int main() {
    f() = f(); // error
    g() = g(); // compiles
}

@danmar danmar changed the title Use l-value reference specifier for assignment operators to prevent misuse Fix #12992 (Refactoring: Use l-value reference specifier for assignment operators to prevent misuse) Aug 7, 2024
@danmar danmar merged commit cf3373d into cppcheck-opensource:main Aug 7, 2024
@danmar danmar deleted the misracpp-15.0.2 branch August 7, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants