Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 7, 2024

Conversation

danmar
Copy link
Owner

@danmar danmar commented Aug 6, 2024

Misra C++ 2023 15.0.2

@danmar
Copy link
Owner 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
Owner 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
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 danmar:main Aug 7, 2024
63 checks passed
@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