Skip to content

Commit 81c895f

Browse files
acoates-msfacebook-github-bot
authored andcommitted
Fix various C++ warnings (#31002)
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
1 parent 4170726 commit 81c895f

File tree

14 files changed

+54
-41
lines changed

14 files changed

+54
-41
lines changed

ReactCommon/cxxreact/MethodCall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ std::vector<MethodCall> parseMethodCalls(folly::dynamic &&jsonData) {
7373
}
7474

7575
methodCalls.emplace_back(
76-
moduleIds[i].asInt(),
77-
methodIds[i].asInt(),
76+
static_cast<int>(moduleIds[i].asInt()),
77+
static_cast<int>(methodIds[i].asInt()),
7878
std::move(params[i]),
7979
callId);
8080

ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jsi::Value TurboModule::get(
3232
return jsi::Function::createFromHostFunction(
3333
runtime,
3434
propName,
35-
meta.argCount,
35+
static_cast<unsigned int>(meta.argCount),
3636
[this, meta](
3737
facebook::jsi::Runtime &rt,
3838
const facebook::jsi::Value &thisVal,

ReactCommon/react/renderer/components/image/conversions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inline void fromRawValue(const RawValue &value, ImageSource &result) {
5050
items.at("scale").hasType<Float>()) {
5151
result.scale = (Float)items.at("scale");
5252
} else {
53-
result.scale = items.find("deprecated") != items.end() ? 0.0 : 1.0;
53+
result.scale = items.find("deprecated") != items.end() ? 0.0f : 1.0f;
5454
}
5555

5656
if (items.find("url") != items.end() &&

ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ void YogaLayoutableShadowNode::appendYogaChild(ShadowNode const &childNode) {
129129
auto &layoutableChildNode =
130130
traitCast<YogaLayoutableShadowNode const &>(childNode);
131131
yogaNode_.insertChild(
132-
&layoutableChildNode.yogaNode_, yogaNode_.getChildren().size());
132+
&layoutableChildNode.yogaNode_,
133+
static_cast<uint32_t>(yogaNode_.getChildren().size()));
133134

134135
ensureYogaChildrenLookFine();
135136
}
@@ -178,10 +179,11 @@ void YogaLayoutableShadowNode::adoptYogaChild(size_t index) {
178179
layoutableClonedChildNode.yogaNode_.setOwner(&yogaNode_);
179180

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

183184
// Replace the Yoga node inside the Yoga node children list.
184-
yogaNode_.replaceChild(&layoutableClonedChildNode.yogaNode_, index);
185+
yogaNode_.replaceChild(
186+
&layoutableClonedChildNode.yogaNode_, static_cast<int>(index));
185187
}
186188

187189
ensureYogaChildrenLookFine();

ReactCommon/react/renderer/components/view/conversions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,9 +399,9 @@ inline Float toRadians(const RawValue &value) {
399399
stringValue.c_str(), &suffixStart); // can't use std::stod, probably
400400
// because of old Android NDKs
401401
if (0 == strncmp(suffixStart, "deg", 3)) {
402-
return num * M_PI / 180;
402+
return static_cast<Float>(num * M_PI / 180.0f);
403403
}
404-
return num; // assume suffix is "rad"
404+
return static_cast<Float>(num); // assume suffix is "rad"
405405
}
406406

407407
inline void fromRawValue(const RawValue &value, Transform &result) {

ReactCommon/react/renderer/core/RawPropsKey.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@ void RawPropsKey::render(char *buffer, RawPropsPropNameLength *length)
2222

2323
// Prefix
2424
if (prefix) {
25-
auto prefixLength = std::strlen(prefix);
25+
auto prefixLength =
26+
static_cast<RawPropsPropNameLength>(std::strlen(prefix));
2627
std::memcpy(buffer, prefix, prefixLength);
2728
*length = prefixLength;
2829
}
2930

3031
// Name
31-
auto nameLength = std::strlen(name);
32+
auto nameLength = static_cast<RawPropsPropNameLength>(std::strlen(name));
3233
std::memcpy(buffer + *length, name, nameLength);
3334
*length += nameLength;
3435

3536
// Suffix
3637
if (suffix) {
37-
int suffixLength = std::strlen(suffix);
38+
auto suffixLength =
39+
static_cast<RawPropsPropNameLength>(std::strlen(suffix));
3840
std::memcpy(buffer + *length, suffix, suffixLength);
3941
*length += suffixLength;
4042
}

ReactCommon/react/renderer/core/RawPropsKeyMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ void RawPropsKeyMap::reindex() noexcept {
6767
auto &item = items_[i];
6868
if (item.length != length) {
6969
for (auto j = length; j < item.length; j++) {
70-
buckets_[j] = i;
70+
buckets_[j] = static_cast<RawPropsPropNameLength>(i);
7171
}
7272
length = item.length;
7373
}
7474
}
7575

7676
for (auto j = length; j < buckets_.size(); j++) {
77-
buckets_[j] = items_.size();
77+
buckets_[j] = static_cast<RawPropsPropNameLength>(items_.size());
7878
}
7979
}
8080

ReactCommon/react/renderer/core/RawPropsParser.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RawValue const *RawPropsParser::at(
4141
// This is not thread-safe part; this happens only during initialization of
4242
// a `ComponentDescriptor` where it is actually safe.
4343
keys_.push_back(key);
44-
nameToIndex_.insert(key, size_);
44+
nameToIndex_.insert(key, static_cast<RawPropsValueIndex>(size_));
4545
size_++;
4646
return nullptr;
4747
}
@@ -120,7 +120,8 @@ void RawPropsParser::preparse(RawProps const &rawProps) const noexcept {
120120

121121
auto name = nameValue.utf8(runtime);
122122

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

144-
auto keyIndex = nameToIndex_.at(name.data(), name.size());
145+
auto keyIndex = nameToIndex_.at(
146+
name.data(), static_cast<RawPropsPropNameLength>(name.size()));
145147
if (keyIndex == kRawPropsValueIndexEmpty) {
146148
continue;
147149
}

ReactCommon/react/renderer/core/RawValue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class RawValue {
215215
}
216216

217217
static int castValue(const folly::dynamic &dynamic, int *type) noexcept {
218-
return dynamic.asInt();
218+
return static_cast<int>(dynamic.asInt());
219219
}
220220

221221
static int64_t castValue(
@@ -225,7 +225,7 @@ class RawValue {
225225
}
226226

227227
static float castValue(const folly::dynamic &dynamic, float *type) noexcept {
228-
return dynamic.asDouble();
228+
return static_cast<float>(dynamic.asDouble());
229229
}
230230

231231
static double castValue(

ReactCommon/react/renderer/graphics/conversions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ inline void fromRawValue(const RawValue &value, SharedColor &result) {
3939
red = items.at(0);
4040
green = items.at(1);
4141
blue = items.at(2);
42-
alpha = length == 4 ? items.at(3) : 1.0;
42+
alpha = length == 4 ? items.at(3) : 1.0f;
4343
} else {
4444
abort();
4545
}

0 commit comments

Comments
 (0)