Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const ASTContext &Context,
return;
}
const BuiltinType *FromType = getBuiltinType(Rhs);
if (ToType->getKind() < FromType->getKind())
if (!llvm::APFloatBase::isRepresentableBy(
Context.getFloatTypeSemantics(FromType->desugar()),
Context.getFloatTypeSemantics(ToType->desugar())))
diagNarrowType(SourceLoc, Lhs, Rhs);
}
}
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ Changes in existing checks
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
a crash when determining if an ``enable_if[_t]`` was found.

- Improve :doc:`bugprone-narrowing-conversions
<clang-tidy/checks/bugprone/narrowing-conversions>` to avoid incorrect check
results when floating point type is not ``float``, ``double`` and
``long double``.

- Improved :doc:`bugprone-optional-value-conversion
<clang-tidy/checks/bugprone/optional-value-conversion>` to support detecting
conversion directly by ``std::make_unique`` and ``std::make_shared``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) {
f = narrow_double_to_float_return();
}

float narrow_float16_to_float_return(_Float16 f) {
return f;
}

_Float16 narrow_float_to_float16_return(float f) {
return f;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 'float' to '_Float16' [bugprone-narrowing-conversions]
}

void narrow_fp_constants() {
float f;
f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is not a narrowing.
Expand Down
Loading