From 81c895fb3f25340c16856e13c40326f9975dbc6a Mon Sep 17 00:00:00 2001 From: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Date: Wed, 10 Mar 2021 12:36:47 -0800 Subject: [PATCH] Fix various C++ warnings (#31002) Summary: Fix warnings about implicit type truncation. ## Changelog [Internal] [Fixed] - Fix various C++ warnings Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- ReactCommon/cxxreact/MethodCall.cpp | 4 ++-- .../core/ReactCommon/TurboModule.cpp | 2 +- .../renderer/components/image/conversions.h | 2 +- .../view/YogaLayoutableShadowNode.cpp | 8 ++++--- .../renderer/components/view/conversions.h | 4 ++-- .../react/renderer/core/RawPropsKey.cpp | 8 ++++--- .../react/renderer/core/RawPropsKeyMap.cpp | 4 ++-- .../react/renderer/core/RawPropsParser.cpp | 8 ++++--- ReactCommon/react/renderer/core/RawValue.h | 4 ++-- .../react/renderer/graphics/conversions.h | 2 +- .../renderer/mounting/Differentiator.cpp | 24 +++++++++++-------- .../react/renderer/mounting/ShadowTree.cpp | 2 +- .../textlayoutmanager/TextMeasureCache.cpp | 18 +++++++------- ReactCommon/yoga/yoga/BitUtils.h | 5 ++-- 14 files changed, 54 insertions(+), 41 deletions(-) diff --git a/ReactCommon/cxxreact/MethodCall.cpp b/ReactCommon/cxxreact/MethodCall.cpp index f5f541265a5cb5..3a22b8994b9c4b 100644 --- a/ReactCommon/cxxreact/MethodCall.cpp +++ b/ReactCommon/cxxreact/MethodCall.cpp @@ -73,8 +73,8 @@ std::vector parseMethodCalls(folly::dynamic &&jsonData) { } methodCalls.emplace_back( - moduleIds[i].asInt(), - methodIds[i].asInt(), + static_cast(moduleIds[i].asInt()), + static_cast(methodIds[i].asInt()), std::move(params[i]), callId); diff --git a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp index fe1f57d2b45abb..f7045b3c89f50c 100644 --- a/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp +++ b/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp @@ -32,7 +32,7 @@ jsi::Value TurboModule::get( return jsi::Function::createFromHostFunction( runtime, propName, - meta.argCount, + static_cast(meta.argCount), [this, meta]( facebook::jsi::Runtime &rt, const facebook::jsi::Value &thisVal, diff --git a/ReactCommon/react/renderer/components/image/conversions.h b/ReactCommon/react/renderer/components/image/conversions.h index fa1dd602161e12..f0cf1dc1481071 100644 --- a/ReactCommon/react/renderer/components/image/conversions.h +++ b/ReactCommon/react/renderer/components/image/conversions.h @@ -50,7 +50,7 @@ inline void fromRawValue(const RawValue &value, ImageSource &result) { items.at("scale").hasType()) { 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() && diff --git a/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 5f17f378a05133..190d3e9db5deff 100644 --- a/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -129,7 +129,8 @@ void YogaLayoutableShadowNode::appendYogaChild(ShadowNode const &childNode) { auto &layoutableChildNode = traitCast(childNode); yogaNode_.insertChild( - &layoutableChildNode.yogaNode_, yogaNode_.getChildren().size()); + &layoutableChildNode.yogaNode_, + static_cast(yogaNode_.getChildren().size())); ensureYogaChildrenLookFine(); } @@ -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(index)); // Replace the Yoga node inside the Yoga node children list. - yogaNode_.replaceChild(&layoutableClonedChildNode.yogaNode_, index); + yogaNode_.replaceChild( + &layoutableClonedChildNode.yogaNode_, static_cast(index)); } ensureYogaChildrenLookFine(); diff --git a/ReactCommon/react/renderer/components/view/conversions.h b/ReactCommon/react/renderer/components/view/conversions.h index c6d77567ba49be..c4b088df058b8a 100644 --- a/ReactCommon/react/renderer/components/view/conversions.h +++ b/ReactCommon/react/renderer/components/view/conversions.h @@ -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(num * M_PI / 180.0f); } - return num; // assume suffix is "rad" + return static_cast(num); // assume suffix is "rad" } inline void fromRawValue(const RawValue &value, Transform &result) { diff --git a/ReactCommon/react/renderer/core/RawPropsKey.cpp b/ReactCommon/react/renderer/core/RawPropsKey.cpp index 87af35384c3d8c..cdaab91f7d5e20 100644 --- a/ReactCommon/react/renderer/core/RawPropsKey.cpp +++ b/ReactCommon/react/renderer/core/RawPropsKey.cpp @@ -22,19 +22,21 @@ void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length) // Prefix if (prefix) { - auto prefixLength = std::strlen(prefix); + auto prefixLength = + static_cast(std::strlen(prefix)); std::memcpy(buffer, prefix, prefixLength); *length = prefixLength; } // Name - auto nameLength = std::strlen(name); + auto nameLength = static_cast(std::strlen(name)); std::memcpy(buffer + *length, name, nameLength); *length += nameLength; // Suffix if (suffix) { - int suffixLength = std::strlen(suffix); + auto suffixLength = + static_cast(std::strlen(suffix)); std::memcpy(buffer + *length, suffix, suffixLength); *length += suffixLength; } diff --git a/ReactCommon/react/renderer/core/RawPropsKeyMap.cpp b/ReactCommon/react/renderer/core/RawPropsKeyMap.cpp index a06f5c497aa9c2..0ae4c9a3a2711b 100644 --- a/ReactCommon/react/renderer/core/RawPropsKeyMap.cpp +++ b/ReactCommon/react/renderer/core/RawPropsKeyMap.cpp @@ -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(i); } length = item.length; } } for (auto j = length; j < buckets_.size(); j++) { - buckets_[j] = items_.size(); + buckets_[j] = static_cast(items_.size()); } } diff --git a/ReactCommon/react/renderer/core/RawPropsParser.cpp b/ReactCommon/react/renderer/core/RawPropsParser.cpp index a90e075c3ba2e5..81142f3aa1f266 100644 --- a/ReactCommon/react/renderer/core/RawPropsParser.cpp +++ b/ReactCommon/react/renderer/core/RawPropsParser.cpp @@ -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(size_)); size_++; return nullptr; } @@ -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(name.size())); if (keyIndex == kRawPropsValueIndexEmpty) { continue; } @@ -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(name.size())); if (keyIndex == kRawPropsValueIndexEmpty) { continue; } diff --git a/ReactCommon/react/renderer/core/RawValue.h b/ReactCommon/react/renderer/core/RawValue.h index 1e0881e5bace81..16cab0bd7b9c4a 100644 --- a/ReactCommon/react/renderer/core/RawValue.h +++ b/ReactCommon/react/renderer/core/RawValue.h @@ -215,7 +215,7 @@ class RawValue { } static int castValue(const folly::dynamic &dynamic, int *type) noexcept { - return dynamic.asInt(); + return static_cast(dynamic.asInt()); } static int64_t castValue( @@ -225,7 +225,7 @@ class RawValue { } static float castValue(const folly::dynamic &dynamic, float *type) noexcept { - return dynamic.asDouble(); + return static_cast(dynamic.asDouble()); } static double castValue( diff --git a/ReactCommon/react/renderer/graphics/conversions.h b/ReactCommon/react/renderer/graphics/conversions.h index df1b5b1442abb2..1f910dd07096ca 100644 --- a/ReactCommon/react/renderer/graphics/conversions.h +++ b/ReactCommon/react/renderer/graphics/conversions.h @@ -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(); } diff --git a/ReactCommon/react/renderer/mounting/Differentiator.cpp b/ReactCommon/react/renderer/mounting/Differentiator.cpp index e5fa4b6abcf055..9edd3060099a9c 100644 --- a/ReactCommon/react/renderer/mounting/Differentiator.cpp +++ b/ReactCommon/react/renderer/mounting/Differentiator.cpp @@ -423,13 +423,13 @@ static void calculateShadowViewMutationsFlattener( ShadowViewMutation::RemoveMutation( node.shadowView, treeChildPair.shadowView, - treeChildPair.mountIndex)); + static_cast(treeChildPair.mountIndex))); } else { mutationInstructionContainer.insertMutations.push_back( ShadowViewMutation::InsertMutation( node.shadowView, treeChildPair.shadowView, - treeChildPair.mountIndex)); + static_cast(treeChildPair.mountIndex))); } } @@ -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(oldChildPair.mountIndex))); // We also have to call the algorithm recursively to clean up the entire // subtree starting from the removed view. @@ -911,7 +913,9 @@ static void calculateShadowViewMutationsV2( } insertMutations.push_back(ShadowViewMutation::InsertMutation( - parentShadowView, newChildPair.shadowView, newChildPair.mountIndex)); + parentShadowView, + newChildPair.shadowView, + static_cast(newChildPair.mountIndex))); createMutations.push_back( ShadowViewMutation::CreateMutation(newChildPair.shadowView)); @@ -969,14 +973,14 @@ static void calculateShadowViewMutationsV2( insertMutations.push_back(ShadowViewMutation::InsertMutation( parentShadowView, newChildPair.shadowView, - newChildPair.mountIndex)); + static_cast(newChildPair.mountIndex))); createMutations.push_back( ShadowViewMutation::CreateMutation(newChildPair.shadowView)); } else { removeMutations.push_back(ShadowViewMutation::RemoveMutation( parentShadowView, oldChildPair.shadowView, - oldChildPair.mountIndex)); + static_cast(oldChildPair.mountIndex))); deleteMutations.push_back( ShadowViewMutation::DeleteMutation(oldChildPair.shadowView)); } @@ -1211,7 +1215,7 @@ static void calculateShadowViewMutationsV2( removeMutations.push_back(ShadowViewMutation::RemoveMutation( parentShadowView, oldChildPair.shadowView, - oldChildPair.mountIndex)); + static_cast(oldChildPair.mountIndex))); deleteMutations.push_back( ShadowViewMutation::DeleteMutation(oldChildPair.shadowView)); } @@ -1226,7 +1230,7 @@ static void calculateShadowViewMutationsV2( removeMutations.push_back(ShadowViewMutation::RemoveMutation( parentShadowView, oldChildPair.shadowView, - oldChildPair.mountIndex)); + static_cast(oldChildPair.mountIndex))); if (oldChildPair.shadowView != newChildPair.shadowView) { updateMutations.push_back(ShadowViewMutation::UpdateMutation( @@ -1272,7 +1276,7 @@ static void calculateShadowViewMutationsV2( removeMutations.push_back(ShadowViewMutation::RemoveMutation( parentShadowView, oldChildPair.shadowView, - oldChildPair.mountIndex)); + static_cast(oldChildPair.mountIndex))); deletionCandidatePairs.insert( {oldChildPair.shadowView.tag, &oldChildPair}); @@ -1300,7 +1304,7 @@ static void calculateShadowViewMutationsV2( insertMutations.push_back(ShadowViewMutation::InsertMutation( parentShadowView, newChildPair.shadowView, - newChildPair.mountIndex)); + static_cast(newChildPair.mountIndex))); } // `inOtherTree` is only set to true during flattening/unflattening of diff --git a/ReactCommon/react/renderer/mounting/ShadowTree.cpp b/ReactCommon/react/renderer/mounting/ShadowTree.cpp index 14299f4c2dae83..4592c5c35c9a21 100644 --- a/ReactCommon/react/renderer/mounting/ShadowTree.cpp +++ b/ReactCommon/react/renderer/mounting/ShadowTree.cpp @@ -386,7 +386,7 @@ CommitStatus ShadowTree::tryCommit( } telemetry.didCommit(); - telemetry.setRevisionNumber(newRevisionNumber); + telemetry.setRevisionNumber(static_cast(newRevisionNumber)); newRevision = ShadowTreeRevision{newRootShadowNode, newRevisionNumber, telemetry}; diff --git a/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.cpp b/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.cpp index d3d9845f93c518..db9910fecb5a16 100644 --- a/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.cpp +++ b/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.cpp @@ -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(data.getDefault("x", 0).getDouble()); + origin.y = static_cast(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(data.getDefault("width", 0).getDouble()); + size.height = static_cast(data.getDefault("height", 0).getDouble()); Rect frame; frame.origin = origin; frame.size = size; @@ -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(data.getDefault("descender", 0).getDouble())), + capHeight( + static_cast(data.getDefault("capHeight", 0).getDouble())), + ascender(static_cast(data.getDefault("ascender", 0).getDouble())), + xHeight(static_cast(data.getDefault("xHeight", 0).getDouble())) {} bool LineMeasurement::operator==(LineMeasurement const &rhs) const { return std::tie( diff --git a/ReactCommon/yoga/yoga/BitUtils.h b/ReactCommon/yoga/yoga/BitUtils.h index 1c32e9ecc35561..2161effc7bb6fe 100644 --- a/ReactCommon/yoga/yoga/BitUtils.h +++ b/ReactCommon/yoga/yoga/BitUtils.h @@ -45,8 +45,9 @@ void setEnumData(uint32_t& flags, size_t index, int newValue) { template void setEnumData(uint8_t& flags, size_t index, int newValue) { - flags = (flags & ~mask(bitWidthFn(), index)) | - ((newValue << index) & (mask(bitWidthFn(), index))); + flags = (flags & ~static_cast(mask(bitWidthFn(), index))) | + ((newValue << index) & + (static_cast(mask(bitWidthFn(), index)))); } constexpr bool getBooleanData(int flags, size_t index) {