mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Fix CI errors due to missing whitespace around operators (#1037)
Also rephrase comments to avoid overlong lines.
This commit is contained in:
@@ -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`.
|
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`.
|
A numeric value is compared to `0` and a pointer value to `nullptr`.
|
||||||
|
|
||||||
if (p) { ... } // means "if `p` is not `nullptr`, good
|
// These all mean "if `p` is not `nullptr`"
|
||||||
if (p!=0) { ... } // means "if `p` is not `nullptr`, redundant `!=0`; bad: don't use 0 for pointers
|
if (p) { ... } // good
|
||||||
if (p!=nullptr) { ... } // means "if `p` is not `nullptr`, redundant `!=nullptr`, not recommended
|
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,
|
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
|
##### Example
|
||||||
|
|
||||||
@@ -11180,14 +11181,14 @@ This rule is especially useful when a declaration is used as a condition
|
|||||||
|
|
||||||
if (auto pc = dynamic_cast<Circle>(ps)) { ... } // execute is ps points to a kind of Circle, good
|
if (auto pc = dynamic_cast<Circle>(ps)) { ... } // execute is ps points to a kind of Circle, good
|
||||||
|
|
||||||
if (auto pc = dynamic_cast<Circle>(ps); pc!=nullptr) { ... } // not recommended
|
if (auto pc = dynamic_cast<Circle>(ps); pc != nullptr) { ... } // not recommended
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
Note that implicit conversions to bool are applied in conditions.
|
Note that implicit conversions to bool are applied in conditions.
|
||||||
For example:
|
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()`.
|
This invokes `istream`'s `operator bool()`.
|
||||||
|
|
||||||
@@ -11195,13 +11196,13 @@ This invokes `istream`'s `operator bool()`.
|
|||||||
|
|
||||||
It has been noted that
|
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.
|
is a common beginners error.
|
||||||
If you use C-style strings, you must know the `<cstring>` functions well.
|
If you use C-style strings, you must know the `<cstring>` functions well.
|
||||||
Being verbose and writing
|
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.
|
would not save you.
|
||||||
|
|
||||||
@@ -11209,9 +11210,10 @@ would not save you.
|
|||||||
|
|
||||||
The opposite condition is most easily expressed using a negation:
|
The opposite condition is most easily expressed using a negation:
|
||||||
|
|
||||||
if (!p) { ... } // means "if `p` is`nullptr`, good
|
// These all mean "if `p` is `nullptr`"
|
||||||
if (p==0) { ... } // means "if `p` is `nullptr`, redundant `!=0`; bad: don't use `0` for pointers
|
if (!p) { ... } // good
|
||||||
if (p==nullptr) { ... } // means "if `p` is `nullptr`, redundant `==nullptr`, not recommended
|
if (p == 0) { ... } // redundant `!= 0`; bad: don't use `0` for pointers
|
||||||
|
if (p == nullptr) { ... } // redundant `== nullptr`, not recommended
|
||||||
|
|
||||||
##### Enforcement
|
##### Enforcement
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user