Skip to content

Instantly share code, notes, and snippets.

@lucasmeijer
Last active November 11, 2015 14:54
Show Gist options
  • Save lucasmeijer/90697ea8a6d2e43f092a to your computer and use it in GitHub Desktop.
Save lucasmeijer/90697ea8a6d2e43f092a to your computer and use it in GitHub Desktop.
//Who can get any microsoft vc++ compiler to emit any warning for any of the horrific code below?
//apparently not me!
//
//Bye, Lucas (@lucasmeijer)
bool boolA, boolB, boolC;
enum MyEnum { kOne, kTwo };
MyEnum myenum;
enum MyOtherEnum { kOtherOne, kOtherTwo };
MyOtherEnum myotherenum;
//a constant float that isn't used anywhere. please warn me! (-Wunused-const-variable)
const float unused_const = 3;
//encourage me to use { {1,2}, {3,4} } to make it harder to screw up. -Wmissing-braces
int my_array[2][2] = { 1,2,3,4 };
struct StructWithTWoMembers
{
int a,b;
};
struct MyBase
{
virtual void func() {}
};
struct MyChild : public MyBase
{
//overriding a base function without specifying 'override', please warn me! (-Winconsistent-missing-override)
virtual void func() {}
private:
//completely unused member, please let me know! (-Wunused-private-field)
int* unused_private_member;
};
int main()
{
MyChild m;
m.func();
switch (myenum)
{
case kOne: return 1;
//kTwo is missing here. please warn me! (-Wswitch)
}
//comparing two enums that have different types. warn me plz! (-Wenum-compare)
if (myenum == myotherenum)
return 1;
//easy to misinterpet / misread / miswrite, please tell me to add braces around boolA && boolB. (-Wlogical-op-parentheses)
if (boolA && boolB || boolC)
return 1;
//dangling-else problem: to which if statement does the else statement belong. force the dev to be explicit! (-Wdangling-else)
if (boolA)
if (boolB)
return 3;
else
return 4;
//this expression is always false, an unsigned int can never be -3. also 9 can never be -3 either.
unsigned int unsigned_int = 9;
if (unsigned_int == -3)
return 3;
//a statement that does nothing. (-Wunused-value)
(unsigned_int < 102);
//assigning to itself. -Wself-assign
boolA = boolA;
//taking a non const char* to a string literal (-Wwritable-strings)
char* c = "myliteral";
(void)c;
//only initialize one member of a struct that has two. (-Wmissing-field-intializers)
StructWithTwoMembers swtm = { 2 };
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment