1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
// RUN: %check_clang_tidy %s readability-magic-numbers %t \
// RUN: -config='{CheckOptions: \
// RUN: [{key: readability-magic-numbers.IgnoredIntegerValues, value: "0;1;2;10;100;"}, \
// RUN: {key: readability-magic-numbers.IgnoredFloatingPointValues, value: "3.14;2.71828;9.81;10000.0;101.0;0x1.2p3"}, \
// RUN: {key: readability-magic-numbers.IgnoreBitFieldsWidths, value: false}, \
// RUN: {key: readability-magic-numbers.IgnorePowersOf2IntegerValues, value: true}, \
// RUN: {key: readability-magic-numbers.IgnoreTypeAliases, value: false}]}' \
// RUN: --
template <typename T, int V>
struct ValueBucket {
T value[V];
};
int BadGlobalInt = 5;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
int IntSquarer(int param) {
return param * param;
}
void BuggyFunction() {
int BadLocalInt = 6;
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
(void)IntSquarer(7);
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
int LocalArray[15];
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 15 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
for (int ii = 0; ii < 22; ++ii)
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 22 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
{
LocalArray[ii] = 3 * ii;
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
}
ValueBucket<int, 66> Bucket;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: 66 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
}
class TwoIntContainer {
public:
TwoIntContainer(int val) : anotherMember(val * val), yetAnotherMember(6), anotherConstant(val + val) {}
// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
int getValue() const;
private:
int oneMember = 9;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: 9 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
int anotherMember;
int yetAnotherMember;
const int oneConstant = 2;
const int anotherConstant;
};
int ValueArray[] = {3, 5, 0, 0, 0};
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
// CHECK-MESSAGES: :[[@LINE-2]]:24: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
float FloatPiVariable = 3.1415926535f;
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3.1415926535f is a magic number; consider replacing it with a named constant [readability-magic-numbers]
double DoublePiVariable = 6.283185307;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 6.283185307 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
float SomeFloats[] = {0.5, 0x1.2p4};
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 0.5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
// CHECK-MESSAGES: :[[@LINE-2]]:28: warning: 0x1.2p4 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
int getAnswer() {
if (ValueArray[0] < ValueArray[1])
return ValueArray[1];
return -3; // FILENOTFOUND
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
}
struct HardwareGateway {
unsigned int Some: 5;
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 5 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
unsigned int Bits: 7;
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 7 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
unsigned int: 6;
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: 6 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
unsigned int Flag: 1; // no warning since this is suppressed by IgnoredIntegerValues rule
unsigned int: 0; // no warning since this is suppressed by IgnoredIntegerValues rule
unsigned int Rest: 13;
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 13 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
//
unsigned int Another[3];
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: 3 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
};
using NumberInTypeAlias = ValueBucket<int, 25>;
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: 25 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
typedef ValueBucket<char, 243> NumberInTypedef;
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 243 is a magic number; consider replacing it with a named constant [readability-magic-numbers]
/*
* Clean code
*/
#define INT_MACRO 5
const int GoodGlobalIntConstant = 42;
constexpr int AlsoGoodGlobalIntConstant = 42;
int InitializedByMacro = INT_MACRO;
void SolidFunction() {
const int GoodLocalIntConstant = 43;
(void)IntSquarer(GoodLocalIntConstant);
int LocalArray[INT_MACRO];
ValueBucket<int, INT_MACRO> Bucket;
}
const int ConstValueArray[] = {7, 9};
const int ConstValueArray2D[2][2] = {{7, 9}, {13, 15}};
/*
* no warnings for ignored values (specified in the configuration above)
*/
int GrandfatheredIntegerValues[] = {0, 1, 2, 10, 100, -1, -10, -100, 65536};
float GrandfatheredFloatValues[] = {3.14f, 3.14, 2.71828, 2.71828f, -1.01E2, 1E4, 0x1.2p3};
/*
* no warnings for enums
*/
enum Smorgasbord {
STARTER,
ALPHA = 3,
BETA = 1 << 5,
};
const float FloatPiConstant = 3.1415926535f;
const double DoublePiConstant = 6.283185307;
const float Angles[] = {45.0f, 90.0f, 135.0f};
double DoubleZeroIsAccepted = 0.0;
float FloatZeroIsAccepted = 0.0f;
namespace geometry {
template <typename T>
struct Point {
T x;
T y;
explicit Point(T xval, T yval) noexcept : x{xval}, y{yval} {
}
};
template <typename T>
struct Dimension {
T x;
T y;
explicit Dimension(T xval, T yval) noexcept : x{xval}, y{yval} {
}
};
template <typename T>
struct Rectangle {
Point<T> origin;
Dimension<T> size;
T rotation; // angle of rotation around origin
Rectangle(Point<T> origin_, Dimension<T> size_, T rotation_ = 0) noexcept : origin{origin_}, size{size_}, rotation{rotation_} {
}
bool contains(Point<T> point) const;
};
} // namespace geometry
const geometry::Rectangle<double> mandelbrotCanvas{geometry::Point<double>{-2.5, -1}, geometry::Dimension<double>{3.5, 2}};
// Simulate the macro magic in Google Test internal headers.
class AssertionHelper {
public:
AssertionHelper(const char *Message, int LineNumber) : Message(Message), LineNumber(LineNumber) {}
private:
const char *Message;
int LineNumber;
};
#define ASSERTION_HELPER_AT(M, L) AssertionHelper(M, L)
#define ASSERTION_HELPER(M) ASSERTION_HELPER_AT(M, __LINE__)
void FunctionWithCompilerDefinedSymbol(void) {
ASSERTION_HELPER("here and now");
}
// Prove that integer literals introduced by the compiler are accepted silently.
extern int ConsumeString(const char *Input);
const char *SomeStrings[] = {"alpha", "beta", "gamma"};
int TestCheckerOverreach() {
int Total = 0;
for (const auto *Str : SomeStrings) {
Total += ConsumeString(Str);
}
return Total;
}
// Prove that using enumerations values don't produce warnings.
enum class Letter : unsigned {
A, B, C, D, E, F, G, H, I, J
};
template<Letter x> struct holder { Letter letter = x; };
template<Letter x> struct wrapper { using h_type = holder<x>; };
template struct wrapper<Letter::A>;
template struct wrapper<Letter::J>;
|