1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 Expr
const * ignoreParenImpCastAndComma(Expr
const * expr
) {
16 expr
= expr
->IgnoreParenImpCasts();
17 auto e
= dyn_cast
<BinaryOperator
>(expr
);
18 if (e
== nullptr || e
->getOpcode() != BO_Comma
) {
25 Expr
const * getSubExprOfLogicalNegation(Expr
const * expr
) {
26 auto e
= dyn_cast
<UnaryOperator
>(ignoreParenImpCastAndComma(expr
));
27 return e
== nullptr || e
->getOpcode() != UO_LNot
28 ? nullptr : e
->getSubExpr();
31 enum class Value
{ Unknown
, False
, True
};
33 Value
getValue(Expr
const * expr
) {
34 expr
= ignoreParenImpCastAndComma(expr
);
35 if (expr
->getType()->isBooleanType()) {
36 // Instead going via Expr::isCXX11ConstantExpr would turn up excatly one
37 // additional place in svx/source/dialog/framelinkarray.cxx
39 // const bool DIAG_DBL_CLIP_DEFAULT = false;
41 // ... = mxImpl.get() ? mxImpl->mbDiagDblClip : DIAG_DBL_CLIP_DEFAULT;
43 // where it is unclear whether it is not actually better to consider
44 // DIAG_DBL_CLIP_DEFAULT a tunable parameter (and thus not to simplify):
45 auto lit
= dyn_cast
<CXXBoolLiteralExpr
>(expr
);
47 return lit
->getValue() ? Value::True
: Value::False
;
50 return Value::Unknown
;
54 public RecursiveASTVisitor
<SimplifyBool
>, public loplugin::Plugin
57 explicit SimplifyBool(InstantiationData
const & data
): Plugin(data
) {}
61 bool VisitUnaryLNot(UnaryOperator
const * expr
);
63 bool VisitBinLT(BinaryOperator
const * expr
);
65 bool VisitBinGT(BinaryOperator
const * expr
);
67 bool VisitBinLE(BinaryOperator
const * expr
);
69 bool VisitBinGE(BinaryOperator
const * expr
);
71 bool VisitBinEQ(BinaryOperator
const * expr
);
73 bool VisitBinNE(BinaryOperator
const * expr
);
75 bool VisitConditionalOperator(ConditionalOperator
const * expr
);
78 void SimplifyBool::run() {
79 if (compiler
.getLangOpts().CPlusPlus
) {
80 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
84 bool SimplifyBool::VisitUnaryLNot(UnaryOperator
const * expr
) {
85 if (ignoreLocation(expr
)) {
88 auto e
= getSubExprOfLogicalNegation(expr
->getSubExpr());
92 /* hits for OSL_ENSURE(!b, ...);
94 DiagnosticsEngine::Warning,
95 ("double logical negation expression of the form '!!A' (with A of type"
96 " %0) can %select{logically|literally}1 be simplified as 'A'"),
98 << e->IgnoreImpCasts()->getType()
99 << e->IgnoreImpCasts()->getType()->isBooleanType()
100 << expr->getSourceRange();
105 bool SimplifyBool::VisitBinLT(BinaryOperator
const * expr
) {
106 if (ignoreLocation(expr
)) {
109 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
110 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
114 auto v1
= getValue(expr
->getLHS());
115 auto v2
= getValue(expr
->getRHS());
123 DiagnosticsEngine::Warning
,
124 ("less-than expression of the form 'A < false' (with A of type"
125 " %0) can logically be simplified as 'false'"),
127 << expr
->getLHS()->IgnoreImpCasts()->getType()
128 << expr
->getSourceRange();
132 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
135 DiagnosticsEngine::Warning
,
136 ("less-than expression of the form 'A < true' (with A"
137 " of type %0) can %select{logically|literally}1 be"
138 " simplified as '!A'"),
140 << expr
->getLHS()->IgnoreImpCasts()->getType()
141 << (expr
->getLHS()->IgnoreImpCasts()->getType()
143 << expr
->getSourceRange();
146 DiagnosticsEngine::Warning
,
147 ("less-than expression of the form '!A < true' (with A"
148 " of type %0) can %select{logically|literally}1 be"
149 " simplified as 'A'"),
151 << e
->IgnoreImpCasts()->getType()
152 << e
->IgnoreImpCasts()->getType()->isBooleanType()
153 << expr
->getSourceRange();
163 DiagnosticsEngine::Warning
,
164 ("less-than expression of the form 'false < A' (with A of type"
165 " %0) can %select{logically|literally}1 be simplified as 'A'"),
167 << expr
->getRHS()->IgnoreImpCasts()->getType()
168 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
169 << expr
->getSourceRange();
173 DiagnosticsEngine::Warning
,
174 ("less-than expression of the form 'false < false' can"
175 " literally be simplified as 'false'"),
177 << expr
->getSourceRange();
181 DiagnosticsEngine::Warning
,
182 ("less-than expression of the form 'false < true' can"
183 " literally be simplified as 'true'"),
185 << expr
->getSourceRange();
193 DiagnosticsEngine::Warning
,
194 ("less-than expression of the form 'true < A' (with A of type"
195 " %0) can logically be simplified as 'false'"),
197 << expr
->getRHS()->IgnoreImpCasts()->getType()
198 << expr
->getSourceRange();
202 DiagnosticsEngine::Warning
,
203 ("less-than expression of the form 'true < false' can"
204 " literally be simplified as 'false'"),
206 << expr
->getSourceRange();
210 DiagnosticsEngine::Warning
,
211 ("less-than expression of the form 'true < true' can"
212 " literally be simplified as 'false'"),
214 << expr
->getSourceRange();
222 bool SimplifyBool::VisitBinGT(BinaryOperator
const * expr
) {
223 if (ignoreLocation(expr
)) {
226 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
227 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
231 auto v1
= getValue(expr
->getLHS());
232 auto v2
= getValue(expr
->getRHS());
240 DiagnosticsEngine::Warning
,
241 ("greater-than expression of the form 'A > false' (with A of"
242 " type %0) can %select{logically|literally}1 be simplified as"
245 << expr
->getLHS()->IgnoreImpCasts()->getType()
246 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
247 << expr
->getSourceRange();
251 DiagnosticsEngine::Warning
,
252 ("greater-than expression of the form 'A > true' (with A of"
253 " type %0) can logically be simplified as 'false'"),
255 << expr
->getLHS()->IgnoreImpCasts()->getType()
256 << expr
->getSourceRange();
264 DiagnosticsEngine::Warning
,
265 ("greater-than expression of the form 'false > A' (with A of"
266 " type %0) can logically be simplified as 'false'"),
268 << expr
->getRHS()->IgnoreImpCasts()->getType()
269 << expr
->getSourceRange();
273 DiagnosticsEngine::Warning
,
274 ("greater-than expression of the form 'false > false' can"
275 " literally be simplified as 'false'"),
277 << expr
->getSourceRange();
281 DiagnosticsEngine::Warning
,
282 ("greater-than expression of the form 'false > true' can"
283 " literally be simplified as 'false'"),
285 << expr
->getSourceRange();
293 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
296 DiagnosticsEngine::Warning
,
297 ("greater-than expression of the form 'true > A' (with"
298 " A of type %0) can %select{logically|literally}1 be"
299 " simplified as '!A'"),
301 << expr
->getRHS()->IgnoreImpCasts()->getType()
302 << (expr
->getRHS()->IgnoreImpCasts()->getType()
304 << expr
->getSourceRange();
307 DiagnosticsEngine::Warning
,
308 ("greater-than expression of the form 'true > !A' (with"
309 " A of type %0) can %select{logically|literally}1 be"
310 " simplified as 'A'"),
312 << e
->IgnoreImpCasts()->getType()
313 << e
->IgnoreImpCasts()->getType()->isBooleanType()
314 << expr
->getSourceRange();
320 DiagnosticsEngine::Warning
,
321 ("greater-than expression of the form 'true > false' can"
322 " literally be simplified as 'true'"),
324 << expr
->getSourceRange();
328 DiagnosticsEngine::Warning
,
329 ("greater-than expression of the form 'true > true' can"
330 " literally be simplified as 'false'"),
332 << expr
->getSourceRange();
340 bool SimplifyBool::VisitBinLE(BinaryOperator
const * expr
) {
341 if (ignoreLocation(expr
)) {
344 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
345 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
349 auto v1
= getValue(expr
->getLHS());
350 auto v2
= getValue(expr
->getRHS());
358 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
361 DiagnosticsEngine::Warning
,
362 ("less-than-or-equal-to expression of the form 'A <="
363 " false' (with A of type %0) can"
364 " %select{logically|literally}1 be simplified as"
367 << expr
->getLHS()->IgnoreImpCasts()->getType()
368 << (expr
->getLHS()->IgnoreImpCasts()->getType()
370 << expr
->getSourceRange();
373 DiagnosticsEngine::Warning
,
374 ("less-than-or-equal-to expression of the form '!A <="
375 " false' (with A of type %0) can"
376 " %select{logically|literally}1 be simplified as 'A'"),
378 << e
->IgnoreImpCasts()->getType()
379 << e
->IgnoreImpCasts()->getType()->isBooleanType()
380 << expr
->getSourceRange();
386 DiagnosticsEngine::Warning
,
387 ("less-than-or-equal-to expression of the form 'A <= true'"
388 " (with A of type %0) can logically be simplified as 'true'"),
390 << expr
->getLHS()->IgnoreImpCasts()->getType()
391 << expr
->getSourceRange();
399 DiagnosticsEngine::Warning
,
400 ("less-than-or-equal-to expression of the form 'false <= A'"
401 " (with A of type %0) can logically be simplified as 'true'"),
403 << expr
->getRHS()->IgnoreImpCasts()->getType()
404 << expr
->getSourceRange();
408 DiagnosticsEngine::Warning
,
409 ("less-than-or-equal-to expression of the form 'false <= false'"
410 " can literally be simplified as 'true'"),
412 << expr
->getSourceRange();
416 DiagnosticsEngine::Warning
,
417 ("less-than-or-equal-to expression of the form 'false <= true'"
418 " can literally be simplified as 'true'"),
420 << expr
->getSourceRange();
428 DiagnosticsEngine::Warning
,
429 ("less-than-or-equal-to expression of the form 'true <= A'"
430 " (with A of type %0) can %select{logically|literally}1 be"
431 " simplified as 'A'"),
433 << expr
->getRHS()->IgnoreImpCasts()->getType()
434 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
435 << expr
->getSourceRange();
439 DiagnosticsEngine::Warning
,
440 ("less-than-or-equal-to expression of the form 'true <= false'"
441 " can literally be simplified as 'false'"),
443 << expr
->getSourceRange();
447 DiagnosticsEngine::Warning
,
448 ("less-than-or-equal-to expression of the form 'true <= true'"
449 " can literally be simplified as 'true'"),
451 << expr
->getSourceRange();
459 bool SimplifyBool::VisitBinGE(BinaryOperator
const * expr
) {
460 if (ignoreLocation(expr
)) {
463 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
464 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
468 auto v1
= getValue(expr
->getLHS());
469 auto v2
= getValue(expr
->getRHS());
477 DiagnosticsEngine::Warning
,
478 ("greater-than-or-equal-to expression of the form 'A >= false'"
479 " (with A of type %0) can logically be simplified as 'true'"),
481 << expr
->getLHS()->IgnoreImpCasts()->getType()
482 << expr
->getSourceRange();
486 DiagnosticsEngine::Warning
,
487 ("greater-than-or-equal-to expression of the form 'A >= true'"
488 " (with A of type %0) can %select{logically|literally}1 be"
489 " simplified as 'A'"),
491 << expr
->getLHS()->IgnoreImpCasts()->getType()
492 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
493 << expr
->getSourceRange();
501 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
504 DiagnosticsEngine::Warning
,
505 ("greater-than-or-equal-to expression of the form"
506 " 'false >= A' (with A of type %0) can"
507 " %select{logically|literally}1 be simplified as"
510 << expr
->getRHS()->IgnoreImpCasts()->getType()
511 << (expr
->getRHS()->IgnoreImpCasts()->getType()
513 << expr
->getSourceRange();
516 DiagnosticsEngine::Warning
,
517 ("greater-than-or-equal-to expression of the form"
518 " 'false >= !A' (with A of type %0) can"
519 " %select{logically|literally}1 be simplified as 'A'"),
521 << e
->IgnoreImpCasts()->getType()
522 << e
->IgnoreImpCasts()->getType()->isBooleanType()
523 << expr
->getSourceRange();
529 DiagnosticsEngine::Warning
,
530 ("greater-than-or-equal-to expression of the form 'false >="
531 " false' can literally be simplified as 'true'"),
533 << expr
->getSourceRange();
537 DiagnosticsEngine::Warning
,
538 ("greater-than-or-equal-to expression of the form 'false >="
539 " true' can literally be simplified as 'false'"),
541 << expr
->getSourceRange();
549 DiagnosticsEngine::Warning
,
550 ("greater-than-or-equal-to expression of the form 'true >= A'"
551 " (with A of type %0) can logically be simplified as 'true'"),
553 << expr
->getRHS()->IgnoreImpCasts()->getType()
554 << expr
->getSourceRange();
558 DiagnosticsEngine::Warning
,
559 ("greater-than-or-equal-to expression of the form 'true >="
560 " false' can literally be simplified as 'true'"),
562 << expr
->getSourceRange();
566 DiagnosticsEngine::Warning
,
567 ("greater-than-or-equal-to expression of the form 'true >="
568 " true' can literally be simplified as 'true'"),
570 << expr
->getSourceRange();
578 bool SimplifyBool::VisitBinEQ(BinaryOperator
const * expr
) {
579 if (ignoreLocation(expr
)) {
582 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
583 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
587 auto v1
= getValue(expr
->getLHS());
588 auto v2
= getValue(expr
->getRHS());
596 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
599 DiagnosticsEngine::Warning
,
600 ("equal-to expression of the form 'A == false' (with A"
601 " of type %0) can %select{logically|literally}1 be"
602 " simplified as '!A'"),
604 << expr
->getLHS()->IgnoreImpCasts()->getType()
605 << (expr
->getLHS()->IgnoreImpCasts()->getType()
607 << expr
->getSourceRange();
610 DiagnosticsEngine::Warning
,
611 ("equal-to expression of the form '!A == false' (with A"
612 " of type %0) can %select{logically|literally}1 be"
613 " simplified as 'A'"),
615 << e
->IgnoreImpCasts()->getType()
616 << e
->IgnoreImpCasts()->getType()->isBooleanType()
617 << expr
->getSourceRange();
623 DiagnosticsEngine::Warning
,
624 ("equal-to expression of the form 'A == true' (with A of type"
625 " %0) can %select{logically|literally}1 be simplified as 'A'"),
627 << expr
->getLHS()->IgnoreImpCasts()->getType()
628 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
629 << expr
->getSourceRange();
637 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
640 DiagnosticsEngine::Warning
,
641 ("equal-to expression of the form 'false == A' (with A"
642 " of type %0) can %select{logically|literally}1 be"
643 " simplified as '!A'"),
645 << expr
->getRHS()->IgnoreImpCasts()->getType()
646 << (expr
->getRHS()->IgnoreImpCasts()->getType()
648 << expr
->getSourceRange();
651 DiagnosticsEngine::Warning
,
652 ("equal-to expression of the form 'false == !A' (with A"
653 " of type %0) can %select{logically|literally}1 be"
654 " simplified as 'A'"),
656 << e
->IgnoreImpCasts()->getType()
657 << e
->IgnoreImpCasts()->getType()->isBooleanType()
658 << expr
->getSourceRange();
664 DiagnosticsEngine::Warning
,
665 ("equal-to expression of the form 'false == false' can"
666 " literally be simplified as 'true'"),
668 << expr
->getSourceRange();
672 DiagnosticsEngine::Warning
,
673 ("equal-to expression of the form 'false == true' can"
674 " literally be simplified as 'false'"),
676 << expr
->getSourceRange();
684 DiagnosticsEngine::Warning
,
685 ("equal-to expression of the form 'true == A' (with A of type"
686 " %0) can %select{logically|literally}1 be simplified as 'A'"),
688 << expr
->getRHS()->IgnoreImpCasts()->getType()
689 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
690 << expr
->getSourceRange();
694 DiagnosticsEngine::Warning
,
695 ("equal-to expression of the form 'true == false' can"
696 " literally be simplified as 'false'"),
698 << expr
->getSourceRange();
702 DiagnosticsEngine::Warning
,
703 ("equal-to expression of the form 'true == true' can"
704 " literally be simplified as 'true'"),
706 << expr
->getSourceRange();
714 bool SimplifyBool::VisitBinNE(BinaryOperator
const * expr
) {
715 if (ignoreLocation(expr
)) {
718 if (!(expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
719 && expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()))
723 auto v1
= getValue(expr
->getLHS());
724 auto v2
= getValue(expr
->getRHS());
732 DiagnosticsEngine::Warning
,
733 ("not-equal-to expression of the form 'A != false' (with A of"
734 " type %0) can %select{logically|literally}1 be simplified as"
737 << expr
->getLHS()->IgnoreImpCasts()->getType()
738 << expr
->getLHS()->IgnoreImpCasts()->getType()->isBooleanType()
739 << expr
->getSourceRange();
743 auto e
= getSubExprOfLogicalNegation(expr
->getLHS());
746 DiagnosticsEngine::Warning
,
747 ("not-equal-to expression of the form 'A != true' (with"
748 " A of type %0) can %select{logically|literally}1 be"
749 " simplified as '!A'"),
751 << expr
->getLHS()->IgnoreImpCasts()->getType()
752 << (expr
->getLHS()->IgnoreImpCasts()->getType()
754 << expr
->getSourceRange();
757 DiagnosticsEngine::Warning
,
758 ("not-equal-to expression of the form '!A != true'"
759 " (with A of type %0) can"
760 " %select{logically|literally}1 be simplified as 'A'"),
762 << e
->IgnoreImpCasts()->getType()
763 << e
->IgnoreImpCasts()->getType()->isBooleanType()
764 << expr
->getSourceRange();
774 DiagnosticsEngine::Warning
,
775 ("not-equal-to expression of the form 'false != A' (with A of"
776 " type %0) can %select{logically|literally}1 be simplified as"
779 << expr
->getRHS()->IgnoreImpCasts()->getType()
780 << expr
->getRHS()->IgnoreImpCasts()->getType()->isBooleanType()
781 << expr
->getSourceRange();
785 DiagnosticsEngine::Warning
,
786 ("not-equal-to expression of the form 'false != false' can"
787 " literally be simplified as 'false'"),
789 << expr
->getSourceRange();
793 DiagnosticsEngine::Warning
,
794 ("not-equal-to expression of the form 'false != true' can"
795 " literally be simplified as 'true'"),
797 << expr
->getSourceRange();
805 auto e
= getSubExprOfLogicalNegation(expr
->getRHS());
808 DiagnosticsEngine::Warning
,
809 ("not-equal-to expression of the form 'true != A' (with"
810 " A of type %0) can %select{logically|literally}1 be"
811 " simplified as '!A'"),
813 << expr
->getRHS()->IgnoreImpCasts()->getType()
814 << (expr
->getRHS()->IgnoreImpCasts()->getType()
816 << expr
->getSourceRange();
819 DiagnosticsEngine::Warning
,
820 ("not-equal-to expression of the form 'true != !A'"
821 " (with A of type %0) can"
822 " %select{logically|literally}1 be simplified as 'A'"),
824 << e
->IgnoreImpCasts()->getType()
825 << e
->IgnoreImpCasts()->getType()->isBooleanType()
826 << expr
->getSourceRange();
832 DiagnosticsEngine::Warning
,
833 ("not-equal-to expression of the form 'true != false' can"
834 " literally be simplified as 'true'"),
836 << expr
->getSourceRange();
840 DiagnosticsEngine::Warning
,
841 ("not-equal-to expression of the form 'true != true' can"
842 " literally be simplified as 'false'"),
844 << expr
->getSourceRange();
852 bool SimplifyBool::VisitConditionalOperator(ConditionalOperator
const * expr
) {
853 if (ignoreLocation(expr
)) {
856 auto v1
= getValue(expr
->getTrueExpr());
857 auto v2
= getValue(expr
->getFalseExpr());
865 DiagnosticsEngine::Warning
,
866 ("conditional expression of the form 'A ? B : false' (with A of"
867 " type %0 and B of type %1) can %select{logically|literally}2"
868 " be simplified as 'A && B'"),
870 << expr
->getCond()->IgnoreImpCasts()->getType()
871 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
872 << ((expr
->getCond()->IgnoreImpCasts()->getType()
874 && (expr
->getTrueExpr()->IgnoreImpCasts()->getType()
876 << expr
->getSourceRange();
880 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
883 DiagnosticsEngine::Warning
,
884 ("conditional expression of the form 'A ? B : true'"
885 " (with A of type %0 and B of type %1) can"
886 " %select{logically|literally}2 be simplified as '!A"
889 << expr
->getCond()->IgnoreImpCasts()->getType()
890 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
891 << ((expr
->getCond()->IgnoreImpCasts()->getType()
893 && (expr
->getTrueExpr()->IgnoreImpCasts()->getType()
895 << expr
->getSourceRange();
898 DiagnosticsEngine::Warning
,
899 ("conditional expression of the form '!A ? B : true'"
900 " (with A of type %0 and B of type %1) can"
901 " %select{logically|literally}2 be simplified as 'A ||"
904 << e
->IgnoreImpCasts()->getType()
905 << expr
->getTrueExpr()->IgnoreImpCasts()->getType()
906 << (e
->IgnoreImpCasts()->getType()->isBooleanType()
907 && (expr
->getTrueExpr()->IgnoreImpCasts()
908 ->getType()->isBooleanType()))
909 << expr
->getSourceRange();
919 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
922 DiagnosticsEngine::Warning
,
923 ("conditional expression of the form 'A ? false : B'"
924 " (with A of type %0 and B of type %1) can"
925 " %select{logically|literally}2 be simplified as '!A"
928 << expr
->getCond()->IgnoreImpCasts()->getType()
929 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
930 << ((expr
->getCond()->IgnoreImpCasts()->getType()
932 && (expr
->getFalseExpr()->IgnoreImpCasts()
933 ->getType()->isBooleanType()))
934 << expr
->getSourceRange();
937 DiagnosticsEngine::Warning
,
938 ("conditional expression of the form '!A ? false : B'"
939 " (with A of type %0 and B of type %1) can"
940 " %select{logically|literally}2 be simplified as 'A &&"
943 << e
->IgnoreImpCasts()->getType()
944 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
945 << (e
->IgnoreImpCasts()->getType()->isBooleanType()
946 && (expr
->getFalseExpr()->IgnoreImpCasts()
947 ->getType()->isBooleanType()))
948 << expr
->getSourceRange();
954 DiagnosticsEngine::Warning
,
955 ("conditional expression of the form 'A ? false : false' (with"
956 " A of type %0) can logically be simplified as 'false'"),
958 << expr
->getCond()->IgnoreImpCasts()->getType()
959 << expr
->getSourceRange();
963 auto e
= getSubExprOfLogicalNegation(expr
->getCond());
966 DiagnosticsEngine::Warning
,
967 ("conditional expression of the form 'A ? false : true'"
968 " (with A of type %0) can"
969 " %select{logically|literally}1 be simplified as"
972 << expr
->getCond()->IgnoreImpCasts()->getType()
973 << (expr
->getCond()->IgnoreImpCasts()->getType()
975 << expr
->getSourceRange();
978 DiagnosticsEngine::Warning
,
979 ("conditional expression of the form '!A ? false :"
980 " true' (with A of type %0) can"
981 " %select{logically|literally}1 be simplified as 'A'"),
983 << e
->IgnoreImpCasts()->getType()
984 << e
->IgnoreImpCasts()->getType()->isBooleanType()
985 << expr
->getSourceRange();
995 DiagnosticsEngine::Warning
,
996 ("conditional expression of the form 'A ? true : B' (with A of"
997 " type %0 and B of type %1) can %select{logically|literally}2"
998 " be simplified as 'A || B'"),
1000 << expr
->getCond()->IgnoreImpCasts()->getType()
1001 << expr
->getFalseExpr()->IgnoreImpCasts()->getType()
1002 << ((expr
->getCond()->IgnoreImpCasts()->getType()
1004 && (expr
->getFalseExpr()->IgnoreImpCasts()->getType()
1006 << expr
->getSourceRange();
1010 DiagnosticsEngine::Warning
,
1011 ("conditional expression of the form 'A ? true : false' (with A"
1012 " of type %0) can %select{logically|literally}1 be simplified"
1014 expr
->getLocStart())
1015 << expr
->getCond()->IgnoreImpCasts()->getType()
1016 << expr
->getCond()->IgnoreImpCasts()->getType()->isBooleanType()
1017 << expr
->getSourceRange();
1021 DiagnosticsEngine::Warning
,
1022 ("conditional expression of the form 'A ? true : true' (with A"
1023 " of type %0) can logically be simplified as 'true'"),
1024 expr
->getLocStart())
1025 << expr
->getCond()->IgnoreImpCasts()->getType()
1026 << expr
->getSourceRange();
1034 loplugin::Plugin::Registration
<SimplifyBool
> X("simplifybool");
1038 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */