Skip to content

Commit 2ace512

Browse files
authored
Merge pull request #12896 from Mic92/no-dangling-reference
Fix -Wdangling-reference
2 parents 33e638d + 0f72376 commit 2ace512

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/libstore/derivations.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,13 +1228,13 @@ DerivationOutput DerivationOutput::fromJSON(
12281228
keys.insert(key);
12291229

12301230
auto methodAlgo = [&]() -> std::pair<ContentAddressMethod, HashAlgorithm> {
1231-
auto & method_ = getString(valueAt(json, "method"));
1232-
ContentAddressMethod method = ContentAddressMethod::parse(method_);
1231+
ContentAddressMethod method = ContentAddressMethod::parse(
1232+
getString(valueAt(json, "method")));
12331233
if (method == ContentAddressMethod::Raw::Text)
12341234
xpSettings.require(Xp::DynamicDerivations);
12351235

1236-
auto & hashAlgo_ = getString(valueAt(json, "hashAlgo"));
1237-
auto hashAlgo = parseHashAlgo(hashAlgo_);
1236+
auto hashAlgo = parseHashAlgo(
1237+
getString(valueAt(json, "hashAlgo")));
12381238
return { std::move(method), std::move(hashAlgo) };
12391239
};
12401240

@@ -1351,7 +1351,8 @@ Derivation Derivation::fromJSON(
13511351
res.name = getString(valueAt(json, "name"));
13521352

13531353
try {
1354-
for (auto & [outputName, output] : getObject(valueAt(json, "outputs"))) {
1354+
auto outputs = getObject(valueAt(json, "outputs"));
1355+
for (auto & [outputName, output] : outputs) {
13551356
res.outputs.insert_or_assign(
13561357
outputName,
13571358
DerivationOutput::fromJSON(store, res.name, outputName, output));
@@ -1362,7 +1363,8 @@ Derivation Derivation::fromJSON(
13621363
}
13631364

13641365
try {
1365-
for (auto & input : getArray(valueAt(json, "inputSrcs")))
1366+
auto inputSrcs = getArray(valueAt(json, "inputSrcs"));
1367+
for (auto & input : inputSrcs)
13661368
res.inputSrcs.insert(store.parseStorePath(static_cast<const std::string &>(input)));
13671369
} catch (Error & e) {
13681370
e.addTrace({}, "while reading key 'inputSrcs'");
@@ -1375,13 +1377,15 @@ Derivation Derivation::fromJSON(
13751377
auto & json = getObject(_json);
13761378
DerivedPathMap<StringSet>::ChildNode node;
13771379
node.value = getStringSet(valueAt(json, "outputs"));
1378-
for (auto & [outputId, childNode] : getObject(valueAt(json, "dynamicOutputs"))) {
1380+
auto drvs = getObject(valueAt(json, "dynamicOutputs"));
1381+
for (auto & [outputId, childNode] : drvs) {
13791382
xpSettings.require(Xp::DynamicDerivations);
13801383
node.childMap[outputId] = doInput(childNode);
13811384
}
13821385
return node;
13831386
};
1384-
for (auto & [inputDrvPath, inputOutputs] : getObject(valueAt(json, "inputDrvs")))
1387+
auto drvs = getObject(valueAt(json, "inputDrvs"));
1388+
for (auto & [inputDrvPath, inputOutputs] : drvs)
13851389
res.inputDrvs.map[store.parseStorePath(inputDrvPath)] =
13861390
doInput(inputOutputs);
13871391
} catch (Error & e) {

src/libutil-tests/json-utils.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ TEST(valueAt, simpleObject) {
6363

6464
auto nested = R"({ "hello": { "world": "" } })"_json;
6565

66-
auto & nestedObject = valueAt(getObject(nested), "hello");
67-
68-
ASSERT_EQ(valueAt(nestedObject, "world"), "");
66+
ASSERT_EQ(valueAt(valueAt(getObject(nested), "hello"), "world"), "");
6967
}
7068

7169
TEST(valueAt, missingKey) {
@@ -83,7 +81,7 @@ TEST(getObject, rightAssertions) {
8381

8482
auto nested = R"({ "object": { "object": {} } })"_json;
8583

86-
auto & nestedObject = getObject(valueAt(getObject(nested), "object"));
84+
auto nestedObject = getObject(valueAt(getObject(nested), "object"));
8785

8886
ASSERT_EQ(nestedObject, getObject(nlohmann::json::parse(R"({ "object": {} })")));
8987
ASSERT_EQ(getObject(valueAt(getObject(nestedObject), "object")), (nlohmann::json::object_t {}));

0 commit comments

Comments
 (0)