Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Extend 'bugprone-easily-swappable-parameters' with mixab…
…ility because of implicit conversions Adds a relaxation option ModelImplicitConversions which will make the check report for cases where parameters refer to types that are implicitly convertible to one another. Example: struct IntBox { IntBox(int); operator int(); }; void foo(int i, double d, IntBox ib) {} Implicit conversions are the last to model in the set of things that are reasons for the possibility of a function being called the wrong way which is not always immediately apparent when looking at the function (signature or call). Reviewed By: aaron.ballman, martong Differential Revision: http://reviews.llvm.org/D75041
- Loading branch information
1 parent
961e9e6
commit e33d047
Showing
11 changed files
with
1,544 additions
and
104 deletions.
There are no files selected for viewing
1,158 changes: 1,065 additions & 93 deletions
1,158
clang-tools-extra/clang-tidy/bugprone/EasilySwappableParametersCheck.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
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
15 changes: 15 additions & 0 deletions
15
...tra/test/clang-tidy/checkers/bugprone-easily-swappable-parameters-implicit-qualifiers.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,15 @@ | ||
// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ | ||
// RUN: -config='{CheckOptions: [ \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.MinimumLength, value: 2}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.IgnoredParameterNames, value: ""}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes, value: ""}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.QualifiersMix, value: 1}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.ModelImplicitConversions, value: 1} \ | ||
// RUN: ]}' -- | ||
|
||
void numericAndQualifierConversion(int I, const double CD) { numericAndQualifierConversion(CD, I); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: 2 adjacent parameters of 'numericAndQualifierConversion' of convertible types are easily swapped by mistake [bugprone-easily-swappable-parameters] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:40: note: the first parameter in the range is 'I' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:56: note: the last parameter in the range is 'CD' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:43: note: 'int' and 'const double' parameters accept and bind the same kind of values | ||
// CHECK-MESSAGES: :[[@LINE-5]]:43: note: 'int' and 'const double' may be implicitly converted: 'int' -> 'const double' (as 'double'), 'const double' (as 'double') -> 'int' |
75 changes: 75 additions & 0 deletions
75
clang-tools-extra/test/clang-tidy/checkers/bugprone-easily-swappable-parameters-implicits.c
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,75 @@ | ||
// RUN: %check_clang_tidy %s bugprone-easily-swappable-parameters %t \ | ||
// RUN: -config='{CheckOptions: [ \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.MinimumLength, value: 2}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.IgnoredParameterNames, value: ""}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.IgnoredParameterTypeSuffixes, value: ""}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.QualifiersMix, value: 0}, \ | ||
// RUN: {key: bugprone-easily-swappable-parameters.ModelImplicitConversions, value: 1} \ | ||
// RUN: ]}' -- | ||
|
||
void implicitDoesntBreakOtherStuff(int A, int B) {} | ||
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: 2 adjacent parameters of 'implicitDoesntBreakOtherStuff' of similar type ('int') are easily swapped by mistake [bugprone-easily-swappable-parameters] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:40: note: the first parameter in the range is 'A' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:47: note: the last parameter in the range is 'B' | ||
|
||
void arrayAndPtr1(int *IP, int IA[]) { arrayAndPtr1(IA, IP); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 2 adjacent parameters of 'arrayAndPtr1' of similar type ('int *') | ||
// CHECK-MESSAGES: :[[@LINE-2]]:24: note: the first parameter in the range is 'IP' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:32: note: the last parameter in the range is 'IA' | ||
|
||
void arrayAndPtr2(int *IP, int IA[8]) { arrayAndPtr2(IA, IP); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 2 adjacent parameters of 'arrayAndPtr2' of similar type ('int *') | ||
// CHECK-MESSAGES: :[[@LINE-2]]:24: note: the first parameter in the range is 'IP' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:32: note: the last parameter in the range is 'IA' | ||
|
||
void arrayAndElement(int I, int IA[]) {} // NO-WARN. | ||
|
||
void numericConversion1(int I, double D) { numericConversion1(D, I); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion1' of convertible types are easily swapped by mistake [bugprone-easily-swappable-parameters] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:29: note: the first parameter in the range is 'I' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:39: note: the last parameter in the range is 'D' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:32: note: 'int' and 'double' may be implicitly converted{{$}} | ||
|
||
void numericConversion2(int I, short S) { numericConversion2(S, I); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion2' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:29: note: the first parameter in the range is 'I' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:38: note: the last parameter in the range is 'S' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:32: note: 'int' and 'short' may be implicitly converted{{$}} | ||
|
||
void numericConversion3(float F, unsigned long UL) { numericConversion3(UL, F); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion3' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:31: note: the first parameter in the range is 'F' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:48: note: the last parameter in the range is 'UL' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:34: note: 'float' and 'unsigned long' may be implicitly converted{{$}} | ||
|
||
enum Unscoped { U_A, | ||
U_B }; | ||
enum UnscopedFixed : char { UF_A, | ||
UF_B }; | ||
|
||
void numericConversion4(int I, enum Unscoped U) { numericConversion4(U, I); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion4' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:29: note: the first parameter in the range is 'I' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:46: note: the last parameter in the range is 'U' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:32: note: 'int' and 'enum Unscoped' may be implicitly converted{{$}} | ||
|
||
void numericConversion5(int I, enum UnscopedFixed UF) { numericConversion5(UF, I); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion5' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:29: note: the first parameter in the range is 'I' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:51: note: the last parameter in the range is 'UF' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:32: note: 'int' and 'enum UnscopedFixed' may be implicitly converted{{$}} | ||
|
||
void numericConversion7(double D, enum Unscoped U) { numericConversion7(U, D); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion7' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:32: note: the first parameter in the range is 'D' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:49: note: the last parameter in the range is 'U' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:35: note: 'double' and 'enum Unscoped' may be implicitly converted{{$}} | ||
|
||
void numericConversion8(double D, enum UnscopedFixed UF) { numericConversion8(UF, D); } | ||
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 2 adjacent parameters of 'numericConversion8' of convertible types | ||
// CHECK-MESSAGES: :[[@LINE-2]]:32: note: the first parameter in the range is 'D' | ||
// CHECK-MESSAGES: :[[@LINE-3]]:54: note: the last parameter in the range is 'UF' | ||
// CHECK-MESSAGES: :[[@LINE-4]]:35: note: 'double' and 'enum UnscopedFixed' may be implicitly converted{{$}} | ||
|
||
void pointeeConverison(int *IP, double *DP) { pointeeConversion(DP, IP); } | ||
// NO-WARN: Even though this is possible in C, a swap is diagnosed by the compiler. |
Oops, something went wrong.