Skip to content

Commit

Permalink
Fix various C++ warnings (#31002)
Browse files Browse the repository at this point in the history
Summary:
Fix warnings about implicit type truncation.

## Changelog

[Internal] [Fixed] - Fix various C++ warnings

Pull Request resolved: #31002

Test Plan:
Almost all the changes here are simply making explicit conversions which are already occurring.  With the exception of a couple of constants being changed from doubles to floats.

With these changes I am able to remove a bunch of warning suppressions in react-native-windows.

Reviewed By: shergin

Differential Revision: D26900502

Pulled By: rozele

fbshipit-source-id: d5e415282815c2212a840a863713287bbf118c10
  • Loading branch information
acoates-ms authored and facebook-github-bot committed Mar 10, 2021
1 parent 4170726 commit 81c895f
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 41 deletions.
4 changes: 2 additions & 2 deletions ReactCommon/cxxreact/MethodCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ std::vector<MethodCall> parseMethodCalls(folly::dynamic &&jsonData) {
}

methodCalls.emplace_back(
moduleIds[i].asInt(),
methodIds[i].asInt(),
static_cast<int>(moduleIds[i].asInt()),
static_cast<int>(methodIds[i].asInt()),
std::move(params[i]),
callId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jsi::Value TurboModule::get(
return jsi::Function::createFromHostFunction(
runtime,
propName,
meta.argCount,
static_cast<unsigned int>(meta.argCount),
[this, meta](
facebook::jsi::Runtime &rt,
const facebook::jsi::Value &thisVal,
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/components/image/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ inline void fromRawValue(const RawValue &value, ImageSource &result) {
items.at("scale").hasType<Float>()) {
result.scale = (Float)items.at("scale");
} else {
result.scale = items.find("deprecated") != items.end() ? 0.0 : 1.0;
result.scale = items.find("deprecated") != items.end() ? 0.0f : 1.0f;
}

if (items.find("url") != items.end() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ void YogaLayoutableShadowNode::appendYogaChild(ShadowNode const &childNode) {
auto &layoutableChildNode =
traitCast<YogaLayoutableShadowNode const &>(childNode);
yogaNode_.insertChild(
&layoutableChildNode.yogaNode_, yogaNode_.getChildren().size());
&layoutableChildNode.yogaNode_,
static_cast<uint32_t>(yogaNode_.getChildren().size()));

ensureYogaChildrenLookFine();
}
Expand Down Expand Up @@ -178,10 +179,11 @@ void YogaLayoutableShadowNode::adoptYogaChild(size_t index) {
layoutableClonedChildNode.yogaNode_.setOwner(&yogaNode_);

// Replace the child node with a newly cloned one in the children list.
replaceChild(childNode, clonedChildNode, index);
replaceChild(childNode, clonedChildNode, static_cast<int>(index));

// Replace the Yoga node inside the Yoga node children list.
yogaNode_.replaceChild(&layoutableClonedChildNode.yogaNode_, index);
yogaNode_.replaceChild(
&layoutableClonedChildNode.yogaNode_, static_cast<int>(index));
}

ensureYogaChildrenLookFine();
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/react/renderer/components/view/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ inline Float toRadians(const RawValue &value) {
stringValue.c_str(), &suffixStart); // can't use std::stod, probably
// because of old Android NDKs
if (0 == strncmp(suffixStart, "deg", 3)) {
return num * M_PI / 180;
return static_cast<Float>(num * M_PI / 180.0f);
}
return num; // assume suffix is "rad"
return static_cast<Float>(num); // assume suffix is "rad"
}

inline void fromRawValue(const RawValue &value, Transform &result) {
Expand Down
8 changes: 5 additions & 3 deletions ReactCommon/react/renderer/core/RawPropsKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length)

// Prefix
if (prefix) {
auto prefixLength = std::strlen(prefix);
auto prefixLength =
static_cast<RawPropsPropNameLength>(std::strlen(prefix));
std::memcpy(buffer, prefix, prefixLength);
*length = prefixLength;
}

// Name
auto nameLength = std::strlen(name);
auto nameLength = static_cast<RawPropsPropNameLength>(std::strlen(name));
std::memcpy(buffer + *length, name, nameLength);
*length += nameLength;

// Suffix
if (suffix) {
int suffixLength = std::strlen(suffix);
auto suffixLength =
static_cast<RawPropsPropNameLength>(std::strlen(suffix));
std::memcpy(buffer + *length, suffix, suffixLength);
*length += suffixLength;
}
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/react/renderer/core/RawPropsKeyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ void RawPropsKeyMap::reindex() noexcept {
auto &item = items_[i];
if (item.length != length) {
for (auto j = length; j < item.length; j++) {
buckets_[j] = i;
buckets_[j] = static_cast<RawPropsPropNameLength>(i);
}
length = item.length;
}
}

for (auto j = length; j < buckets_.size(); j++) {
buckets_[j] = items_.size();
buckets_[j] = static_cast<RawPropsPropNameLength>(items_.size());
}
}

Expand Down
8 changes: 5 additions & 3 deletions ReactCommon/react/renderer/core/RawPropsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RawValue const *RawPropsParser::at(
// This is not thread-safe part; this happens only during initialization of
// a `ComponentDescriptor` where it is actually safe.
keys_.push_back(key);
nameToIndex_.insert(key, size_);
nameToIndex_.insert(key, static_cast<RawPropsValueIndex>(size_));
size_++;
return nullptr;
}
Expand Down Expand Up @@ -120,7 +120,8 @@ void RawPropsParser::preparse(RawProps const &rawProps) const noexcept {

auto name = nameValue.utf8(runtime);

auto keyIndex = nameToIndex_.at(name.data(), name.size());
auto keyIndex = nameToIndex_.at(
name.data(), static_cast<RawPropsPropNameLength>(name.size()));
if (keyIndex == kRawPropsValueIndexEmpty) {
continue;
}
Expand All @@ -141,7 +142,8 @@ void RawPropsParser::preparse(RawProps const &rawProps) const noexcept {
for (auto const &pair : dynamic.items()) {
auto name = pair.first.getString();

auto keyIndex = nameToIndex_.at(name.data(), name.size());
auto keyIndex = nameToIndex_.at(
name.data(), static_cast<RawPropsPropNameLength>(name.size()));
if (keyIndex == kRawPropsValueIndexEmpty) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/react/renderer/core/RawValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class RawValue {
}

static int castValue(const folly::dynamic &dynamic, int *type) noexcept {
return dynamic.asInt();
return static_cast<int>(dynamic.asInt());
}

static int64_t castValue(
Expand All @@ -225,7 +225,7 @@ class RawValue {
}

static float castValue(const folly::dynamic &dynamic, float *type) noexcept {
return dynamic.asDouble();
return static_cast<float>(dynamic.asDouble());
}

static double castValue(
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/graphics/conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline void fromRawValue(const RawValue &value, SharedColor &result) {
red = items.at(0);
green = items.at(1);
blue = items.at(2);
alpha = length == 4 ? items.at(3) : 1.0;
alpha = length == 4 ? items.at(3) : 1.0f;
} else {
abort();
}
Expand Down
24 changes: 14 additions & 10 deletions ReactCommon/react/renderer/mounting/Differentiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@ static void calculateShadowViewMutationsFlattener(
ShadowViewMutation::RemoveMutation(
node.shadowView,
treeChildPair.shadowView,
treeChildPair.mountIndex));
static_cast<int>(treeChildPair.mountIndex)));
} else {
mutationInstructionContainer.insertMutations.push_back(
ShadowViewMutation::InsertMutation(
node.shadowView,
treeChildPair.shadowView,
treeChildPair.mountIndex));
static_cast<int>(treeChildPair.mountIndex)));
}
}

Expand Down Expand Up @@ -884,7 +884,9 @@ static void calculateShadowViewMutationsV2(
deleteMutations.push_back(
ShadowViewMutation::DeleteMutation(oldChildPair.shadowView));
removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView, oldChildPair.shadowView, oldChildPair.mountIndex));
parentShadowView,
oldChildPair.shadowView,
static_cast<int>(oldChildPair.mountIndex)));

