From 6e6b294a3f237b86e960f92ffe9fa15d7b57c1ee Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 20 Oct 2016 21:14:40 -0700 Subject: [PATCH] lint: deal with "double negative" warnings --- .oclint | 7 +++++++ src/builtin_test.cpp | 6 +++--- src/color.h | 4 ++-- src/common.cpp | 10 +++++----- src/complete.cpp | 6 ++++-- src/expand.cpp | 4 ++-- src/highlight.cpp | 2 +- src/output.cpp | 5 +++-- src/parse_tree.h | 4 +++- src/proc.cpp | 2 +- src/reader.cpp | 10 +++++----- src/tokenizer.cpp | 8 ++++---- src/wildcard.cpp | 2 +- src/wutil.cpp | 2 +- 14 files changed, 42 insertions(+), 30 deletions(-) diff --git a/.oclint b/.oclint index 86f1f7402..5eea63aea 100644 --- a/.oclint +++ b/.oclint @@ -51,3 +51,10 @@ disable-rules: # and is therefore just noise. Disable this rule. # - InvertedLogic + # + # The idea behind the "double negative" rule is sound since constructs + # like "!!(var & flag)" should be written as "static_cast(var & + # flag)". Unfortunately this rule has way too many false positives; + # especially in the context of assert statements. So disable this rule. + # + - DoubleNegative diff --git a/src/builtin_test.cpp b/src/builtin_test.cpp index 334cbfb50..3c45be8a3 100644 --- a/src/builtin_test.cpp +++ b/src/builtin_test.cpp @@ -595,14 +595,14 @@ bool combining_expression::evaluate(wcstring_list_t &errors) { switch (token) { case test_combine_and: case test_combine_or: { + assert(!subjects.empty()); + assert(combiners.size() + 1 == subjects.size()); + // One-element case. if (subjects.size() == 1) return subjects.at(0)->evaluate(errors); // Evaluate our lists, remembering that AND has higher precedence than OR. We can // visualize this as a sequence of OR expressions of AND expressions. - assert(combiners.size() + 1 == subjects.size()); - assert(!subjects.empty()); - size_t idx = 0, max = subjects.size(); bool or_result = false; while (idx < max) { diff --git a/src/color.h b/src/color.h index fd756d0a2..05f1fa930 100644 --- a/src/color.h +++ b/src/color.h @@ -96,7 +96,7 @@ class rgb_color_t { color24_t to_color24() const; /// Returns whether the color is bold. - bool is_bold() const { return !!(flags & flag_bold); } + bool is_bold() const { return static_cast(flags & flag_bold); } /// Set whether the color is bold. void set_bold(bool x) { @@ -107,7 +107,7 @@ class rgb_color_t { } /// Returns whether the color is underlined. - bool is_underline() const { return !!(flags & flag_underline); } + bool is_underline() const { return static_cast(flags & flag_underline); } /// Set whether the color is underlined. void set_underline(bool x) { diff --git a/src/common.cpp b/src/common.cpp index 5c9d40e0a..ceea4ba84 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -768,9 +768,9 @@ static void escape_string_internal(const wchar_t *orig_in, size_t in_len, wcstri assert(orig_in != NULL); const wchar_t *in = orig_in; - bool escape_all = !!(flags & ESCAPE_ALL); - bool no_quoted = !!(flags & ESCAPE_NO_QUOTED); - bool no_tilde = !!(flags & ESCAPE_NO_TILDE); + bool escape_all = static_cast(flags & ESCAPE_ALL); + bool no_quoted = static_cast(flags & ESCAPE_NO_QUOTED); + bool no_tilde = static_cast(flags & ESCAPE_NO_TILDE); int need_escape = 0; int need_complex_escape = 0; @@ -1117,8 +1117,8 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in wcstring result; result.reserve(input_len); - const bool unescape_special = !!(flags & UNESCAPE_SPECIAL); - const bool allow_incomplete = !!(flags & UNESCAPE_INCOMPLETE); + const bool unescape_special = static_cast(flags & UNESCAPE_SPECIAL); + const bool allow_incomplete = static_cast(flags & UNESCAPE_INCOMPLETE); int bracket_count = 0; diff --git a/src/complete.cpp b/src/complete.cpp index 9491c58b8..e4aac9939 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -289,9 +289,11 @@ class completer_t { return flags & COMPLETION_REQUEST_AUTOSUGGESTION ? COMPLETE_AUTOSUGGEST : COMPLETE_DEFAULT; } - bool wants_descriptions() const { return !!(flags & COMPLETION_REQUEST_DESCRIPTIONS); } + bool wants_descriptions() const { + return static_cast(flags & COMPLETION_REQUEST_DESCRIPTIONS); + } - bool fuzzy() const { return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH); } + bool fuzzy() const { return static_cast(flags & COMPLETION_REQUEST_FUZZY_MATCH); } fuzzy_match_type_t max_fuzzy_match_type() const { // If we are doing fuzzy matching, request all types; if not request only prefix matching. diff --git a/src/expand.cpp b/src/expand.cpp index 1c2a92eb1..040216c16 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -1401,8 +1401,8 @@ static expand_error_t expand_stage_wildcards(const wcstring &input, std::vector< // which may be CDPATH if the special flag is set. const wcstring working_dir = env_get_pwd_slash(); wcstring_list_t effective_working_dirs; - bool for_cd = !!(flags & EXPAND_SPECIAL_FOR_CD); - bool for_command = !!(flags & EXPAND_SPECIAL_FOR_COMMAND); + bool for_cd = static_cast(flags & EXPAND_SPECIAL_FOR_CD); + bool for_command = static_cast(flags & EXPAND_SPECIAL_FOR_COMMAND); if (!for_cd && !for_command) { // Common case. effective_working_dirs.push_back(working_dir); diff --git a/src/highlight.cpp b/src/highlight.cpp index 21b75bce3..08ca3ada0 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -87,7 +87,7 @@ bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_l path_flags_t flags) { ASSERT_IS_BACKGROUND_THREAD(); - const bool require_dir = !!(flags & PATH_REQUIRE_DIR); + const bool require_dir = static_cast(flags & PATH_REQUIRE_DIR); wcstring clean_potential_path_fragment; int has_magic = 0; bool result = false; diff --git a/src/output.cpp b/src/output.cpp index 9243730d4..6287b4329 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -115,7 +115,8 @@ static bool write_background_color(unsigned char idx) { // Exported for builtin_set_color's usage only. bool write_color(rgb_color_t color, bool is_fg) { - bool supports_term24bit = !!(output_get_color_support() & color_support_term24bit); + bool supports_term24bit = + static_cast(output_get_color_support() & color_support_term24bit); if (!supports_term24bit || !color.is_rgb()) { // Indexed or non-24 bit color. unsigned char idx = index_for_color(color); @@ -386,7 +387,7 @@ rgb_color_t best_color(const std::vector &candidates, color_support } // If we have both RGB and named colors, then prefer rgb if term256 is supported. rgb_color_t result = rgb_color_t::none(); - bool has_term256 = !!(support & color_support_term256); + bool has_term256 = static_cast(support & color_support_term256); if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) { result = first_rgb; } else { diff --git a/src/parse_tree.h b/src/parse_tree.h index a5245aca6..0582fc947 100644 --- a/src/parse_tree.h +++ b/src/parse_tree.h @@ -118,7 +118,9 @@ class parse_node_t { } /// Indicate if the node has comment nodes. - bool has_comments() const { return !!(this->flags & parse_node_flag_has_comments); } + bool has_comments() const { + return static_cast(this->flags & parse_node_flag_has_comments); + } /// Gets source for the node, or the empty string if it has no source. wcstring get_source(const wcstring &str) const { diff --git a/src/proc.cpp b/src/proc.cpp index a5199c71b..d3e7b733d 100644 --- a/src/proc.cpp +++ b/src/proc.cpp @@ -247,7 +247,7 @@ void job_set_flag(job_t *j, unsigned int flag, int set) { } } -int job_get_flag(const job_t *j, unsigned int flag) { return !!(j->flags & flag); } +int job_get_flag(const job_t *j, unsigned int flag) { return static_cast(j->flags & flag); } int job_signal(job_t *j, int signal) { pid_t my_pid = getpid(); diff --git a/src/reader.cpp b/src/reader.cpp index 947cd09bc..2b6b82ccb 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1027,9 +1027,9 @@ wcstring completion_apply_to_command_line(const wcstring &val_str, complete_flag const wcstring &command_line, size_t *inout_cursor_pos, bool append_only) { const wchar_t *val = val_str.c_str(); - bool add_space = !(flags & COMPLETE_NO_SPACE); - bool do_replace = !!(flags & COMPLETE_REPLACES_TOKEN); - bool do_escape = !(flags & COMPLETE_DONT_ESCAPE); + bool add_space = !static_cast(flags & COMPLETE_NO_SPACE); + bool do_replace = static_cast(flags & COMPLETE_REPLACES_TOKEN); + bool do_escape = !static_cast(flags & COMPLETE_DONT_ESCAPE); const size_t cursor_pos = *inout_cursor_pos; bool back_into_trailing_quote = false; @@ -1046,7 +1046,7 @@ wcstring completion_apply_to_command_line(const wcstring &val_str, complete_flag if (do_escape) { // Respect COMPLETE_DONT_ESCAPE_TILDES. - bool no_tilde = !!(flags & COMPLETE_DONT_ESCAPE_TILDES); + bool no_tilde = static_cast(flags & COMPLETE_DONT_ESCAPE_TILDES); wcstring escaped = escape(val, ESCAPE_ALL | ESCAPE_NO_QUOTED | (no_tilde ? ESCAPE_NO_TILDE : 0)); sb.append(escaped); @@ -1426,7 +1426,7 @@ static bool handle_completions(const std::vector &comp, if (el.match.type > best_match_type) continue; // Only use completions that match replace_token. - bool completion_replace_token = !!(el.flags & COMPLETE_REPLACES_TOKEN); + bool completion_replace_token = static_cast(el.flags & COMPLETE_REPLACES_TOKEN); if (completion_replace_token != will_replace_token) continue; // Don't use completions that want to replace, if we cannot replace them. diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index cc19615c3..61ea9e349 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -64,10 +64,10 @@ tokenizer_t::tokenizer_t(const wchar_t *b, tok_flags_t flags) continue_line_after_comment(false) { assert(b != NULL); - this->accept_unfinished = !!(flags & TOK_ACCEPT_UNFINISHED); - this->show_comments = !!(flags & TOK_SHOW_COMMENTS); - this->squash_errors = !!(flags & TOK_SQUASH_ERRORS); - this->show_blank_lines = !!(flags & TOK_SHOW_BLANK_LINES); + this->accept_unfinished = static_cast(flags & TOK_ACCEPT_UNFINISHED); + this->show_comments = static_cast(flags & TOK_SHOW_COMMENTS); + this->squash_errors = static_cast(flags & TOK_SQUASH_ERRORS); + this->show_blank_lines = static_cast(flags & TOK_SHOW_BLANK_LINES); this->has_next = (*b != L'\0'); this->tok_next(); diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 5d57ead80..50468c706 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -502,7 +502,7 @@ class wildcard_expander_t { void add_expansion_result(const wcstring &result) { // This function is only for the non-completions case. - assert(!(this->flags & EXPAND_FOR_COMPLETIONS)); + assert(!static_cast(this->flags & EXPAND_FOR_COMPLETIONS)); if (this->completion_set.insert(result).second) { append_completion(this->resolved_completions, result); this->did_add = true; diff --git a/src/wutil.cpp b/src/wutil.cpp index 608caf7f8..b8897de3b 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -79,7 +79,7 @@ bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &ou if (stat(fullpath.c_str(), &buf) != 0) { is_dir = false; } else { - is_dir = !!(S_ISDIR(buf.st_mode)); + is_dir = static_cast(S_ISDIR(buf.st_mode)); } } *out_is_dir = is_dir;