diff options
author | Gabor Horvath <xazax.hun@gmail.com> | 2016-02-11 09:23:33 +0000 |
---|---|---|
committer | Gabor Horvath <xazax.hun@gmail.com> | 2016-02-11 09:23:33 +0000 |
commit | 8b6434e56e77a2a60138ffb0085a298b4f38ea1b (patch) | |
tree | 615840584bb7c2da8a312675d0997fff19b14939 /clang-tools-extra/docs/clang-tidy | |
parent | 6e2edc4b84352c126eab02196c3002ab71381808 (diff) | |
download | bcm5719-llvm-8b6434e56e77a2a60138ffb0085a298b4f38ea1b.tar.gz bcm5719-llvm-8b6434e56e77a2a60138ffb0085a298b4f38ea1b.zip |
[clang-tidy] Add a check to find unintended semicolons that changes the semantics.
Reviewers: hokein, alexfh
Differential Revision: http://reviews.llvm.org/D16535
llvm-svn: 260503
Diffstat (limited to 'clang-tools-extra/docs/clang-tidy')
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/list.rst | 1 | ||||
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/checks/misc-suspicious-semicolon.rst | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 5bdc7163d1a..fe1a0225e34 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -61,6 +61,7 @@ Clang-Tidy Checks misc-sizeof-container misc-static-assert misc-string-integer-assignment + misc-suspicious-semicolon misc-swapped-arguments misc-throw-by-value-catch-by-reference misc-undelegated-constructor diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-suspicious-semicolon.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-suspicious-semicolon.rst new file mode 100644 index 00000000000..b58fed68d1e --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/misc-suspicious-semicolon.rst @@ -0,0 +1,72 @@ +.. title:: clang-tidy - misc-suspicious-semicolon + +misc-suspicious-semicolon +========================= + +Finds most instances of stray semicolons that unexpectedly alter the meaning of +the code. More specifically, it looks for `if`, `while`, `for` and `for-range` +statements whose body is a single semicolon, and then analyzes the context of +the code (e.g. indentation) in an attempt to determine whether that is +intentional. + + .. code-block:: c++ + + if(x < y); + { + x++; + } + +Here the body of the `if` statement consists of only the semicolon at the end of +the first line, and `x` will be incremented regardless of the condition. + + + .. code-block:: c++ + + while((line = readLine(file)) != NULL); + processLine(line); + +As a result of this code, `processLine()` will only be called once, when the +`while` loop with the empty body exits with `line == NULL`. The indentation of +the code indicates the intention of the programmer. + + + .. code-block:: c++ + + if(x >= y); + x -= y; + +While the indentation does not imply any nesting, there is simply no valid +reason to have an `if` statement with an empty body (but it can make sense for +a loop). So this check issues a warning for the code above. + +To solve the issue remove the stray semicolon or in case the empty body is +intentional, reflect this using code indentation or put the semicolon in a new +line. For example: + + .. code-block:: c++ + + while(readWhitespace()); + Token t = readNextToken(); + +Here the second line is indented in a way that suggests that it is meant to be +the body of the `while` loop - whose body is in fact empty, becasue of the +semicolon at the end of the first line. + +Either remove the indentation from the second line: + + .. code-block:: c++ + + while(readWhitespace()); + Token t = readNextToken(); + +... or move the semicolon from the end of the first line to a new line: + + .. code-block:: c++ + + while(readWhitespace()) + ; + + Token t = readNextToken(); + +In this case the check will assume that you know what you are doing, and will +not raise a warning. |