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

yaml-cpp/node/impl.h:206 error 'operator=' should return a reference to *this [-Werror=effc++] #970

Closed
tomeravi opened this issue Jan 4, 2021 · 14 comments · Fixed by #1124

Comments

@tomeravi
Copy link
Contributor

tomeravi commented Jan 4, 2021

on Linux build

@btbouwens
Copy link

btbouwens commented Jan 7, 2021

Indeed, I also see this:

In file included from /workspace/software/lib/3rdparty/yaml-cpp/src/parse.cpp:7:
/workspace/software/lib/3rdparty/yaml-cpp/include/yaml-cpp/node/impl.h: In member function 'YAML::Node& YAML::Node::operator=(const T&)':
/workspace/software/lib/3rdparty/yaml-cpp/include/yaml-cpp/node/impl.h:206:10: warning: 'operator=' should return a reference to '*this' [-Weffc++]
  206 |   return *this;
      |          ^~~~~

@tomeravi
Copy link
Contributor Author

tomeravi commented Jan 7, 2021 via email

@btbouwens
Copy link

No, I don't understand the problem. It looks OK to me actually.
I've seen comments that the -Weffc++ is actually questionable, but the arguments are outside my competence ...

@olafmandel
Copy link

I opened a GCC bug #98841 about this.

@olafmandel
Copy link

Jakub Jelinek wrote a fix for GCC for this, which "just" has to make it into a future release of GCC.

If I understand the explanation of the bug correctly, there is nothing that can be done to the yaml-cpp code to workaround the warning for older compilers, short of disabling the warning:

diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h
--- a/include/yaml-cpp/node/impl.h
+++ b/include/yaml-cpp/node/impl.h
@@ -209,7 +209,15 @@ inline Node& Node::operator=(const T& rhs) {
   if (!m_isValid)
     throw InvalidNode(m_invalidKey);
   Assign(rhs);
+// work around GCC PR c++/98841
+#if defined __GNUC__ && __GNUC__ < 11
+#  pragma GCC diagnostic push
+#  pragma GCC diagnostic ignored "-Weffc++"
+#endif
   return *this;
+#if defined __GNUC__ && __GNUC__ < 11
+#  pragma GCC diagnostic pop
+#endif
 }

 inline Node& Node::operator=(const Node& rhs) {

Of course, that distracts a lot when reading the code. Including this or not may also depend on when the fix gets released and how long it takes for the developer-base of yaml-cpp to upgrade...

@christianparpart
Copy link

Jakub Jelinek wrote a fix for GCC for this, which "just" has to make it into a future release of GCC.

If I understand the explanation of the bug correctly, there is nothing that can be done to the yaml-cpp code to workaround the warning for older compilers, short of disabling the warning:

diff --git a/include/yaml-cpp/node/impl.h b/include/yaml-cpp/node/impl.h
--- a/include/yaml-cpp/node/impl.h
+++ b/include/yaml-cpp/node/impl.h
@@ -209,7 +209,15 @@ inline Node& Node::operator=(const T& rhs) {
   if (!m_isValid)
     throw InvalidNode(m_invalidKey);
   Assign(rhs);
+// work around GCC PR c++/98841
+#if defined __GNUC__ && __GNUC__ < 11
+#  pragma GCC diagnostic push
+#  pragma GCC diagnostic ignored "-Weffc++"
+#endif
   return *this;
+#if defined __GNUC__ && __GNUC__ < 11
+#  pragma GCC diagnostic pop
+#endif
 }

 inline Node& Node::operator=(const Node& rhs) {

Of course, that distracts a lot when reading the code. Including this or not may also depend on when the fix gets released and how long it takes for the developer-base of yaml-cpp to upgrade...

Can we get this patch applied to the yaml-cpp repo, please? I'd like to be able to compile my own code without any warnings (-Werror) and can't because of this. :)
Maintaining custom patches per dependency is also cumbersome.

@jbeder
Copy link
Owner

jbeder commented Jul 10, 2021

Is there a PR to fix this? If so, I'll review it.

But it's totally not worth it to spam with a bunch of compiler-specific declarations just to fix a warning.

@jasonbeach
Copy link
Contributor

maybe one option is to remove the WeffC++ option when the project is not the top level project.
Added that to my PR.

@BMBurstein
Copy link
Contributor

Is there a solution for this? Our project compiles with -Wall -Wextra -Werror, and we were looking into adding yaml-cpp as a dependency, but this broke our build

@olafmandel
Copy link

Yes and no:

So the question for the project maintainers is:

  • do they want to fill the code with such preprocessor-macros
  • or do they want to remove the -Weffc++ compiler flag (in some situations)
  • or do they want to declare this wont-fix
  • or can this issue (eventually) be closed as no longer relevant

@BMBurstein
Copy link
Contributor

Our project is compiling on Ubuntu 20.04 (among others), which has GCC 9

@BMBurstein
Copy link
Contributor

I think a solution would be to disable all these extra warnings if this is being compiled as part of another project instead of as the main project. No need to block a project from using this library because of warnings

@christianparpart
Copy link

It may be just warnings (for whatever reason), but some projects build with all warnings enabled and then use -Werror error to force the dev to fix them. Now, when you use yaml-cpp, it's kinda hard, either you live in a strict world, or you can use yaml-cpp. Is there a solution to this?

@jbeder
Copy link
Owner

jbeder commented Aug 4, 2022

To answer questions from @olafmandel above:

do they want to fill the code with such preprocessor-macros

Ideally, no.

or do they want to remove the -Weffc++ compiler flag (in some situations)

Maybe? Depending on what that looks like.

or do they want to declare this wont-fix
or can this issue (eventually) be closed as no longer relevant

Fine with me. I don't particularly care about warnings, personally.

jbeder pushed a commit that referenced this issue Sep 20, 2022
Minimize warnings when not the top-level project

Should fix #970 and #764 when trying to add yaml-cpp to other project
davemccann pushed a commit to davemccann/yaml-cpp that referenced this issue Jul 30, 2023
Minimize warnings when not the top-level project

Should fix jbeder#970 and jbeder#764 when trying to add yaml-cpp to other project
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 a pull request may close this issue.

7 participants