1// RUN: %check_clang_tidy %s readability-avoid-nested-conditional-operator %t
2
3int NestInConditional = (true ? true : false) ? 1 : 2;
4// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators
5// CHECK-MESSAGES: :[[@LINE-2]]:25: note: parent conditional operator here
6
7int NestInTrue = true ? (true ? 1 : 2) : 2;
8// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators
9// CHECK-MESSAGES: :[[@LINE-2]]:18: note: parent conditional operator here
10
11int NestInFalse = true ? 1 : true ? 1 : 2;
12// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators
13// CHECK-MESSAGES: :[[@LINE-2]]:19: note: parent conditional operator here
14int NestInFalse2 = true ? 1 : (true ? 1 : 2);
15// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators
16// CHECK-MESSAGES: :[[@LINE-2]]:20: note: parent conditional operator here
17
18int NestWithParensis = true ? 1 : ((((true ? 1 : 2))));
19// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: conditional operator is used as sub-expression of parent conditional operator, refrain from using nested conditional operators
20// CHECK-MESSAGES: :[[@LINE-2]]:24: note: parent conditional operator here
21
22#define CONDITIONAL_EXPR (true ? 1 : 2)
23// not diag for macro since it will not reduce readability
24int NestWithMacro = true ? CONDITIONAL_EXPR : 2;
25

source code of clang-tools-extra/test/clang-tidy/checkers/readability/avoid-nested-conditional-operator.cpp