Skip to content

Commit f1f3fff

Browse files
committed
fable: Simplify several if-else statements
This was also a clang-tidy finding.
1 parent 3126b8f commit f1f3fff

File tree

6 files changed

+16
-19
lines changed

6 files changed

+16
-19
lines changed

fable/include/fable/environment.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,8 @@ class Environment {
134134
inline std::string interpolate_vars(const std::string& s, const Environment* env = nullptr) {
135135
if (env != nullptr) {
136136
return env->interpolate(s);
137-
} else {
138-
return Environment().interpolate(s);
139137
}
138+
return Environment().interpolate(s);
140139
}
141140

142141
} // namespace fable

fable/include/fable/schema/array.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ class Array : public Base<Array<T, N, P>> {
179179
return true;
180180
}
181181

182-
if (c->type() == JsonType::array) {
183-
return this->validate_array(c, err);
184-
} else if (c->type() == JsonType::object) {
185-
return this->validate_object(c, err);
186-
} else {
187-
return this->set_wrong_type(err, c);
182+
// If not require-all, then we can also support object notation.
183+
switch (c->type()) {
184+
case JsonType::array:
185+
return this->validate_array(c, err);
186+
case JsonType::object:
187+
return this->validate_object(c, err);
188+
default:
189+
return this->set_wrong_type(err, c);
188190
}
189191
}
190192

fable/include/fable/schema/factory.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class FactoryBase : public Base<CRTP> {
314314

315315
Conf args;
316316
if (args_subset_) {
317-
if (args_key_ != "") {
317+
if (!args_key_.empty()) {
318318
if (c.has(args_key_)) {
319319
args = c.at(args_key_);
320320
}
@@ -364,7 +364,7 @@ class FactoryBase : public Base<CRTP> {
364364
Struct base{
365365
{factory_key_, make_const_schema(kv.first, "name of factory").require()},
366366
};
367-
if (args_key_ == "") {
367+
if (args_key_.empty()) {
368368
base.set_properties_from(kv.second.schema);
369369
} else {
370370
base.set_property(args_key_, kv.second.schema.clone());

fable/include/fable/schema/interface.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,8 @@ class Base : public Interface {
422422
const auto* required = required_ ? "!" : "";
423423
if (desc_.empty()) {
424424
return type_string() + required;
425-
} else {
426-
return fmt::format("{}{} :: {}", type_string(), required, desc_);
427425
}
426+
return fmt::format("{}{} :: {}", type_string(), required, desc_);
428427
}
429428

430429
[[nodiscard]] bool is_required() const override { return required_; }

fable/include/fable/schema/optional.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ class Optional : public Base<Optional<T, P>> {
119119
}
120120

121121
[[nodiscard]] Json serialize(const Type& x) const {
122-
if (x) {
123-
return prototype_.serialize(x.value());
124-
} else {
122+
if (!x) {
125123
return nullptr;
126124
}
125+
return prototype_.serialize(x.value());
127126
}
128127

129128
[[nodiscard]] Type deserialize(const Conf& c) const {

fable/src/fable/conf.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ std::string Conf::resolve_file(const std::string& filepath) const {
172172
[[noreturn]] void Conf::throw_unexpected(const std::string& key, const std::string& msg) const {
173173
if (msg.empty()) {
174174
throw error::UnexpectedProperty(*this, key);
175-
} else {
176-
throw ConfError{*this, msg};
177175
}
176+
throw ConfError{*this, msg};
178177
}
179178

180179
[[noreturn]] void Conf::throw_missing(const std::string& key) const {
@@ -184,9 +183,8 @@ std::string Conf::resolve_file(const std::string& filepath) const {
184183
[[noreturn]] void Conf::throw_wrong_type(const std::string& key) const {
185184
if (key.empty()) {
186185
throw error::WrongType(*this);
187-
} else {
188-
throw error::WrongType(*this, key);
189186
}
187+
throw error::WrongType(*this, key);
190188
}
191189

192190
[[noreturn]] void Conf::throw_wrong_type(const std::string& key, JsonType type) const {

0 commit comments

Comments
 (0)