about summary refs log tree commit diff
path: root/third_party/nix/src/libutil
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-05-19T16·38+0100
committerVincent Ambo <tazjin@google.com>2020-05-19T16·38+0100
commitb490742a511dd03afc43f5143d6d61edaeeb8091 (patch)
tree727370e8e7607a3078c2ce1cd6d717105c7d9c44 /third_party/nix/src/libutil
parentc758de9d22506eb279c5abe61f621e5c8f61af95 (diff)
style(3p/nix): Enforce braces around loops and conditionals r/767
This change was generated with:

  fd -e cc -e hh | xargs -I{} clang-tidy {} -p ~/projects/nix-build/ \
    --checks='-*,readability-braces-around-statements' --fix \
    -fix-errors

Some manual fixes were applied because some convoluted unbraced
statements couldn't be untangled by clang-tidy.

This commit still includes invalid files, but I decided to clean them
up in a subsequent commit so that it becomes more obvious where
clang-tidy failed. Maybe this will allow for a bug-report to
clang-tidy.
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r--third_party/nix/src/libutil/affinity.cc4
-rw-r--r--third_party/nix/src/libutil/hash.cc53
-rw-r--r--third_party/nix/src/libutil/json.cc12
-rw-r--r--third_party/nix/src/libutil/serialise.cc4
-rw-r--r--third_party/nix/src/libutil/util.cc12
-rw-r--r--third_party/nix/src/libutil/xml-writer.cc8
6 files changed, 66 insertions, 27 deletions
diff --git a/third_party/nix/src/libutil/affinity.cc b/third_party/nix/src/libutil/affinity.cc
index b6aedc77e54a..7db8906eb0b4 100644
--- a/third_party/nix/src/libutil/affinity.cc
+++ b/third_party/nix/src/libutil/affinity.cc
@@ -36,7 +36,9 @@ void setAffinityTo(int cpu) {
 int lockToCurrentCPU() {
 #if __linux__
   int cpu = sched_getcpu();
-  if (cpu != -1) setAffinityTo(cpu);
+  if (cpu != -1) {
+    setAffinityTo(cpu);
+  }
   return cpu;
 #else
   return -1;
diff --git a/third_party/nix/src/libutil/hash.cc b/third_party/nix/src/libutil/hash.cc
index 18b3c89795e8..d303a4f5b97d 100644
--- a/third_party/nix/src/libutil/hash.cc
+++ b/third_party/nix/src/libutil/hash.cc
@@ -16,35 +16,49 @@
 namespace nix {
 
 void Hash::init() {
-  if (type == htMD5)
+  if (type == htMD5) {
     hashSize = md5HashSize;
-  else if (type == htSHA1)
+  } else if (type == htSHA1) {
     hashSize = sha1HashSize;
-  else if (type == htSHA256)
+  } else if (type == htSHA256) {
     hashSize = sha256HashSize;
-  else if (type == htSHA512)
+  } else if (type == htSHA512) {
     hashSize = sha512HashSize;
-  else
+  } else {
     abort();
+  }
   assert(hashSize <= maxHashSize);
   memset(hash, 0, maxHashSize);
 }
 
 bool Hash::operator==(const Hash& h2) const {
-  if (hashSize != h2.hashSize) return false;
-  for (unsigned int i = 0; i < hashSize; i++)
-    if (hash[i] != h2.hash[i]) return false;
+  if (hashSize != h2.hashSize) {
+    return false;
+  }
+  for (unsigned int i = 0; i < hashSize; i++) {
+    if (hash[i] != h2.hash[i]) {
+      return false;
+    }
+  }
   return true;
 }
 
 bool Hash::operator!=(const Hash& h2) const { return !(*this == h2); }
 
 bool Hash::operator<(const Hash& h) const {
-  if (hashSize < h.hashSize) return true;
-  if (hashSize > h.hashSize) return false;
+  if (hashSize < h.hashSize) {
+    return true;
+  }
+  if (hashSize > h.hashSize) {
+    return false;
+  }
   for (unsigned int i = 0; i < hashSize; i++) {
-    if (hash[i] < h.hash[i]) return true;
-    if (hash[i] > h.hash[i]) return false;
+    if (hash[i] < h.hash[i]) {
+      return true;
+    }
+    if (hash[i] > h.hash[i]) {
+      return false;
+    }
   }
   return false;
 }
@@ -137,9 +151,15 @@ Hash::Hash(const std::string& s, HashType type) : type(type) {
 
   if (!isSRI && size == base16Len()) {
     auto parseHexDigit = [&](char c) {
-      if (c >= '0' && c <= '9') return c - '0';
-      if (c >= 'A' && c <= 'F') return c - 'A' + 10;
-      if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+      if (c >= '0' && c <= '9') {
+        return c - '0';
+      }
+      if (c >= 'A' && c <= 'F') {
+        return c - 'A' + 10;
+      }
+      if (c >= 'a' && c <= 'f') {
+        return c - 'a' + 10;
+      }
       throw BadHash("invalid base-16 hash '%s'", s);
     };
 
@@ -292,8 +312,9 @@ HashResult hashPath(HashType ht, const Path& path, PathFilter& filter) {
 Hash compressHash(const Hash& hash, unsigned int newSize) {
   Hash h;
   h.hashSize = newSize;
-  for (unsigned int i = 0; i < hash.hashSize; ++i)
+  for (unsigned int i = 0; i < hash.hashSize; ++i) {
     h.hash[i % newSize] ^= hash.hash[i];
+  }
   return h;
 }
 
diff --git a/third_party/nix/src/libutil/json.cc b/third_party/nix/src/libutil/json.cc
index 3e5608c582fa..8746ca9a6947 100644
--- a/third_party/nix/src/libutil/json.cc
+++ b/third_party/nix/src/libutil/json.cc
@@ -102,7 +102,9 @@ void JSONWriter::comma() {
   } else {
     state->str << ',';
   }
-  if (state->indent) indent();
+  if (state->indent) {
+    indent();
+  }
 }
 
 void JSONWriter::indent() {
@@ -116,7 +118,9 @@ void JSONList::open() {
 
 JSONList::~JSONList() {
   state->depth--;
-  if (state->indent && !first) indent();
+  if (state->indent && !first) {
+    indent();
+  }
   state->str << "]";
 }
 
@@ -143,7 +147,9 @@ void JSONObject::open() {
 JSONObject::~JSONObject() {
   if (state) {
     state->depth--;
-    if (state->indent && !first) indent();
+    if (state->indent && !first) {
+      indent();
+    }
     state->str << "}";
   }
 }
diff --git a/third_party/nix/src/libutil/serialise.cc b/third_party/nix/src/libutil/serialise.cc
index 187642df66fc..079b88c1cfc4 100644
--- a/third_party/nix/src/libutil/serialise.cc
+++ b/third_party/nix/src/libutil/serialise.cc
@@ -211,7 +211,9 @@ Sink& operator<<(Sink& sink, const string& s) {
 template <class T>
 void writeStrings(const T& ss, Sink& sink) {
   sink << ss.size();
-  for (auto& i : ss) sink << i;
+  for (auto& i : ss) {
+    sink << i;
+  }
 }
 
 Sink& operator<<(Sink& sink, const Strings& s) {
diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc
index 941680b70a70..1740d1f256d0 100644
--- a/third_party/nix/src/libutil/util.cc
+++ b/third_party/nix/src/libutil/util.cc
@@ -194,7 +194,9 @@ bool pathExists(const Path& path) {
   int res;
   struct stat st;
   res = lstat(path.c_str(), &st);
-  if (!res) return true;
+  if (!res) {
+    return true;
+  }
   if (errno != ENOENT && errno != ENOTDIR)
     throw SysError(format("getting status of %1%") % path);
   return false;
@@ -579,9 +581,9 @@ AutoDelete::AutoDelete(const string& p, bool recursive) : path(p) {
 AutoDelete::~AutoDelete() {
   try {
     if (del) {
-      if (recursive)
+      if (recursive) {
         deletePath(path);
-      else {
+      } else {
         if (remove(path.c_str()) == -1)
           throw SysError(format("cannot unlink '%1%'") % path);
       }
@@ -1200,7 +1202,9 @@ string base64Decode(const string& s) {
   if (!init) {
     // FIXME: not thread-safe.
     memset(decode, -1, sizeof(decode));
-    for (int i = 0; i < 64; i++) decode[(int)base64Chars[i]] = i;
+    for (int i = 0; i < 64; i++) {
+      decode[(int)base64Chars[i]] = i;
+    }
     init = true;
   }
 
diff --git a/third_party/nix/src/libutil/xml-writer.cc b/third_party/nix/src/libutil/xml-writer.cc
index 77e63395f224..8bc4e187779f 100644
--- a/third_party/nix/src/libutil/xml-writer.cc
+++ b/third_party/nix/src/libutil/xml-writer.cc
@@ -13,13 +13,17 @@ XMLWriter::XMLWriter(bool indent, std::ostream& output)
 XMLWriter::~XMLWriter() { close(); }
 
 void XMLWriter::close() {
-  if (closed) return;
+  if (closed) {
+    return;
+  }
   while (!pendingElems.empty()) closeElement();
   closed = true;
 }
 
 void XMLWriter::indent_(size_t depth) {
-  if (!indent) return;
+  if (!indent) {
+    return;
+  }
   output << string(depth * 2, ' ');
 }