Fix a false positive in misplaced-widening-cast

Summary:
bugprone-misplaced-widening-cast check
used to give a false warning to the
following example.

 enum DaysEnum{
    MON = 0,
    TUE = 1
    };

 day = (DaysEnum)(day + 1);
 //warning: either cast from 'int' to 'DaysEnum' is ineffective...

But i think int to enum cast is not widening neither ineffective.

Patch by dkrupp.

Reviewers: JonasToth, alexfh

Reviewed By: alexfh

Subscribers: rnkovacs, Szelethus, gamesh411, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D55255

llvm-svn: 348341
This commit is contained in:
Jonas Toth 2018-12-05 08:29:56 +00:00
parent 381b4fb0ab
commit c361e0d627
2 changed files with 21 additions and 2 deletions

View File

@ -213,8 +213,9 @@ void MisplacedWideningCastCheck::check(const MatchFinder::MatchResult &Result) {
dyn_cast<BuiltinType>(CastType->getUnqualifiedDesugaredType());
const auto *CalcBuiltinType =
dyn_cast<BuiltinType>(CalcType->getUnqualifiedDesugaredType());
if (CastBuiltinType && CalcBuiltinType &&
!isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))
if (!CastBuiltinType || !CalcBuiltinType)
return;
if (!isFirstWider(CastBuiltinType->getKind(), CalcBuiltinType->getKind()))
return;
}

View File

@ -62,3 +62,21 @@ template <class> class A {
enum Type {};
static char *m_fn1() { char p = (Type)(&p - m_fn1()); }
};
enum DaysEnum {
MON,
TUE,
WED,
THR,
FRI,
SAT,
SUN
};
// Do not warn for int to enum casts.
void nextDay(DaysEnum day) {
if (day < SUN)
day = (DaysEnum)(day + 1);
if (day < SUN)
day = static_cast<DaysEnum>(day + 1);
}