-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Improve bugprone-branch-clone with support for fallthrou…
…gh attribute Ignore duplicated switch cases with [[fallthrough]] attribute to reduce false positives. Fixes: #47588 Reviewed By: donat.nagy Differential Revision: https://reviews.llvm.org/D147889
- Loading branch information
Showing
4 changed files
with
148 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
clang-tools-extra/test/clang-tidy/checkers/bugprone/branch-clone-fallthrough.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// RUN: %check_clang_tidy %s bugprone-branch-clone %t -- -- -std=c++17 | ||
|
||
void handle(int); | ||
|
||
void testSwitchFallthroughAttribute(int value) { | ||
switch(value) { | ||
case 1: [[fallthrough]]; | ||
case 2: [[fallthrough]]; | ||
case 3: | ||
handle(value); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void testSwitchFallthroughAttributeAndBraces(int value) { | ||
switch(value) { | ||
case 1: { [[fallthrough]]; } | ||
case 2: { [[fallthrough]]; } | ||
case 3: { | ||
handle(value); | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void testSwitchWithFallthroughAttributeAndCode(int value) { | ||
switch(value) { | ||
case 1: value += 1; [[fallthrough]]; | ||
case 2: value += 1; [[fallthrough]]; | ||
case 3: | ||
handle(value); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void testSwitchWithFallthroughAndCode(int value) { | ||
switch(value) { | ||
// CHECK-MESSAGES: :[[@LINE+1]]:5: warning: switch has 2 consecutive identical branches [bugprone-branch-clone] | ||
case 1: value += 1; | ||
case 2: value += 1; | ||
// CHECK-MESSAGES: :[[@LINE-1]]:23: note: last of these clones ends here | ||
case 3: | ||
handle(value); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void testSwitchFallthroughAttributeIntoDefault(int value) { | ||
switch(value) { | ||
case 1: [[fallthrough]]; | ||
case 2: [[fallthrough]]; | ||
default: | ||
handle(value); | ||
break; | ||
} | ||
} |