From 13fe9e83fa31b23b7693001700c953889e418e98 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Tue, 8 Jun 2021 22:16:17 +0200 Subject: [PATCH] Use Clang 11 (#1846) --- .clang-tidy | 40 +++++++++++++++++++ esphome/components/mqtt/mqtt_client.cpp | 17 ++++---- .../sensor/mqtt_subscribe_sensor.cpp | 23 ++++++----- .../mqtt_subscribe_text_sensor.cpp | 6 +-- esphome/core/helpers.h | 2 +- script/clang-format | 6 +-- script/clang-tidy | 12 +++--- 7 files changed, 74 insertions(+), 32 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 5e486e6a0c..ceb51884a5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -4,14 +4,37 @@ Checks: >- -abseil-*, -android-*, -boost-*, + -bugprone-branch-clone, -bugprone-macro-parentheses, + -bugprone-narrowing-conversions, + -bugprone-reserved-identifier, + -bugprone-signed-char-misuse, + -bugprone-suspicious-include, + -bugprone-too-small-loop-variable, + -bugprone-unhandled-self-assignment, + -cert-dcl37-c, -cert-dcl50-cpp, + -cert-dcl51-cpp, -cert-err58-cpp, + -cert-oop54-cpp, + -cert-oop57-cpp, + -cert-str34-c, -clang-analyzer-core.CallAndMessage, + -clang-analyzer-deadcode.DeadStores, + -clang-analyzer-optin.*, -clang-analyzer-osx.*, -clang-analyzer-security.*, + -clang-diagnostic-fortify-source, + -clang-diagnostic-shadow-field, + -cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-avoid-goto, + -cppcoreguidelines-avoid-magic-numbers, + -cppcoreguidelines-avoid-non-const-global-variables, -cppcoreguidelines-c-copy-assignment-signature, + -cppcoreguidelines-init-variables, + -cppcoreguidelines-macro-usage, + -cppcoreguidelines-narrowing-conversions, + -cppcoreguidelines-non-private-member-variables-in-classes, -cppcoreguidelines-owning-memory, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -cppcoreguidelines-pro-bounds-constant-array-index, @@ -37,10 +60,16 @@ Checks: >- -google-runtime-int, -google-runtime-references, -hicpp-*, + -llvm-else-after-return, -llvm-header-guard, -llvm-include-order, + -llvm-qualified-auto, + -llvmlibc-*, + -misc-non-private-member-variables-in-classes, + -misc-no-recursion, -misc-unconventional-assign-operator, -misc-unused-parameters, + -modernize-avoid-c-arrays, -modernize-deprecated-headers, -modernize-pass-by-value, -modernize-pass-by-value, @@ -48,14 +77,25 @@ Checks: >- -modernize-use-auto, -modernize-use-default-member-init, -modernize-use-equals-default, + -modernize-use-trailing-return-type, -mpi-*, -objc-*, -performance-unnecessary-value-param, -readability-braces-around-statements, + -readability-const-return-type, + -readability-convert-member-functions-to-static, -readability-else-after-return, -readability-implicit-bool-conversion, + -readability-isolate-declaration, + -readability-magic-numbers, + -readability-make-member-function-const, -readability-named-parameter, + -readability-qualified-auto, + -readability-redundant-access-specifiers, -readability-redundant-member-init, + -readability-redundant-string-init, + -readability-uppercase-literal-suffix, + -readability-use-anyofallof, -warnings-as-errors, -zircon-* WarningsAsErrors: '*' diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index b8743fc142..3330956b52 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -565,15 +565,16 @@ MQTTMessageTrigger::MQTTMessageTrigger(const std::string &topic) : topic_(topic) void MQTTMessageTrigger::set_qos(uint8_t qos) { this->qos_ = qos; } void MQTTMessageTrigger::set_payload(const std::string &payload) { this->payload_ = payload; } void MQTTMessageTrigger::setup() { - global_mqtt_client->subscribe(this->topic_, - [this](const std::string &topic, const std::string &payload) { - if (this->payload_.has_value() && payload != *this->payload_) { - return; - } + global_mqtt_client->subscribe( + this->topic_, + [this](const std::string &topic, const std::string &payload) { + if (this->payload_.has_value() && payload != *this->payload_) { + return; + } - this->trigger(payload); - }, - this->qos_); + this->trigger(payload); + }, + this->qos_); } void MQTTMessageTrigger::dump_config() { ESP_LOGCONFIG(TAG, "MQTT Message Trigger:"); diff --git a/esphome/components/mqtt_subscribe/sensor/mqtt_subscribe_sensor.cpp b/esphome/components/mqtt_subscribe/sensor/mqtt_subscribe_sensor.cpp index 50977e98ca..f6ecb0df36 100644 --- a/esphome/components/mqtt_subscribe/sensor/mqtt_subscribe_sensor.cpp +++ b/esphome/components/mqtt_subscribe/sensor/mqtt_subscribe_sensor.cpp @@ -7,18 +7,19 @@ namespace mqtt_subscribe { static const char *TAG = "mqtt_subscribe.sensor"; void MQTTSubscribeSensor::setup() { - mqtt::global_mqtt_client->subscribe(this->topic_, - [this](const std::string &topic, std::string payload) { - auto val = parse_float(payload); - if (!val.has_value()) { - ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str()); - this->publish_state(NAN); - return; - } + mqtt::global_mqtt_client->subscribe( + this->topic_, + [this](const std::string &topic, std::string payload) { + auto val = parse_float(payload); + if (!val.has_value()) { + ESP_LOGW(TAG, "Can't convert '%s' to number!", payload.c_str()); + this->publish_state(NAN); + return; + } - this->publish_state(*val); - }, - this->qos_); + this->publish_state(*val); + }, + this->qos_); } float MQTTSubscribeSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; } diff --git a/esphome/components/mqtt_subscribe/text_sensor/mqtt_subscribe_text_sensor.cpp b/esphome/components/mqtt_subscribe/text_sensor/mqtt_subscribe_text_sensor.cpp index fdab5cf6d7..e7373c4b95 100644 --- a/esphome/components/mqtt_subscribe/text_sensor/mqtt_subscribe_text_sensor.cpp +++ b/esphome/components/mqtt_subscribe/text_sensor/mqtt_subscribe_text_sensor.cpp @@ -7,9 +7,9 @@ namespace mqtt_subscribe { static const char *TAG = "mqtt_subscribe.text_sensor"; void MQTTSubscribeTextSensor::setup() { - this->parent_->subscribe(this->topic_, - [this](const std::string &topic, std::string payload) { this->publish_state(payload); }, - this->qos_); + this->parent_->subscribe( + this->topic_, [this](const std::string &topic, std::string payload) { this->publish_state(payload); }, + this->qos_); } float MQTTSubscribeTextSensor::get_setup_priority() const { return setup_priority::AFTER_CONNECTION; } void MQTTSubscribeTextSensor::set_qos(uint8_t qos) { this->qos_ = qos; } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index d4ee109126..499ca8f832 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -95,7 +95,7 @@ float clamp(float val, float min, float max); float lerp(float completion, float start, float end); /// std::make_unique -template std::unique_ptr make_unique(Args &&... args) { +template std::unique_ptr make_unique(Args &&...args) { return std::unique_ptr(new T(std::forward(args)...)); } diff --git a/script/clang-format b/script/clang-format index e9c3692bb8..bb2b722e1c 100755 --- a/script/clang-format +++ b/script/clang-format @@ -31,7 +31,7 @@ def run_format(args, queue, lock): """Takes filenames out of queue and runs clang-tidy on them.""" while True: path = queue.get() - invocation = ['clang-format-7'] + invocation = ['clang-format-11'] if args.inplace: invocation.append('-i') invocation.append(path) @@ -69,12 +69,12 @@ def main(): args = parser.parse_args() try: - get_output('clang-format-7', '-version') + get_output('clang-format-11', '-version') except: print(""" Oops. It looks like clang-format is not installed. - Please check you can run "clang-format-7 -version" in your terminal and install + Please check you can run "clang-format-11 -version" in your terminal and install clang-format (v7) if necessary. Note you can also upload your code as a pull request on GitHub and see the CI check diff --git a/script/clang-tidy b/script/clang-tidy index 0bd1ef51fa..0bf17f9076 100755 --- a/script/clang-tidy +++ b/script/clang-tidy @@ -30,7 +30,7 @@ else: def run_tidy(args, tmpdir, queue, lock, failed_files): while True: path = queue.get() - invocation = ['clang-tidy-7', '-header-filter=^{}/.*'.format(re.escape(basepath))] + invocation = ['clang-tidy-11', '-header-filter=^{}/.*'.format(re.escape(basepath))] if tmpdir is not None: invocation.append('-export-fixes') # Get a temporary file. We immediately close the handle so clang-tidy can @@ -90,13 +90,13 @@ def main(): args = parser.parse_args() try: - get_output('clang-tidy-7', '-version') + get_output('clang-tidy-11', '-version') except: print(""" - Oops. It looks like clang-tidy is not installed. + Oops. It looks like clang-tidy-11 is not installed. - Please check you can run "clang-tidy-7 -version" in your terminal and install - clang-tidy (v7) if necessary. + Please check you can run "clang-tidy-11 -version" in your terminal and install + clang-tidy (v11) if necessary. Note you can also upload your code as a pull request on GitHub and see the CI check output to apply clang-tidy. @@ -163,7 +163,7 @@ def main(): if args.fix and failed_files: print('Applying fixes ...') try: - subprocess.call(['clang-apply-replacements-7', tmpdir]) + subprocess.call(['clang-apply-replacements-11', tmpdir]) except: print('Error applying fixes.\n', file=sys.stderr) raise