mat007 / turtle

C++ mock object library for Boost

Home Page:http://turtle.sourceforge.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clang throws unused variable warnings

Flamefire opened this issue · comments

Using clang 3.5

contrib/turtle/include/turtle/detail/function_impl_template.hpp:66:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:83:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:199:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:206:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:215:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:251:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( mutex_ );
                 ^
contrib/turtle/include/turtle/detail/function_impl_template.hpp:261:18: error: unused variable '_' [-Werror,-Wunused-variable]
            lock _( impl.mutex_ );

That seems indeed a little inconvenient, what is the usual way to deal with an RAII guard like this with clang?
I suppose there is a better way than just disabling that warning for the whole code base, isn't there?

Maybe template <class T> inline void ignore_unused_variable_warning(T const&) {}

Yes, that rings a bell, there is even something in Boost for this : https://www.boost.org/doc/libs/1_68_0/libs/core/doc/html/core/ignore_unused.html

Yeah. But as this is such a simple thing I'd like you to avoid this quite recent addition. E.g. again we are stuck with boost 1.55 ATM

That seems indeed a little inconvenient, what is the usual way to deal with an RAII guard like this with clang?

From what I found clang "knows" when ctors have side effects so it doesn't warn for such scoped locks. However for the non-thread-safe variant, the ctor of lock class is empty so that instance does in fact not do anything at all. Hence the warning.

I'd like to fix this. Would you rather like to add the ignore_unused_variable_warning function or use a macro #define MOCK_SCOPED_LOCK(mutex) lock _( mutex ); which is empty for non-threadsafe builds?

I'd go with the macro otherwise all users of the library would have to add ignore_unused_variable_warning themselves…
Thanks!

Done. Seems like this is only for older clangs as your CI clangs do not show this behaviour. I also could not remove the dummy lock class as it is used as a member in a class. Besides that the lock/unlock from the dummy mutex cannot be removed either due to https://github.com/mat007/turtle/blob/master/include/turtle/detail/functor.hpp#L40

Thank you!