Skip to content

Incorrect -Wunused-lambda-capture #56920

Open
@mjaffaj

Description

@mjaffaj

The following code causes -Wunused-lambda-capture on Clang 14.
https://godbolt.org/z/nna4q3jxo

#include <algorithm>
#include <memory>
#include <vector>

class Foo
{
public:
	void reset() {}
};

template <class T>
class Bar
{
public:
	using const_iterator = typename std::vector<T>::const_iterator;

	void reset()
	{
		std::for_each(
				_v.begin(),
				_v.end(),
				/// Changing any one of the following does not cause -Wunused-lambda-capture
				/// [ this] to [ = ], [ & ]
				/// toReference(v).reset to this->toReference(v).reset()
				/// (auto& v) to (T& v)
				/// Removing member function: Foo& toReference(const_iterator it) const
				[ this ](auto& v){ toReference(v).reset(); }
		);
	}

private:
	Foo& toReference(const_iterator it) const
	{
		return toReference(*it);
	}

	Foo& toReference(const T& object) const
	{
		if constexpr (std::is_pointer<decltype(object.get())>::value)
		{
			return *object.get();
		}
		else
		{
			return object.get();
		}
	}

	std::vector<T> _v;
};

int main()
{
	Bar<std::unique_ptr<Foo>> myBar;
	myBar.reset();
	return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++14clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerconfirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions