From 6153d69b2cbe694585c2911ec8aee939d3407384 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 2 Oct 2017 19:39:18 +0100 Subject: [PATCH] Fix CI errors due to missing whitespace around operators (#1037) Also rephrase comments to avoid overlong lines. --- CppCoreGuidelines.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index efec18c..72ebcd3 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -11167,12 +11167,13 @@ Helps make style consistent and conventional. By definition, a condition in an `if`-statement, `while`-statement, or a `for`-statement selects between `true` and `false`. A numeric value is compared to `0` and a pointer value to `nullptr`. - if (p) { ... } // means "if `p` is not `nullptr`, good - if (p!=0) { ... } // means "if `p` is not `nullptr`, redundant `!=0`; bad: don't use 0 for pointers - if (p!=nullptr) { ... } // means "if `p` is not `nullptr`, redundant `!=nullptr`, not recommended + // These all mean "if `p` is not `nullptr`" + if (p) { ... } // good + if (p != 0) { ... } // redundant `!=0`; bad: don't use 0 for pointers + if (p != nullptr) { ... } // redundant `!=nullptr`, not recommended Often, `if (p)` is read as "if `p` is valid" which is a direct expression of the programmers intent, -whereas `if (p!=nullptr)` would be a long-winded workaround. +whereas `if (p != nullptr)` would be a long-winded workaround. ##### Example @@ -11180,14 +11181,14 @@ This rule is especially useful when a declaration is used as a condition if (auto pc = dynamic_cast(ps)) { ... } // execute is ps points to a kind of Circle, good - if (auto pc = dynamic_cast(ps); pc!=nullptr) { ... } // not recommended + if (auto pc = dynamic_cast(ps); pc != nullptr) { ... } // not recommended ##### Example Note that implicit conversions to bool are applied in conditions. For example: - for (string s; cin>>s; ) v.push_back(s); + for (string s; cin >> s; ) v.push_back(s); This invokes `istream`'s `operator bool()`. @@ -11195,13 +11196,13 @@ This invokes `istream`'s `operator bool()`. It has been noted that - if(strcmp(p1,p2)) { ... } // are the two C-style strings equal? (mistake!) + if(strcmp(p1, p2)) { ... } // are the two C-style strings equal? (mistake!) is a common beginners error. If you use C-style strings, you must know the `` functions well. Being verbose and writing - if(strcmp(p1,p2)!=0) { ... } // are the two C-style strings equal? (mistake!) + if(strcmp(p1, p2) != 0) { ... } // are the two C-style strings equal? (mistake!) would not save you. @@ -11209,9 +11210,10 @@ would not save you. The opposite condition is most easily expressed using a negation: - if (!p) { ... } // means "if `p` is`nullptr`, good - if (p==0) { ... } // means "if `p` is `nullptr`, redundant `!=0`; bad: don't use `0` for pointers - if (p==nullptr) { ... } // means "if `p` is `nullptr`, redundant `==nullptr`, not recommended + // These all mean "if `p` is `nullptr`" + if (!p) { ... } // good + if (p == 0) { ... } // redundant `!= 0`; bad: don't use `0` for pointers + if (p == nullptr) { ... } // redundant `== nullptr`, not recommended ##### Enforcement