From c74eb81356ef0e202713111d621434e46edc27ea Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 22 Jul 2024 17:15:57 +0200 Subject: [PATCH] enable -Werror=suggest-override *accidentally* overriding a function is almost guaranteed to be an error. overriding a function without labeling it as such is merely bad style, but bad style that makes the code harder to understand. Change-Id: Ic0594f3d1604ab6b3c1a75cb5facc246effe45f0 --- meson.build | 1 + src/libexpr/json-to-value.cc | 28 +++++++++++---------- src/libstore/build/local-derivation-goal.cc | 2 +- src/libstore/daemon.cc | 2 +- src/libutil/serialise.hh | 8 +++--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/meson.build b/meson.build index 8ce06800b..0e74520ca 100644 --- a/meson.build +++ b/meson.build @@ -445,6 +445,7 @@ add_project_arguments( '-Werror=unused-result', '-Wdeprecated-copy', '-Wignored-qualifiers', + '-Werror=suggest-override', # Enable assertions in libstdc++ by default. Harmless on libc++. Benchmarked # at ~1% overhead in `nix search`. # diff --git a/src/libexpr/json-to-value.cc b/src/libexpr/json-to-value.cc index b69216a48..2309b6c97 100644 --- a/src/libexpr/json-to-value.cc +++ b/src/libexpr/json-to-value.cc @@ -82,28 +82,28 @@ class JSONSax : nlohmann::json_sax { public: JSONSax(EvalState & state, Value & v) : state(state), rs(new JSONState(&v)) {}; - bool null() + bool null() override { rs->value(state).mkNull(); rs->add(); return true; } - bool boolean(bool val) + bool boolean(bool val) override { rs->value(state).mkBool(val); rs->add(); return true; } - bool number_integer(number_integer_t val) + bool number_integer(number_integer_t val) override { rs->value(state).mkInt(val); rs->add(); return true; } - bool number_unsigned(number_unsigned_t val_) + bool number_unsigned(number_unsigned_t val_) override { if (val_ > std::numeric_limits::max()) { throw Error("unsigned json number %1% outside of Nix integer range", val_); @@ -114,14 +114,14 @@ public: return true; } - bool number_float(number_float_t val, const string_t & s) + bool number_float(number_float_t val, const string_t & s) override { rs->value(state).mkFloat(val); rs->add(); return true; } - bool string(string_t & val) + bool string(string_t & val) override { rs->value(state).mkString(val); rs->add(); @@ -129,7 +129,7 @@ public: } #if NLOHMANN_JSON_VERSION_MAJOR >= 3 && NLOHMANN_JSON_VERSION_MINOR >= 8 - bool binary(binary_t&) + bool binary(binary_t&) override { // This function ought to be unreachable assert(false); @@ -137,35 +137,37 @@ public: } #endif - bool start_object(std::size_t len) + bool start_object(std::size_t len) override { rs = std::make_unique(std::move(rs)); return true; } - bool key(string_t & name) + bool key(string_t & name) override { dynamic_cast(rs.get())->key(name, state); return true; } - bool end_object() { + bool end_object() override { rs = rs->resolve(state); rs->add(); return true; } - bool end_array() { + bool end_array() override { return end_object(); } - bool start_array(size_t len) { + bool start_array(size_t len) override { rs = std::make_unique(std::move(rs), len != std::numeric_limits::max() ? len : 128); return true; } - bool parse_error(std::size_t, const std::string&, const nlohmann::detail::exception& ex) { + bool + parse_error(std::size_t, const std::string &, const nlohmann::detail::exception & ex) override + { throw JSONParseError("%s", ex.what()); } }; diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 660512e49..7d1d339e8 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -976,7 +976,7 @@ bool LocalDerivationGoal::isAllowed(const DerivedPath & req) struct RestrictedStoreConfig : virtual LocalFSStoreConfig { using LocalFSStoreConfig::LocalFSStoreConfig; - const std::string name() { return "Restricted Store"; } + const std::string name() override { return "Restricted Store"; } }; /* A wrapper around LocalStore that only allows building/querying of diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 4162015d6..b09645f46 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -158,7 +158,7 @@ struct TunnelSink : Sink { Sink & to; TunnelSink(Sink & to) : to(to) { } - void operator () (std::string_view data) + void operator () (std::string_view data) override { to << STDERR_WRITE << data; } diff --git a/src/libutil/serialise.hh b/src/libutil/serialise.hh index 9b1892bbb..6c637bd35 100644 --- a/src/libutil/serialise.hh +++ b/src/libutil/serialise.hh @@ -211,7 +211,7 @@ struct TeeSink : Sink { Sink & sink1, & sink2; TeeSink(Sink & sink1, Sink & sink2) : sink1(sink1), sink2(sink2) { } - virtual void operator () (std::string_view data) + virtual void operator () (std::string_view data) override { sink1(data); sink2(data); @@ -228,7 +228,7 @@ struct TeeSource : Source Sink & sink; TeeSource(Source & orig, Sink & sink) : orig(orig), sink(sink) { } - size_t read(char * data, size_t len) + size_t read(char * data, size_t len) override { size_t n = orig.read(data, len); sink({data, n}); @@ -245,7 +245,7 @@ struct SizedSource : Source size_t remain; SizedSource(Source & orig, size_t size) : orig(orig), remain(size) { } - size_t read(char * data, size_t len) + size_t read(char * data, size_t len) override { if (this->remain <= 0) { throw EndOfFile("sized: unexpected end-of-file"); @@ -338,7 +338,7 @@ struct GeneratorSource : Source { GeneratorSource(Generator && g) : g(std::move(g)) {} - virtual size_t read(char * data, size_t len) + virtual size_t read(char * data, size_t len) override { // we explicitly do not poll the generator multiple times to fill the // buffer, only to produce some output at all. this is allowed by the