|
| 1 | +// RUN: %check_clang_tidy %s readability-math-missing-parentheses %t |
| 2 | + |
| 3 | +#define MACRO_AND & |
| 4 | +#define MACRO_ADD + |
| 5 | +#define MACRO_OR | |
| 6 | +#define MACRO_MULTIPLY * |
| 7 | +#define MACRO_XOR ^ |
| 8 | +#define MACRO_SUBTRACT - |
| 9 | +#define MACRO_DIVIDE / |
| 10 | + |
| 11 | +int foo(){ |
| 12 | + return 5; |
| 13 | +} |
| 14 | + |
| 15 | +int bar(){ |
| 16 | + return 4; |
| 17 | +} |
| 18 | + |
| 19 | +class fun{ |
| 20 | +public: |
| 21 | + int A; |
| 22 | + double B; |
| 23 | + fun(){ |
| 24 | + A = 5; |
| 25 | + B = 5.4; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +void f(){ |
| 30 | + //CHECK-MESSAGES: :[[@LINE+2]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 31 | + //CHECK-FIXES: int a = 1 + (2 * 3); |
| 32 | + int a = 1 + 2 * 3; |
| 33 | + |
| 34 | + int a_negative = 1 + (2 * 3); // No warning |
| 35 | + |
| 36 | + int b = 1 + 2 + 3; // No warning |
| 37 | + |
| 38 | + int c = 1 * 2 * 3; // No warning |
| 39 | + |
| 40 | + //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 41 | + //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 42 | + //CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5); |
| 43 | + int d = 1 + 2 * 3 - 4 / 5; |
| 44 | + |
| 45 | + int d_negative = 1 + (2 * 3) - (4 / 5); // No warning |
| 46 | + |
| 47 | + //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 48 | + //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 49 | + //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 50 | + //CHECK-FIXES: int e = (1 & (2 + 3)) | (4 * 5); |
| 51 | + int e = 1 & 2 + 3 | 4 * 5; |
| 52 | + |
| 53 | + int e_negative = (1 & (2 + 3)) | (4 * 5); // No warning |
| 54 | + |
| 55 | + //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 56 | + //CHECK-FIXES: int f = (1 * -2) + 4; |
| 57 | + int f = 1 * -2 + 4; |
| 58 | + |
| 59 | + int f_negative = (1 * -2) + 4; // No warning |
| 60 | + |
| 61 | + //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 62 | + //CHECK-FIXES: int g = (1 * 2 * 3) + 4 + 5; |
| 63 | + int g = 1 * 2 * 3 + 4 + 5; |
| 64 | + |
| 65 | + int g_negative = (1 * 2 * 3) + 4 + 5; // No warning |
| 66 | + |
| 67 | + //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 68 | + //CHECK-MESSAGES: :[[@LINE+3]]:19: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 69 | + //CHECK-MESSAGES: :[[@LINE+2]]:27: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 70 | + //CHECK-FIXES: int h = (120 & (2 + 3)) | (22 * 5); |
| 71 | + int h = 120 & 2 + 3 | 22 * 5; |
| 72 | + |
| 73 | + int h_negative = (120 & (2 + 3)) | (22 * 5); // No warning |
| 74 | + |
| 75 | + int i = 1 & 2 & 3; // No warning |
| 76 | + |
| 77 | + int j = 1 | 2 | 3; // No warning |
| 78 | + |
| 79 | + int k = 1 ^ 2 ^ 3; // No warning |
| 80 | + |
| 81 | + //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '+' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 82 | + //CHECK-FIXES: int l = (1 + 2) ^ 3; |
| 83 | + int l = 1 + 2 ^ 3; |
| 84 | + |
| 85 | + int l_negative = (1 + 2) ^ 3; // No warning |
| 86 | + |
| 87 | + //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 88 | + //CHECK-FIXES: int m = (2 * foo()) + bar(); |
| 89 | + int m = 2 * foo() + bar(); |
| 90 | + |
| 91 | + int m_negative = (2 * foo()) + bar(); // No warning |
| 92 | + |
| 93 | + //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 94 | + //CHECK-FIXES: int n = (1.05 * foo()) + double(bar()); |
| 95 | + int n = 1.05 * foo() + double(bar()); |
| 96 | + |
| 97 | + int n_negative = (1.05 * foo()) + double(bar()); // No warning |
| 98 | + |
| 99 | + //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 100 | + //CHECK-FIXES: int o = 1 + (obj.A * 3) + obj.B; |
| 101 | + fun obj; |
| 102 | + int o = 1 + obj.A * 3 + obj.B; |
| 103 | + |
| 104 | + int o_negative = 1 + (obj.A * 3) + obj.B; // No warning |
| 105 | + |
| 106 | + //CHECK-MESSAGES: :[[@LINE+2]]:18: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 107 | + //CHECK-FIXES: int p = 1U + (2 * 3); |
| 108 | + int p = 1U + 2 * 3; |
| 109 | + |
| 110 | + int p_negative = 1U + (2 * 3); // No warning |
| 111 | + |
| 112 | + //CHECK-MESSAGES: :[[@LINE+7]]:13: warning: '+' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 113 | + //CHECK-MESSAGES: :[[@LINE+6]]:25: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 114 | + //CHECK-MESSAGES: :[[@LINE+5]]:53: warning: '&' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 115 | + //CHECK-MESSAGES: :[[@LINE+4]]:53: warning: '^' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 116 | + //CHECK-MESSAGES: :[[@LINE+3]]:77: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 117 | + //CHECK-MESSAGES: :[[@LINE+2]]:94: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses] |
| 118 | + //CHECK-FIXES: int q = (1 MACRO_ADD (2 MACRO_MULTIPLY 3)) MACRO_OR ((4 MACRO_AND 5) MACRO_XOR (6 MACRO_SUBTRACT (7 MACRO_DIVIDE 8))); |
| 119 | + int q = 1 MACRO_ADD 2 MACRO_MULTIPLY 3 MACRO_OR 4 MACRO_AND 5 MACRO_XOR 6 MACRO_SUBTRACT 7 MACRO_DIVIDE 8; // No warning |
| 120 | +} |
0 commit comments