// We also have to call the algorithm recursively to clean up the entire
// subtree starting from the removed view.
Expand All @@ -911,7 +913,9 @@ static void calculateShadowViewMutationsV2(
}

insertMutations.push_back(ShadowViewMutation::InsertMutation(
parentShadowView, newChildPair.shadowView, newChildPair.mountIndex));
parentShadowView,
newChildPair.shadowView,
static_cast<int>(newChildPair.mountIndex)));
createMutations.push_back(
ShadowViewMutation::CreateMutation(newChildPair.shadowView));

Expand Down Expand Up @@ -969,14 +973,14 @@ static void calculateShadowViewMutationsV2(
insertMutations.push_back(ShadowViewMutation::InsertMutation(
parentShadowView,
newChildPair.shadowView,
newChildPair.mountIndex));
static_cast<int>(newChildPair.mountIndex)));
createMutations.push_back(
ShadowViewMutation::CreateMutation(newChildPair.shadowView));
} else {
removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView,
oldChildPair.shadowView,
oldChildPair.mountIndex));
static_cast<int>(oldChildPair.mountIndex)));
deleteMutations.push_back(
ShadowViewMutation::DeleteMutation(oldChildPair.shadowView));
}
Expand Down Expand Up @@ -1211,7 +1215,7 @@ static void calculateShadowViewMutationsV2(
removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView,
oldChildPair.shadowView,
oldChildPair.mountIndex));
static_cast<int>(oldChildPair.mountIndex)));
deleteMutations.push_back(
ShadowViewMutation::DeleteMutation(oldChildPair.shadowView));
}
Expand All @@ -1226,7 +1230,7 @@ static void calculateShadowViewMutationsV2(
removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView,
oldChildPair.shadowView,
oldChildPair.mountIndex));
static_cast<int>(oldChildPair.mountIndex)));

