From c6a95395b57e6925d683339c969fa43d2f6b48e3 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 20 Dec 2017 14:59:51 +0100
Subject: [PATCH] tag/Tag: Merge() returns std::unique_ptr<Tag>

---
 src/decoder/Bridge.cxx       | 20 +++++++-------------
 src/input/IcyInputStream.cxx |  2 +-
 src/tag/Tag.cxx              |  8 ++++----
 src/tag/Tag.hxx              |  5 +++--
 4 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/src/decoder/Bridge.cxx b/src/decoder/Bridge.cxx
index b4793c757..857fd32c1 100644
--- a/src/decoder/Bridge.cxx
+++ b/src/decoder/Bridge.cxx
@@ -450,13 +450,11 @@ DecoderBridge::SubmitData(InputStream *is,
 	/* send stream tags */
 
 	if (UpdateStreamTag(is)) {
-		if (decoder_tag != nullptr) {
+		if (decoder_tag != nullptr)
 			/* merge with tag from decoder plugin */
-			Tag *tag = Tag::Merge(*decoder_tag,
-					      *stream_tag);
-			cmd = DoSendTag(*tag);
-			delete tag;
-		} else
+			cmd = DoSendTag(*Tag::Merge(*decoder_tag,
+						    *stream_tag));
+		else
 			/* send only the stream tag */
 			cmd = DoSendTag(*stream_tag);
 
@@ -559,14 +557,10 @@ DecoderBridge::SubmitTag(InputStream *is, Tag &&tag)
 
 	/* send tag to music pipe */
 
-	if (stream_tag != nullptr) {
+	if (stream_tag != nullptr)
 		/* merge with tag from input stream */
-		Tag *merged;
-
-		merged = Tag::Merge(*stream_tag, *decoder_tag);
-		cmd = DoSendTag(*merged);
-		delete merged;
-	} else
+		cmd = DoSendTag(*Tag::Merge(*stream_tag, *decoder_tag));
+	else
 		/* send only the decoder tag */
 		cmd = DoSendTag(*decoder_tag);
 
diff --git a/src/input/IcyInputStream.cxx b/src/input/IcyInputStream.cxx
index 238b32c02..df9cbbb49 100644
--- a/src/input/IcyInputStream.cxx
+++ b/src/input/IcyInputStream.cxx
@@ -75,7 +75,7 @@ IcyInputStream::ReadTag()
 	if (icy_tag == nullptr)
 		return new Tag(*input_tag);
 
-	return Tag::Merge(*input_tag, *icy_tag);
+	return Tag::Merge(*input_tag, *icy_tag).release();
 }
 
 size_t
diff --git a/src/tag/Tag.cxx b/src/tag/Tag.cxx
index 054872096..7fba27b09 100644
--- a/src/tag/Tag.cxx
+++ b/src/tag/Tag.cxx
@@ -55,12 +55,12 @@ Tag::Tag(const Tag &other)
 	}
 }
 
-Tag *
-Tag::Merge(const Tag &base, const Tag &add)
+std::unique_ptr<Tag>
+Tag::Merge(const Tag &base, const Tag &add) noexcept
 {
 	TagBuilder builder(add);
 	builder.Complement(base);
-	return builder.CommitNew().release();
+	return builder.CommitNew();
 }
 
 Tag *
@@ -72,7 +72,7 @@ Tag::MergeReplace(Tag *base, Tag *add)
 	if (base == nullptr)
 		return add;
 
-	Tag *tag = Merge(*base, *add);
+	Tag *tag = Merge(*base, *add).release();
 	delete base;
 	delete add;
 
diff --git a/src/tag/Tag.hxx b/src/tag/Tag.hxx
index 0696cbab9..d5cc3c030 100644
--- a/src/tag/Tag.hxx
+++ b/src/tag/Tag.hxx
@@ -26,6 +26,7 @@
 #include "Compiler.h"
 
 #include <algorithm>
+#include <memory>
 
 /**
  * The meta information about a song file.  It is a MPD specific
@@ -116,8 +117,8 @@ struct Tag {
 	 *
 	 * @return a newly allocated tag
 	 */
-	gcc_malloc gcc_returns_nonnull
-	static Tag *Merge(const Tag &base, const Tag &add);
+	static std::unique_ptr<Tag> Merge(const Tag &base,
+					  const Tag &add) noexcept;
 
 	/**
 	 * Merges the data from two tags.  Any of the two may be nullptr.  Both