Skip to content

Commit

Permalink
Fix a false positive in misplaced-widening-cast
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JonasToth committed Dec 5, 2018
1 parent 381b4fb commit c361e0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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);
}

0 comments on commit c361e0d

Please sign in to comment.