if (oldChildPair.shadowView != newChildPair.shadowView) {
updateMutations.push_back(ShadowViewMutation::UpdateMutation(
Expand Down Expand Up @@ -1272,7 +1276,7 @@ static void calculateShadowViewMutationsV2(
removeMutations.push_back(ShadowViewMutation::RemoveMutation(
parentShadowView,
oldChildPair.shadowView,
oldChildPair.mountIndex));
static_cast<int>(oldChildPair.mountIndex)));

deletionCandidatePairs.insert(
{oldChildPair.shadowView.tag, &oldChildPair});
Expand Down Expand Up @@ -1300,7 +1304,7 @@ static void calculateShadowViewMutationsV2(
insertMutations.push_back(ShadowViewMutation::InsertMutation(
parentShadowView,
newChildPair.shadowView,
newChildPair.mountIndex));
static_cast<int>(newChildPair.mountIndex)));
}

// `inOtherTree` is only set to true during flattening/unflattening of
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/react/renderer/mounting/ShadowTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ CommitStatus ShadowTree::tryCommit(
}

telemetry.didCommit();
telemetry.setRevisionNumber(newRevisionNumber);
telemetry.setRevisionNumber(static_cast<int>(newRevisionNumber));

newRevision =
ShadowTreeRevision{newRootShadowNode, newRevisionNumber, telemetry};
Expand Down
18 changes: 10 additions & 8 deletions ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ namespace react {

static Rect rectFromDynamic(folly::dynamic const &data) {
Point origin;
origin.x = data.getDefault("x", 0).getDouble();
origin.y = data.getDefault("y", 0).getDouble();
origin.x = static_cast<Float>(data.getDefault("x", 0).getDouble());
origin.y = static_cast<Float>(data.getDefault("y", 0).getDouble());
Size size;
size.width = data.getDefault("width", 0).getDouble();
size.height = data.getDefault("height", 0).getDouble();
size.width = static_cast<Float>(data.getDefault("width", 0).getDouble());
size.height = static_cast<Float>(data.getDefault("height", 0).getDouble());
Rect frame;
frame.origin = origin;
frame.size = size;
Expand All @@ -39,10 +39,12 @@ LineMeasurement::LineMeasurement(
LineMeasurement::LineMeasurement(folly::dynamic const &data)
: text(data.getDefault("text", "").getString()),
frame(rectFromDynamic(data)),
descender(data.getDefault("descender", 0).getDouble()),
capHeight(data.getDefault("capHeight", 0).getDouble()),
ascender(data.getDefault("ascender", 0).getDouble()),
xHeight(data.getDefault("xHeight", 0).getDouble()) {}
descender(
static_cast<Float>(data.getDefault("descender", 0).getDouble())),
capHeight(
static_cast<Float>(data.getDefault("capHeight", 0).getDouble())),
ascender(static_cast<Float>(data.getDefault("ascender", 0).getDouble())),
xHeight(static_cast<Float>(data.getDefault("xHeight", 0).getDouble())) {}

bool LineMeasurement::operator==(LineMeasurement const &rhs) const {
return std::tie(
Expand Down
5 changes: 3 additions & 2 deletions ReactCommon/yoga/yoga/BitUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ void setEnumData(uint32_t& flags, size_t index, int newValue) {

template <typename Enum>
void setEnumData(uint8_t& flags, size_t index, int newValue) {
flags = (flags & ~mask(bitWidthFn<Enum>(), index)) |
((newValue << index) & (mask(bitWidthFn<Enum>(), index)));
flags = (flags & ~static_cast<uint8_t>(mask(bitWidthFn<Enum>(), index))) |
((newValue << index) &
(static_cast<uint8_t>(mask(bitWidthFn<Enum>(), index))));
}

constexpr bool getBooleanData(int flags, size_t index) {
Expand Down

0 comments on commit 81c895f

Please sign in to comment.