Skip to content

Commit b8244b3

Browse files
committed
[clang] adding diagnose for impl-cast (draft)
This is an unfinished implementation of llvm#33528
1 parent 8129ba6 commit b8244b3

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8477,6 +8477,8 @@ def err_incomplete_object_call : Error<
84778477
def warn_condition_is_assignment : Warning<"using the result of an "
84788478
"assignment as a condition without parentheses">,
84798479
InGroup<Parentheses>;
8480+
def warn_parens_bool_assign : Warning<"suggest parentheses around assignment used as truth value">,
8481+
InGroup<Parentheses>;
84808482
def warn_free_nonheap_object
84818483
: Warning<"attempt to call %0 on non-heap %select{object %2|object: block expression|object: lambda-to-function-pointer conversion}1">,
84828484
InGroup<FreeNonHeapObject>;

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7232,6 +7232,8 @@ class Sema final : public SemaBase {
72327232
/// being used as a boolean condition, warn if it's an assignment.
72337233
void DiagnoseAssignmentAsCondition(Expr *E);
72347234

7235+
void DiagnoseImplicitCastBoolAssignment(Expr *E, QualType ToType);
7236+
72357237
/// Redundant parentheses over an equality comparison can indicate
72367238
/// that the user intended an assignment used as condition.
72377239
void DiagnoseEqualityWithExtraParens(ParenExpr *ParenE);

clang/lib/Sema/Sema.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,18 @@ void Sema::diagnoseZeroToNullptrConversion(CastKind Kind, const Expr *E) {
687687
<< FixItHint::CreateReplacement(E->getSourceRange(), "nullptr");
688688
}
689689

690+
void Sema::DiagnoseImplicitCastBoolAssignment(Expr* E,QualType Ty) {
691+
if (Ty->isBooleanType()) {
692+
if (BinaryOperator* Op = dyn_cast<BinaryOperator>(E)) {
693+
// should only be issued for regular assignment `=`, not for e.g `+=`
694+
if (Op->getOpcode() == BO_Assign) {
695+
SourceLocation Loc = Op->getOperatorLoc();
696+
Diag(Loc,diag::warn_parens_bool_assign) << E->getSourceRange();
697+
}
698+
}
699+
}
700+
}
701+
690702
/// ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
691703
/// If there is already an implicit cast, merge into the existing one.
692704
/// The result is of the given category.
@@ -761,6 +773,8 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
761773
}
762774
}
763775

776+
DiagnoseImplicitCastBoolAssignment(E,Ty);
777+
764778
if (ImplicitCastExpr *ImpCast = dyn_cast<ImplicitCastExpr>(E)) {
765779
if (ImpCast->getCastKind() == Kind && (!BasePath || BasePath->empty())) {
766780
ImpCast->setType(Ty);

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/AST/CXXInheritance.h"
1919
#include "clang/AST/CharUnits.h"
2020
#include "clang/AST/DeclObjC.h"
21+
#include "clang/AST/Expr.h"
2122
#include "clang/AST/ExprCXX.h"
2223
#include "clang/AST/ExprConcepts.h"
2324
#include "clang/AST/ExprObjC.h"
@@ -4392,6 +4393,8 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
43924393
FromType = From->getType();
43934394
}
43944395

4396+
DiagnoseImplicitCastBoolAssignment(From,ToType);
4397+
43954398
// If we're converting to an atomic type, first convert to the corresponding
43964399
// non-atomic type.
43974400
QualType ToAtomicType;

0 commit comments

Comments
 (0)