Skip to content

Commit

Permalink
lint: deal with "double negative" warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurtis Rader committed Oct 21, 2016
1 parent 00303ed commit 6e6b294
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .oclint
Expand Up @@ -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<bool>(var &
# flag)". Unfortunately this rule has way too many false positives;
# especially in the context of assert statements. So disable this rule.
#
- DoubleNegative
6 changes: 3 additions & 3 deletions src/builtin_test.cpp
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/color.h
Expand Up @@ -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<bool>(flags & flag_bold); }

/// Set whether the color is bold.
void set_bold(bool x) {
Expand All @@ -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<bool>(flags & flag_underline); }

/// Set whether the color is underlined.
void set_underline(bool x) {
Expand Down
10 changes: 5 additions & 5 deletions src/common.cpp
Expand Up @@ -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<bool>(flags & ESCAPE_ALL);
bool no_quoted = static_cast<bool>(flags & ESCAPE_NO_QUOTED);
bool no_tilde = static_cast<bool>(flags & ESCAPE_NO_TILDE);

int need_escape = 0;
int need_complex_escape = 0;
Expand Down Expand Up @@ -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<bool>(flags & UNESCAPE_SPECIAL);
const bool allow_incomplete = static_cast<bool>(flags & UNESCAPE_INCOMPLETE);

int bracket_count = 0;

Expand Down
6 changes: 4 additions & 2 deletions src/complete.cpp
Expand Up @@ -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<bool>(flags & COMPLETION_REQUEST_DESCRIPTIONS);
}

bool fuzzy() const { return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH); }
bool fuzzy() const { return static_cast<bool>(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.
Expand Down
4 changes: 2 additions & 2 deletions src/expand.cpp
Expand Up @@ -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<bool>(flags & EXPAND_SPECIAL_FOR_CD);
bool for_command = static_cast<bool>(flags & EXPAND_SPECIAL_FOR_COMMAND);
if (!for_cd && !for_command) {
// Common case.
effective_working_dirs.push_back(working_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/highlight.cpp
Expand Up @@ -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<bool>(flags & PATH_REQUIRE_DIR);
wcstring clean_potential_path_fragment;
int has_magic = 0;
bool result = false;
Expand Down
5 changes: 3 additions & 2 deletions src/output.cpp
Expand Up @@ -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<bool>(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);
Expand Down Expand Up @@ -386,7 +387,7 @@ rgb_color_t best_color(const std::vector<rgb_color_t> &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<bool>(support & color_support_term256);
if ((!first_rgb.is_none() && has_term256) || first_named.is_none()) {
result = first_rgb;
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/parse_tree.h
Expand Up @@ -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<bool>(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 {
Expand Down
2 changes: 1 addition & 1 deletion src/proc.cpp
Expand Up @@ -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<bool>(j->flags & flag); }

int job_signal(job_t *j, int signal) {
pid_t my_pid = getpid();
Expand Down
10 changes: 5 additions & 5 deletions src/reader.cpp
Expand Up @@ -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<bool>(flags & COMPLETE_NO_SPACE);
bool do_replace = static_cast<bool>(flags & COMPLETE_REPLACES_TOKEN);
bool do_escape = !static_cast<bool>(flags & COMPLETE_DONT_ESCAPE);

const size_t cursor_pos = *inout_cursor_pos;
bool back_into_trailing_quote = false;
Expand All @@ -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<bool>(flags & COMPLETE_DONT_ESCAPE_TILDES);
wcstring escaped =
escape(val, ESCAPE_ALL | ESCAPE_NO_QUOTED | (no_tilde ? ESCAPE_NO_TILDE : 0));
sb.append(escaped);
Expand Down Expand Up @@ -1426,7 +1426,7 @@ static bool handle_completions(const std::vector<completion_t> &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<bool>(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.
Expand Down
8 changes: 4 additions & 4 deletions src/tokenizer.cpp
Expand Up @@ -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<bool>(flags & TOK_ACCEPT_UNFINISHED);
this->show_comments = static_cast<bool>(flags & TOK_SHOW_COMMENTS);
this->squash_errors = static_cast<bool>(flags & TOK_SQUASH_ERRORS);
this->show_blank_lines = static_cast<bool>(flags & TOK_SHOW_BLANK_LINES);

this->has_next = (*b != L'\0');
this->tok_next();
Expand Down
2 changes: 1 addition & 1 deletion src/wildcard.cpp
Expand Up @@ -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<bool>(this->flags & EXPAND_FOR_COMPLETIONS));
if (this->completion_set.insert(result).second) {
append_completion(this->resolved_completions, result);
this->did_add = true;
Expand Down
2 changes: 1 addition & 1 deletion src/wutil.cpp
Expand Up @@ -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<bool>(S_ISDIR(buf.st_mode));
}
}
*out_is_dir = is_dir;
Expand Down

0 comments on commit 6e6b294

Please sign in to comment.